Documentation ¶
Index ¶
- func AlwaysJSONContentType() gin.HandlerFunc
- func LegacyActionHandler(fn func(w http.ResponseWriter, r *http.Request)) func(*gin.Context)
- func NoMethodHandler(c *gin.Context)
- func NotFoundHandler(c *gin.Context)
- func RespondWithErrorJSON(ctx *gin.Context, err error, status int)
- func WriteCacheableJSONResponse(w http.ResponseWriter, req *http.Request, value any)
- func WriteCustomJSONErrorResponse(w http.ResponseWriter, value any, status int, details ...string)
- func WriteJSONErrorResponse(w http.ResponseWriter, aerr ActionError, status int, details ...string)
- func WriteJSONMultiErrorResponse(w http.ResponseWriter, errors []error, status int)
- func WriteJSONResponse(w http.ResponseWriter, value any)
- func WriteJSONResponseWithStatus(w http.ResponseWriter, status int, value any)
- type ActionError
- type ErrorResponse
- type MultiErrorResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AlwaysJSONContentType ¶ added in v0.3.1
func AlwaysJSONContentType() gin.HandlerFunc
AlwaysJSONContentType middleware ensures that there is always application/json content type written with a response.
func LegacyActionHandler ¶ added in v0.3.0
LegacyActionHandler allows using old-style HTTP action processing methods with "response writer" and "request" arguments. This is mostly for legacy actions from "gorilla/mux" times.
func NoMethodHandler ¶ added in v0.3.0
NoMethodHandler is a handler for "method not allowed" situations
func NotFoundHandler ¶
NotFoundHandler is a handler for "not found" situations
func RespondWithErrorJSON ¶ added in v0.4.4
RespondWithErrorJSON is currently the preferred way how to write error responses. Compared with `WriteJSONErrorResponse` it accepts any type of error but it is able to detect `ActionError` and use it accordingly. It also attaches the error to Gin's Context.
func WriteCacheableJSONResponse ¶
func WriteCacheableJSONResponse(w http.ResponseWriter, req *http.Request, value any)
WriteCacheableJSONResponse writes 'value' to an HTTP response encoded as JSON but before doing that it calculates a checksum of the JSON and in case it is equal to provided 'If-Match' header, 304 is returned. Otherwise a value with ETag header is returned.
func WriteCustomJSONErrorResponse ¶ added in v0.1.1
func WriteCustomJSONErrorResponse(w http.ResponseWriter, value any, status int, details ...string)
WriteCustonmJSONErrorResponse writes any JSON serializable object as an HTTP error response. In case the value cannot be serialized into JSON, the function will write error 500 (Internal Server Error).
func WriteJSONErrorResponse ¶
func WriteJSONErrorResponse(w http.ResponseWriter, aerr ActionError, status int, details ...string)
WriteJSONErrorResponse writes 'aerr' to an HTTP error response as JSON Please note that in most cases, the `RespondWithErrorJSON` is preferred.
func WriteJSONMultiErrorResponse ¶ added in v0.2.1
func WriteJSONMultiErrorResponse(w http.ResponseWriter, errors []error, status int)
func WriteJSONResponse ¶
func WriteJSONResponse(w http.ResponseWriter, value any)
WriteJSONResponse writes 'value' to an HTTP response encoded as JSON
func WriteJSONResponseWithStatus ¶
func WriteJSONResponseWithStatus(w http.ResponseWriter, status int, value any)
WriteJSONResponseWithStatus writes 'value' to an HTTP response encoded as JSON
Types ¶
type ActionError ¶
type ActionError struct {
// contains filtered or unexported fields
}
ActionError represents a basic user action error (e.g. a wrong parameter, non-existing record etc.)
func NewActionError ¶
func NewActionError(msg string, args ...any) ActionError
NewActionError creates an Action error from provided message using a newly defined general error as the original error
func NewActionErrorFrom ¶
func NewActionErrorFrom(err error) ActionError
func (ActionError) MarshalJSON ¶
func (me ActionError) MarshalJSON() ([]byte, error)
MarshalJSON serializes the error to JSON
type ErrorResponse ¶
type ErrorResponse struct { Error *ActionError `json:"error"` Details []string `json:"details"` Code int `json:"code"` }
ErrorResponse describes a wrapping object for all error HTTP responses