api

package
v1.2.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const ReqIDKey string = "requestID"

Variables

This section is empty.

Functions

func AsErrorResp

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

AsErrorResp checks whether the error is of type *ErrorResp.

func DefaultErrorHandler

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

Can be used in Wrap func as default one

func HTMLErrorHandler

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

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

Default func for json error handling.

func Wrap

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

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

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

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

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