Documentation ¶
Index ¶
Constants ¶
const ( // CTXKeyLogger is the key under which the request scoped logger is placed CTXKeyLogger ctxKey = iota )
Variables ¶
var ( ErrForbidden = errors.New("access is forbidden") )
These are the errors that can be used in your controllers and matched against in the Errors middleware to perform special actions.
Functions ¶
func Logger ¶
Logger returns the Request ID scoped logger from the request Context and panics if it cannot be found. This function is only ever used by your controllers if your app uses the RequestID middlewares, otherwise you should use the controller's receiver logger directly.
Types ¶
type AppHandler ¶
type AppHandler func(w http.ResponseWriter, r *http.Request) error
AppHandler is the function signature for controllers that return errors.
type ErrorContainer ¶
type ErrorContainer struct { // The error that will be returned by the controller Err error // Optional override layout string ErrLayout string // The template file top render (e.g "errors/500") Template string // HTTP status code to respond with Code int // A custom handler to perform additional operations on this error Handler ErrorHandler }
ErrorContainer holds all of the relevant variables for a users custom error
func NewError ¶
func NewError(err error, code int, errorLayout, template string, handler ErrorHandler) ErrorContainer
NewError creates a new ErrorContainer that can be added to an errorManager. If you provide a handler here (instead of nil) then the Errors middleware will use your handler opposed to taking the default route of logging and rendering. You must handle logging and rendering yourself.
type ErrorHandler ¶
type ErrorHandler func(w http.ResponseWriter, r *http.Request, e ErrorContainer, render abcrender.Renderer) error
ErrorHandler is the function signature for user supplied error handlers.
type ErrorManager ¶
type ErrorManager struct {
// contains filtered or unexported fields
}
ErrorManager helps manage errors at the application level
func NewErrorManager ¶
func NewErrorManager(render abcrender.Renderer, errorLayout string) *ErrorManager
NewErrorManager creates an error manager that can be used to create an error handler to wrap your controllers with
func (*ErrorManager) Add ¶
func (m *ErrorManager) Add(e ErrorContainer)
Add a new ErrorContainer to the error manager
func (*ErrorManager) Errors ¶
func (m *ErrorManager) Errors(ctrl AppHandler) http.HandlerFunc
Errors is a middleware to handle controller errors and error page rendering. The benefit of using this middleware opposed to logging and rendering errors directly in your controller is that it's all centralized to one location which simplifies adding notifiers (like slack and email). It also reduces a lot of controller boilerplate.
func (*ErrorManager) Remove ¶
func (m *ErrorManager) Remove(e ErrorContainer)
Remove a ErrorContainer from the error manager
type MW ¶
MW is an interface defining middleware wrapping
func ZapRecover ¶
func ZapRecover(fallback *zap.Logger, errorHandler http.HandlerFunc) MW
ZapRecover will attempt to log a panic, as well as produce a reasonable error for the client by calling the passed in errorHandler function.
It uses the zap logger and attempts to look up a request-scoped logger created with this package before using the passed in logger.
The zap logger that's used here should be careful to enable stacktrace logging for any levels that they require it for.
func ZapRequestIDLogger ¶
ZapRequestIDLogger returns a request id logger middleware. This only works if chi has inserted a request id into the stack first.