errorx

package
v0.0.0-...-dfe2c77 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OK = &ErrorX{Code: http.StatusOK, Msg: ""}

	ErrInternal = &ErrorX{Code: http.StatusInternalServerError, Reason: "InternalError", Msg: "Internal server error."}

	ErrNotFound = &ErrorX{Code: http.StatusNotFound, Reason: "NotFound", Msg: "NotFound."}

	ErrBind = &ErrorX{Code: http.StatusBadRequest, Reason: "BindError", Msg: "Error occurred while binding the request body to the struct."}

	ErrInvalidArgument = &ErrorX{Code: http.StatusBadRequest, Reason: "InvalidArgument", Msg: "InvalidArgument."}

	ErrUnauthenticated = &ErrorX{Code: http.StatusUnauthorized, Reason: "Unauthenticated", Msg: "Unauthenticated."}

	ErrPermissionDenied = &ErrorX{Code: http.StatusForbidden, Reason: "PermissionDenied", Msg: "Permission denied."}

	ErrOperationFailed = &ErrorX{Code: http.StatusConflict, Reason: "OperationFailed", Msg: "OperationFailed."}
)

define error codes for the application

Functions

func As

func As(err error, target interface{}) bool

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.

func Code

func Code(err error) int

func Is

func Is(err, target error) bool

Is reports whether any error in err's chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

func Reason

func Reason(err error) string

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

Types

type ErrorX

type ErrorX struct {
	Code     int               `json:"code,omitempty"`
	Msg      string            `json:"msg,omitempty"`
	Reason   string            `json:"reason,omitempty"`
	MetaData map[string]string `json:"meta_data,omitempty"`
}

ErrorX defines a standardized error structure for API responses

func FromError

func FromError(err error) *ErrorX

func New

func New(code int, reason, format string, args ...any) *ErrorX

func (*ErrorX) Error

func (e *ErrorX) Error() string

func (*ErrorX) GRPCStatus

func (e *ErrorX) GRPCStatus() *status.Status

func (*ErrorX) Is

func (err *ErrorX) Is(target error) bool

func (*ErrorX) KV

func (e *ErrorX) KV(kvs ...string) *ErrorX

func (*ErrorX) WithMessage

func (e *ErrorX) WithMessage(format string, args ...any) *ErrorX

func (*ErrorX) WithMetaData

func (e *ErrorX) WithMetaData(md map[string]string) *ErrorX

func (*ErrorX) WithRequestID

func (e *ErrorX) WithRequestID(requestID string) *ErrorX

Jump to

Keyboard shortcuts

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