handler

package
v1.34.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogErrorStacktrace

func LogErrorStacktrace(err error)

func ReportToSentry added in v1.31.0

func ReportToSentry(c *echo.Context, err error)

ReportToSentry reports err to the Sentry hub the sentryecho middleware put on c, and does nothing without one. It has the signature rest.WithErrorReporter takes, so an app that reports to Sentry passes it there. Only errors worth a person's attention are reported, classified with ez.ErrorCode as ManageError does: EINTERNAL (also a plain error or a recovered panic) at error level, ETIMEOUT or EUNAVAILABLE (usually a vendor outage; also a raw context.DeadlineExceeded) at warning level. Everything else is skipped: client errors, ECANCELED (also a raw context.Canceled, the client gave up), and an error that is not an ez error but carries an Echo status (404, 405, 413, 415, *echo.HTTPError). The error is an ez error when it is an *ez.Error itself, or when an *ez.Error is in its chain and the chain carries no Echo status, as rest.ErrorHandler has it; so an Echo error wrapping an ez error, as c.Bind or middleware.KeyAuth make, is skipped. The event carries the id of the request Request returns, which the server made and ManageError writes in its answer, or without a request the X-Request-Id of the response, the same id under rest.Configure; and the user the request's context holds (see Request).

func Request added in v1.31.0

func Request(c *echo.Context) requests.Request

Request returns the request of this HTTP request, as the api registry and ManageError store it with SetRequest, or nil when nothing stored one (a route that never reached either, an unknown route, a panic before the handler). Echo middleware reads it after next(c), and an error translator or reporter of rest.ErrorHandler reads it to localize or describe an error.

func SetRequest added in v1.31.0

func SetRequest(c *echo.Context, request requests.Request)

SetRequest stores request as the request of this HTTP request, for Request. The api registry calls it as soon as it builds the request, and ManageError does for plain routes.

Types

type App

type App interface {
	HandleRequest(requests requests.Request) (interface{}, error)
}

type BaseHandler

type BaseHandler struct {
	App App

	// ErrorTranslator, when set, is applied to every error right before it is
	// written to the HTTP response, so applications can localize error messages.
	// Only the translated error's message is used; the response code and HTTP
	// status always come from the original error. Returning nil keeps the
	// original error untouched.
	ErrorTranslator func(err error, request requests.Request) error
}

BaseHandler is a struct with basic methods that should be extended to properly handle a HTTP Service.

func NewHandler

func NewHandler(App App) *BaseHandler

func (*BaseHandler) BindedJSONResponse

func (h *BaseHandler) BindedJSONResponse(c *echo.Context, op string, request requests.Request, body requests.Body) error

func (*BaseHandler) BindedXMLResponse

func (h *BaseHandler) BindedXMLResponse(c *echo.Context, op string, request requests.Request, body requests.Body) error

func (*BaseHandler) BlobResponse

func (h *BaseHandler) BlobResponse(c *echo.Context, op string, request requests.Request, contentType string, body requests.Body) error

func (*BaseHandler) GetListLimit

func (h *BaseHandler) GetListLimit(c *echo.Context, defaultLimit int) int

func (*BaseHandler) GetListOffset

func (h *BaseHandler) GetListOffset(c *echo.Context, defaultOffset int) int

func (*BaseHandler) GetNumericQueryParam

func (h *BaseHandler) GetNumericQueryParam(c *echo.Context, param string, defaultValue int) int

func (*BaseHandler) GetParameterInt64

func (h *BaseHandler) GetParameterInt64(c *echo.Context, name string) (int64, error)

func (*BaseHandler) GetParameterString

func (h *BaseHandler) GetParameterString(c *echo.Context, name string) (string, error)

Parameters

func (*BaseHandler) GetParameterUUID

func (h *BaseHandler) GetParameterUUID(c *echo.Context, name string) (uuid.UUID, error)

func (*BaseHandler) GetQueryParamBool added in v1.23.0

func (h *BaseHandler) GetQueryParamBool(c *echo.Context, key string) (bool, error)

GetQueryParamBool parses an optional boolean query param with strconv.ParseBool. It returns false when the param is absent or empty.

func (*BaseHandler) GetQueryParamInt64

func (h *BaseHandler) GetQueryParamInt64(c *echo.Context, value string) (int64, error)

func (*BaseHandler) GetQueryParamInt64s

func (h *BaseHandler) GetQueryParamInt64s(c *echo.Context, key string) ([]int64, error)

func (*BaseHandler) GetQueryParamTime added in v1.5.3

func (h *BaseHandler) GetQueryParamTime(c *echo.Context, key string) (time.Time, error)

GetQueryParamTime parses a required query param as RFC3339/RFC3339Nano

func (*BaseHandler) GetQueryParams added in v1.23.0

func (h *BaseHandler) GetQueryParams(c *echo.Context, key string) []string

GetQueryParams returns every value of a query param that may be repeated (?status=a&status=b) or comma separated (?status=a,b), trimmed, without empties. It returns nil when the param is absent or holds only empty values.

func (*BaseHandler) JSONResponse

func (h *BaseHandler) JSONResponse(c *echo.Context, op string, request requests.Request, body requests.Body) error

func (*BaseHandler) ManageError

func (h *BaseHandler) ManageError(c *echo.Context, op string, request requests.Request, err error) error

ManageError translates an error into the appropriate HTTP error code

type ErrorResponse

type ErrorResponse struct {
	Error StandardError `json:"error"`
}

ErrorResponse defines the structure of the error that the handler will return

type FieldError added in v1.33.0

type FieldError struct {
	In      string `json:"in" doc:"Where the field is in the request: path, query, header or body."`
	Name    string `` /* 217-byte string literal not displayed */
	Message string `json:"message" doc:"Why the field failed. It never contains the submitted value."`
}

FieldError is one request field that failed validation, as an error answer lists it under fields. It is validate.FieldError on the wire.

type StandardError

type StandardError struct {
	Code      string `json:"code"`
	Message   string `json:"message"`
	RequestID string `json:"request_id"`
	// Fields lists the request fields that failed validation, at most
	// validate.MaxFields of them. Only rest.ErrorHandler sets it, when the
	// error it answers is an ez EINVALID built by validate.NewFieldsError:
	// the api registry's for a request that failed its validation, or an
	// app's own. The key is absent otherwise.
	Fields []FieldError `` /* 186-byte string literal not displayed */
}

StandardError defines a error in JSON format

Jump to

Keyboard shortcuts

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