Documentation
¶
Index ¶
- func AddRequestMetadataMiddleware(next http.Handler) http.Handler
- func LoggingMiddleware(next http.Handler) http.Handler
- func MakeHandler(handler HandlerFunc) func(http.ResponseWriter, *http.Request)
- func NewErrorFromDBErrors(prefix string, dbErrors []error) error
- func NewHandlerError(code int, err error) error
- func NewHandlerErrorWithCustomClientMessage(code int, err error, clientMessage string) error
- func NewInternalServerHandlerError(err error) error
- func RecoveryMiddleware(h http.Handler) http.Handler
- func SendErr(ctx *ServerContext, w http.ResponseWriter, err error)
- func SendJSONResponse(w http.ResponseWriter, response interface{})
- func SetJSONMiddleware(h http.Handler) http.Handler
- type ClientError
- type HandlerError
- type HandlerFunc
- type ServerContext
- func (ctx *ServerContext) Criticalf(format string, params ...interface{})
- func (ctx *ServerContext) Debugf(format string, params ...interface{})
- func (ctx *ServerContext) Errorf(format string, params ...interface{})
- func (ctx *ServerContext) Infof(format string, params ...interface{})
- func (ctx *ServerContext) SetRequestID(requestID uint64) context.Context
- func (ctx *ServerContext) Tracef(format string, params ...interface{})
- func (ctx *ServerContext) Warnf(format string, params ...interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddRequestMetadataMiddleware ¶
AddRequestMetadataMiddleware is a middleware that adds some metadata to the context of every request.
func LoggingMiddleware ¶
LoggingMiddleware is a middleware that writes logs for every request.
func MakeHandler ¶
func MakeHandler(handler HandlerFunc) func(http.ResponseWriter, *http.Request)
MakeHandler is a wrapper function that takes a handler in the form of HandlerFunc and returns a function that can be used as a handler in mux.Router.HandleFunc.
func NewErrorFromDBErrors ¶
NewErrorFromDBErrors takes a slice of database errors and a prefix, and returns an error with all of the database errors formatted to one string with the given prefix
func NewHandlerError ¶
NewHandlerError returns a HandlerError with the given code and message.
func NewHandlerErrorWithCustomClientMessage ¶
NewHandlerErrorWithCustomClientMessage returns a HandlerError with the given code, message and client error message.
func NewInternalServerHandlerError ¶
NewInternalServerHandlerError returns a HandlerError with the given message, and the http.StatusInternalServerError status text as client message.
func RecoveryMiddleware ¶
RecoveryMiddleware is a middleware that recovers from panics, log it, and sends Internal Server Error to the client.
func SendErr ¶
func SendErr(ctx *ServerContext, w http.ResponseWriter, err error)
SendErr takes a HandlerError and create a ClientError out of it that is sent to the http client.
func SendJSONResponse ¶
func SendJSONResponse(w http.ResponseWriter, response interface{})
SendJSONResponse encodes the given response to JSON format and sends it to the client
Types ¶
type ClientError ¶
type ClientError struct { ErrorCode int `json:"errorCode"` ErrorMessage string `json:"errorMessage"` }
ClientError is the http response that is sent to the client in case of an error.
func (*ClientError) Error ¶
func (err *ClientError) Error() string
type HandlerError ¶
HandlerError is an error returned from a rest route handler or a middleware.
func (*HandlerError) Error ¶
func (hErr *HandlerError) Error() string
type HandlerFunc ¶
type HandlerFunc func(ctx *ServerContext, r *http.Request, routeParams map[string]string, queryParams map[string]string, requestBody []byte) ( interface{}, error)
HandlerFunc is a handler function that is passed to the MakeHandler wrapper and gets the relevant request fields from it.
type ServerContext ¶
ServerContext is a context.Context wrapper that enables custom logs with request ID.
func ToServerContext ¶
func ToServerContext(ctx context.Context) *ServerContext
ToServerContext takes a context.Context instance and converts it to *ServerContext.
func (*ServerContext) Criticalf ¶
func (ctx *ServerContext) Criticalf(format string, params ...interface{})
Criticalf writes a customized formatted context related log with log level 'Critical'.
func (*ServerContext) Debugf ¶
func (ctx *ServerContext) Debugf(format string, params ...interface{})
Debugf writes a customized formatted context related log with log level 'Debug'.
func (*ServerContext) Errorf ¶
func (ctx *ServerContext) Errorf(format string, params ...interface{})
Errorf writes a customized formatted context related log with log level 'Error'.
func (*ServerContext) Infof ¶
func (ctx *ServerContext) Infof(format string, params ...interface{})
Infof writes a customized formatted context related log with log level 'Info'.
func (*ServerContext) SetRequestID ¶
func (ctx *ServerContext) SetRequestID(requestID uint64) context.Context
SetRequestID associates a request ID for the context.
func (*ServerContext) Tracef ¶
func (ctx *ServerContext) Tracef(format string, params ...interface{})
Tracef writes a customized formatted context related log with log level 'Trace'.
func (*ServerContext) Warnf ¶
func (ctx *ServerContext) Warnf(format string, params ...interface{})
Warnf writes a customized formatted context related log with log level 'Warn'.