api

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const Email string = "email"
View Source
const ReqIDKey string = "requestID"

Variables

This section is empty.

Functions

func AsErrorResp added in v0.3.1

func AsErrorResp(err error, target **ErrorResp) bool

AsErrorResp checks whether the error is of type *ErrorResp.

func DefaultErrorHandler added in v0.3.1

func DefaultErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, errResp *ErrorResp)

Can be used in Wrap func as default one

func HTMLErrorHandler added in v0.3.1

func HTMLErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, errResp *ErrorResp)

Default func for html error handling, with examplary html code.

func JSONErrorHandler added in v0.3.1

func JSONErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, errResp *ErrorResp)

Default func for json error handling.

func Wrap added in v0.3.1

A wrapper func for handling errors from the called handler funcs. Uses custom func which handles error response. Defaults to DefaultErrorHandler if nothing is passed.

Types

type ErrorHandlerFunc added in v0.3.1

type ErrorHandlerFunc func(context.Context, http.ResponseWriter, *http.Request, *ErrorResp)

Custom type of error handler func mostly used for custom transport logic.

type ErrorResp

type ErrorResp struct {
	Status      int    `json:"status"`      // HTTP status code
	MsgKey      string `json:"message_key"` // i18n key
	InternalMsg string `json:"-"`           // Server-side log message
}

ErrorResp is a simple error response struct used for API responses. `MsgKey` is a frontend-friendly message key for localization (i18n). `InternalMsg` is not serialized in JSON and is only used for logging.

func FromError added in v0.3.1

func FromError(err error) *ErrorResp

FromError attempts to convert a generic error into an *ErrorResp. Returns nil if the error is not of the correct type.

func NewClientErrorResp added in v0.4.1

func NewClientErrorResp(status int, msgKey string) *ErrorResp

NewClientErrorResp constructs an ErrorResp with only a client-facing key.

func NewErrorResp

func NewErrorResp(status int, msgKey, internalMsg string) *ErrorResp

NewErrorResp constructs a new ErrorResp with both client-facing key and internal message.

func (*ErrorResp) Error

func (e *ErrorResp) Error() string

Error implements the error interface and returns the MsgKey.

type HandlerFunc added in v0.3.1

type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request) *ErrorResp

A handler func which accepts context field and returns error response.

Jump to

Keyboard shortcuts

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