errdef

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: Apache-2.0, MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OK                  = &ErrDef{Code: 0, Msg: "OK", HttpStatus: http.StatusOK}
	InternalServerError = &ErrDef{Code: 10001, Msg: "Internal server error.", HttpStatus: http.StatusForbidden}
	ErrBind             = &ErrDef{
		Code:       10002,
		Msg:        "Error occurred while binding the request body to the struct.",
		HttpStatus: http.StatusBadRequest,
	}
	ErrParams = &ErrDef{Code: 10003, Msg: "Error params.", HttpStatus: http.StatusBadRequest}

	ErrParse = &ErrDef{Code: 10004, Msg: "Error parse.", HttpStatus: http.StatusBadRequest}

	// ErrValidation database errors
	ErrValidation = &ErrDef{Code: 20001, Msg: "Validation failed.", HttpStatus: http.StatusUnauthorized}
	ErrDatabase   = &ErrDef{Code: 20002, Msg: "Database error.", HttpStatus: http.StatusForbidden}
	ErrToken      = &ErrDef{Code: 20003,
		Msg:        "Error occurred while signing the JSON web token.",
		HttpStatus: http.StatusUnauthorized,
	}

	// ErrEncrypt
	// user errors
	ErrEncrypt = &ErrDef{Code: 20101,
		Msg:        "Error occurred while encrypting the user password.",
		HttpStatus: http.StatusBadRequest,
	}
	ErrUserNotFound      = &ErrDef{Code: 20102, Msg: "The user was not found.", HttpStatus: http.StatusUnauthorized}
	ErrTokenInvalid      = &ErrDef{Code: 20103, Msg: "The token was invalid.", HttpStatus: http.StatusUnauthorized}
	ErrPasswordIncorrect = &ErrDef{Code: 20104, Msg: "The password was incorrect.", HttpStatus: http.StatusUnauthorized}
)

Common errors, each error must have HttpStatus!

Functions

func DecodeErr

func DecodeErr(err error) (int, string)

DecodeErr decode error

func IsEqualByCode

func IsEqualByCode(err error, code int) bool

IsEqualByCode asset error is code to use errdef.DecodeErr()

func IsErrUserNotFound

func IsErrUserNotFound(err error) bool

IsErrUserNotFound asset error is ErrUserNotFound to use errdef.DecodeErr()

Types

type Err

type Err struct {
	// Code
	// error code default 0 is OK
	Code int `json:"code"   validate:"required"  example:"0"`
	// Msg
	// error message, if OK is empty
	Msg string `json:"msg"  validate:"optional"  example:"msg of error code 0 is empty"`
	Err error  `json:"-"`
}

Err represents an error only show in log, but not to client

func New

func New(e *ErrDef, err error) *Err

New new error for use e errdef.ErrDef in $project/pkg/errdef/ file e.go, you can add more! err error can use fmt.Errorf() to create use like errdef.New(errdef.InternalServerError, fmt.Errorf("server error, err: %v", err)) and you can add message errdef.New(errdef.InternalServerError, fmt.Errorf("server error, err: %v", err)).Add("client can know error")

func NewErr

func NewErr(e *ErrDef) *Err

NewErr new error for use e errdef.ErrDef in $project/pkg/errdef/ file e.go, you can add more! use like errdef.NewErr(errdef.ErrParams) and you can add message

errdef.NewErr(errdef.ErrParams).Add("params id not found")

func (*Err) Add

func (err *Err) Add(message string) error

Add to errdef.New().add("user message")

func (*Err) Addf

func (err *Err) Addf(format string, args ...interface{}) error

Addf to errdef.New().addf("user message %v", args)

func (*Err) Error

func (err *Err) Error() string

Error errdef.New().Error() to print error message

type ErrDef

type ErrDef struct {
	// Code
	// error code default 0 is OK
	Code int `json:"code"   validate:"required"  example:"0"`
	// Msg
	// error message, if OK is empty
	Msg string `json:"msg"  validate:"optional"  example:"msg of error code 0 is empty"`
	// HttpStatus
	// http status code, this is set by config
	HttpStatus int `json:"-"`
}

func (*ErrDef) Error

func (err *ErrDef) Error() string

Jump to

Keyboard shortcuts

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