Documentation
¶
Index ¶
- Constants
- func AsErrorResp(err error, target **ErrorResp) bool
- func DefaultErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, ...)
- func HTMLErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, ...)
- func JSONErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, ...)
- func Wrap(h HandlerFunc, onErr ErrorHandlerFunc) http.HandlerFunc
- type ErrorHandlerFunc
- type ErrorResp
- type HandlerFunc
Constants ¶
const Email string = "email"
const ReqIDKey string = "requestID"
Variables ¶
This section is empty.
Functions ¶
func AsErrorResp ¶ added in v0.3.1
AsErrorResp checks whether the error is of type *ErrorResp.
func DefaultErrorHandler ¶ added in v0.3.1
func DefaultErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, errResp *ErrorResp)
Can be used in Wrap func as default one
func HTMLErrorHandler ¶ added in v0.3.1
func HTMLErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, errResp *ErrorResp)
Default func for html error handling, with examplary html code.
func JSONErrorHandler ¶ added in v0.3.1
func JSONErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, errResp *ErrorResp)
Default func for json error handling.
func Wrap ¶ added in v0.3.1
func Wrap(h HandlerFunc, onErr ErrorHandlerFunc) http.HandlerFunc
A wrapper func for handling errors from the called handler funcs. Uses custom func which handles error response. Defaults to DefaultErrorHandler if nothing is passed.
Types ¶
type ErrorHandlerFunc ¶ added in v0.3.1
Custom type of error handler func mostly used for custom transport logic.
type ErrorResp ¶
type ErrorResp struct {
Status int `json:"status"` // HTTP status code
MsgKey string `json:"message_key"` // i18n key
InternalMsg string `json:"-"` // Server-side log message
}
ErrorResp is a simple error response struct used for API responses. `MsgKey` is a frontend-friendly message key for localization (i18n). `InternalMsg` is not serialized in JSON and is only used for logging.
func FromError ¶ added in v0.3.1
FromError attempts to convert a generic error into an *ErrorResp. Returns nil if the error is not of the correct type.
func NewClientErrorResp ¶ added in v0.4.1
NewClientErrorResp constructs an ErrorResp with only a client-facing key.
func NewErrorResp ¶
NewErrorResp constructs a new ErrorResp with both client-facing key and internal message.
type HandlerFunc ¶ added in v0.3.1
A handler func which accepts context field and returns error response.