Documentation
¶
Index ¶
- func ErrorStorage() map[int]string
- func Init(errors map[int]string)
- type Error
- func (e *Error) Error() string
- func (e *Error) FormatMessage(args ...any)
- func (e *Error) GetCode() int
- func (e *Error) GetDetails() map[string]any
- func (e *Error) GetHTTPMessage() string
- func (e *Error) GetHTTPStatus() int
- func (e *Error) GetMessage() string
- func (e *Error) JSON() string
- func (e *Error) JSONOrigin() string
- func (e *Error) SetCode(code int)
- func (e *Error) SetDetail(name, data string)
- func (e *Error) SetHTTPStatus(code int)
- func (e *Error) SetMessage(msg string, format ...any)
- type ErrorHandler
- type ErrorOption
- type PropertySetter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Error ¶
type Error struct { Code int `json:"code"` Message string `json:"message"` Details map[string]any `json:"details"` // contains filtered or unexported fields }
Default type for error data
func (*Error) FormatMessage ¶
Formats the message field of the Error using fmt.Sprintf. It replaces any %s and %v specifiers in the message with the provided args.
func (*Error) GetDetails ¶ added in v1.1.6
Returns the details map of the Error.
func (*Error) GetHTTPMessage ¶
Returns the HTTP message associated with the error.
func (*Error) GetHTTPStatus ¶
Returns the HTTP status code associated with the error.
func (*Error) GetMessage ¶
Returns the message field of the Error.
func (*Error) SetDetail ¶
Adds a key-value pair to the Details map of the Error. This allows arbitrary additional context to be attached to the Error.
func (*Error) SetHTTPStatus ¶
Sets the HTTP status code and message for the error. The status text is looked up from the provided HTTP status code.
func (*Error) SetMessage ¶
Sets the message field of the Error, formatting it with fmt.Sprintf if format args are provided.
type ErrorHandler ¶
type ErrorHandler interface { JSON() string JSONOrigin() string Error() string GetHTTPStatus() int GetHTTPMessage() string GetCode() int GetMessage() string GetDetails() map[string]any }
ErrorHandler defines an interface for handling errors that can be converted to JSON. It includes methods to get the HTTP status, message, error code, error message, and convert the error to JSON.
func New ¶
func New(code int, options ...ErrorOption) ErrorHandler
Creates a new ErrorHandler with the given error code and options. The code parameter specifies the error code. If a message is registered for the code in ErrorStorage, it will be used as the default message. The options allow configuring additional details like the message, HTTP status, and custom key-value pairs. Returns the configured ErrorHandler.
type ErrorOption ¶
type ErrorOption func(PropertySetter)
ErrorOption is a function type that can be used to configure an Error. It accepts a PropertySetter func to set properties on the Error being constructed.
func Detail ¶
func Detail(name, data string) ErrorOption
Returns an ErrorOption that adds a key-value pair to the Details map of the Error being constructed. This allows arbitrary additional context to be attached to the Error.
func HTTPStatus ¶
func HTTPStatus(status int) ErrorOption
Returns an ErrorOption that sets the HTTPStatus field of the Error being constructed to the provided status code. This allows associating an HTTP status code with the error.
func Message ¶
func Message(message string, format ...any) ErrorOption
Returns an ErrorOption that sets the message field of the Error being constructed, formatting it with fmt.Sprintf if format args are provided.
func MessageArgs ¶
func MessageArgs(args ...any) ErrorOption
Returns an ErrorOption that formats the message field of the Error being constructed using fmt.Sprintf, replacing any %s and %v specifiers with the provided args.
type PropertySetter ¶
type PropertySetter interface { SetMessage(string, ...any) FormatMessage(...any) SetCode(int) SetDetail(name, data string) SetHTTPStatus(int) }
PropertySetter defines methods to set error properties like message, code, details, and HTTP status. This interface allows abstracting away the specific error implementation.