Documentation ¶
Index ¶
- Constants
- Variables
- func ErrorHandler(w http.ResponseWriter, r *http.Request, err error)
- func GetID(ctx context.Context) int64
- func GetOp(ctx context.Context) string
- func JSONWrite(w http.ResponseWriter, code int, obj interface{}) error
- func SetID(ctx context.Context, id int64) context.Context
- func SetOp(ctx context.Context, op string) context.Context
- type AuthError
- type ContextKey
- type ErrorHandlerFunc
Constants ¶
const ( AuthError("Unauthorized") // ErrForbidden represents failure when authorizing a request. ErrForbidden = AuthError("Forbidden") // ErrBasicAuthenticate is designed to trigger an HTTP Basic Auth challenge. ErrBasicAuthenticate = AuthError("ErrWWWAuthenticate") )ErrUnauthorized =
const InternalErrorFormat = "Internal Server Error: %d"
InternalErrorFormat is the default error message returned for unhandled errors.
Variables ¶
var ( // ContentType header value. ContentType = "Content-Type" // ApplicationJSON content-type. ApplicationJSON = "application/json" // TextHTML content-type. TextHTML = "text/html" )
Functions ¶
func ErrorHandler ¶
func ErrorHandler(w http.ResponseWriter, r *http.Request, err error)
ErrorHandler provides some standard handling for errors in an http request flow.
Maps: json.SyntaxError to "BadRequest", body is the JSON string of the error message. validation.Errors to "BadRequest", body is the JSON of the error object (map of field name to list of errors). AuthError to "Forbidden" or "Unauthorized" as defined by the err instance. In addition ErrBasicAuthenticate issues a basic auth challenge using default realm of "Restricted". To override handle in your custom error handlers instead.
Unhandled errors are added to the ctx and return "Internal Server Error" with the request ID to aid with troubleshooting.
func JSONWrite ¶
func JSONWrite(w http.ResponseWriter, code int, obj interface{}) error
JSONWrite is a simple helper utility to return the json encoded obj with appropriate content-type and code.
Types ¶
type AuthError ¶
type AuthError string
AuthError encompasses Authentication and Authorization errors.
type ContextKey ¶
type ContextKey string
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
ErrorHandlerFunc is useful to standardize the exception management of requests.