hook

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorHookStatusCodeMinLogged = 500

Functions

func Error

func Error(w http.ResponseWriter, r *http.Request, err error, logger LogHook)

Error is the default error hook. It check the error and return the corresponding response to the client. logger parameter is optional (you can set it to nil)

func Log

func Log(_ http.ResponseWriter, r *http.Request, err error)

Log will log the error.

func Render

func Render(w http.ResponseWriter, _ *http.Request, response interface{}, statusCode int) error

Render is the default render hook. It marshals the output into JSON, or returns an empty body if the payload is nil.

func Validate

func Validate(ctx context.Context, input interface{}) error

Validate is the default validation hook. It use 'ozzo-validation' to validate structure. A structure must implement 'ValidatableWithContext' or 'Validatable'.

Types

type BindHook

type BindHook func(w http.ResponseWriter, r *http.Request, in interface{}) error

BindHook is the hook called by the wrapped http handler when binding an incoming request to the kcd handler's input object.

func Bind

func Bind(maxBodyBytes int64) BindHook

Bind returns a Bind hook, it will read only maxBodyBytes bytes from the body and unmarshall the input interface with the json encoding of the stdlib.

type ErrorHook

type ErrorHook func(w http.ResponseWriter, r *http.Request, err error, logger LogHook)

ErrorHook hook lets you interpret errors returned by your handlers. After analysis, the hook should return a suitable http status code and and error payload. This lets you deeply inspect custom error types.

type ErrorResponse

type ErrorResponse struct {
	ErrorDescription string      `json:"error_description"`
	Error            errors.Kind `json:"error"`

	Fields   map[string]string      `json:"fields,omitempty"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

ErrorResponse is the default response that send the default error hook.

type LogHook

type LogHook func(w http.ResponseWriter, r *http.Request, err error)

LogHook is the logger triggered after the error hook. It can show detailed error about a problem that you can't explain to users.

type RenderHook

type RenderHook func(w http.ResponseWriter, r *http.Request, response interface{}, defaultStatusCode int) error

RenderHook is the last hook called by the wrapped handler before returning. It takes the response, request, the success HTTP status code and the response payload as parameters.

Its role is to render the payload to the client to the proper format.

type ValidateHook

type ValidateHook func(ctx context.Context, input interface{}) error

ValidateHook is the hook called to validate the input. The default expected return (handled by the error hook) is a map[string]error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL