apiutil

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrCodeInternal is returned when an internal error happens.
	ErrCodeInternal = errors.Code("INTERNAL_ERROR")

	// ErrCodeBadRequest is returned when an error happens due to request data.
	ErrCodeBadRequest = errors.Code("BAD_REQUEST")

	// ErrCodeTimeout is returned then the request returns an error due to a timeout.
	ErrCodeTimeout = errors.Code("REQUEST_TIMEOUT")
)

Variables

This section is empty.

Functions

func EncodeJSONResponse

func EncodeJSONResponse(_ context.Context, w http.ResponseWriter, response interface{}) error

EncodeJSONResponse encodes an endpoint response into json

func GetDefaultErrorHTTPStatusCode

func GetDefaultErrorHTTPStatusCode(err error) (s int)

GetDefaultErrorHTTPStatusCode returns an HTTP status code base on an error.

This is a default implementation that sets the satus code base on the error severity. nolint:gocritic

func NewHTTPErrorJSONEncoder

func NewHTTPErrorJSONEncoder(
	getHTTPStatus GetHTTPStatusFunc,
	parseErrorFunc ParseErrorFunc,
) func(context.Context, error, http.ResponseWriter)

NewHTTPErrorJSONEncoder returns a new

Types

type BaseEndpointResponse

type BaseEndpointResponse struct {
	// Example: "51e8cb62dbc57774a7720ef828e96c34"
	TraceID   trace.ID    `json:"trace_id,omitempty"` // [Legacy] Already exists in Trace.
	Trace     trace.Trace `json:"trace,omitempty"`
	RequestID request.ID  `json:"request_id,omitempty"`
}

BaseEndpointResponse should be used to extend responses from endpoint

func CreateBaseEndpointResponse

func CreateBaseEndpointResponse(ctx context.Context) BaseEndpointResponse

CreateBaseEndpointResponse creates and returns a struct of BaseEndpointResponse filled with data from context and error.

type ErrorDescription

type ErrorDescription struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

ErrorDescription represents the detailed returned error in all APIs

func ParseError

func ParseError(err error) ErrorDescription

ParseError parses @err in a ErrorDescription

type GetHTTPStatusFunc

type GetHTTPStatusFunc func(error) int

GetHTTPStatusFunc is a type of function that should take an error and return an HTTP status code.

type ParseErrorFunc

type ParseErrorFunc func(context.Context, error) interface{}

ParseErrorFunc is a type of function that should take an error parses in a struct.

Jump to

Keyboard shortcuts

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