Documentation
¶
Overview ¶
Example (Get) ¶
Wrapping a GET http handler.
zl := zerolog.New(io.Discard).With()
getter := func(ctx context.Context, key string) (string, *ErrorResponse) {
if key == "id" {
return "1", nil
}
return "", MissingParamError{Name: key}.ToErrorResponse()
}
getHandler := func(nupg NamedURLParamsGetter) TypedHandler {
return func(r *http.Request) (*Response, *ErrorResponse) {
idParam, errR := nupg(r.Context(), "id")
if errR != nil {
return nil, errR
}
id, err := strconv.ParseInt(idParam, 10, 64)
if err != nil {
return nil, ParsingParamError{
Name: "id",
Value: idParam,
}.ToErrorResponse()
}
return &Response{
Body: id,
HTTPStatusCode: http.StatusOK,
}, nil
}
}
Wrapper(zl.Logger(), getHandler(getter)).ServeHTTP(nil, nil)
Example (Post) ¶
Wrapping a POST http handler.
zl := zerolog.New(io.Discard).With()
type postRequest struct {
Name string `json:"name"`
}
createHandler := func() TypedHandler {
return func(r *http.Request) (*Response, *ErrorResponse) {
var pr postRequest
if err := BindBody(r, &pr); err != nil {
return nil, err
}
log.Println(pr)
return &Response{
Body: pr,
HTTPStatusCode: http.StatusCreated,
}, nil
}
}
Wrapper(zl.Logger(), createHandler()).ServeHTTP(nil, nil)
Index ¶
Examples ¶
Constants ¶
const (
ErrCodeParsingBody = "error_parsing_body"
)
Variables ¶
This section is empty.
Functions ¶
func Wrapper ¶
func Wrapper( log zerolog.Logger, f TypedHandler, ) http.HandlerFunc
Wrapper will actually do the boring work of logging an error and render the response.
Types ¶
type ErrorResponse ¶
type ErrorResponse struct {
Error error `json:"-"`
HTTPStatusCode int `json:"-"`
ErrorCode string `json:"error_code"`
ErrorMsg string `json:"error_msg"`
}
ErrorResponse is a wrapper for the error response body to have a clean way of displaying errors.
func BindBody ¶
func BindBody(r *http.Request, target interface{}) *ErrorResponse
BindBody will bind the body of the request to the given interface.
func NewErrorResponse ¶
func NewErrorResponse( e error, hsc int, ec string, msg string, ) *ErrorResponse
NewErrorResponse creates a new ErrorResponse.
func (*ErrorResponse) IsEqual ¶
func (her *ErrorResponse) IsEqual(e1 *ErrorResponse) bool
IsEqual checks if an error response is equal to another.
type InternalServerError ¶
type InternalServerError struct {
Err error
}
InternalServerError is an error that is returned when an internal server error occurs.
func (InternalServerError) Error ¶
func (e InternalServerError) Error() string
func (InternalServerError) ToErrorResponse ¶
func (e InternalServerError) ToErrorResponse() *ErrorResponse
type MissingParamError ¶
type MissingParamError struct {
Name string
}
MissingParamError is the error that is returned when a named URL param is missing.
func (MissingParamError) Error ¶
func (e MissingParamError) Error() string
func (MissingParamError) ToErrorResponse ¶
func (e MissingParamError) ToErrorResponse() *ErrorResponse
type NamedURLParamsGetter ¶
type NamedURLParamsGetter func(ctx context.Context, key string) (string, *ErrorResponse)
NamedURLParamsGetter is the interface that is used to parse the URL parameters.
type NotFoundError ¶
type NotFoundError struct {
Designation string
}
NotFoundError is an error that is returned when a resource is not found.
func (NotFoundError) Error ¶
func (e NotFoundError) Error() string
func (NotFoundError) ToErrorResponse ¶
func (e NotFoundError) ToErrorResponse() *ErrorResponse
type ParsingParamError ¶
ParsingParamError is the error that is returned when a named URL param is invalid.
func (ParsingParamError) Error ¶
func (e ParsingParamError) Error() string
func (ParsingParamError) ToErrorResponse ¶
func (e ParsingParamError) ToErrorResponse() *ErrorResponse
type TypedHandler ¶
type TypedHandler func(r *http.Request) (*Response, *ErrorResponse)
TypedHandler is the handler that you are actually handling the response.