hes

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2024 License: Apache-2.0 Imports: 7 Imported by: 47

README

hes

Build Status

Create a http error

API

HTTP Error

err := errors.New("abcd")
he := &Error{
  StatusCode: 500,
  Code: "cus-validate-fail",
  Category: "comon",
  Message: err.Error(),
  Err: err,
  Exception: true,
  Extra: map[string]interface{}{
    "url": "http:///127.0.0.1/users/me",
  },
}
he := New("error message")
he := NewWithCaller("error message")
Error

Get the description of http error

he := &Error{
  Message: "error message",
  Code: "cus-validate-fail",
  Category: "common",
}
// category=common, code=cus-validate-fail, message=error message
fmt.Println(he.Error())
Format

Error format

he := &Error{
  Message: "error message",
  Code: "cus-validate-fail",
  Category: "common",
}
SetCaller

Set the caller of error

he := &Error{
  Message: "error message",
  Code: "cus-validate-fail",
  Category: "common",
}
he.SetCaller(1)
ToJSON

Error to json

he := &Error{
  Message: "error message",
  Code: "cus-validate-fail",
  Category: "common",
}
he.ToJSON()

EnableCaller

Enable or disable to get caller by default

EnableCaller(true);
EnableCaller(false);

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnableCaller

func EnableCaller(enabled bool)

EnableCaller enable caller

func IsError

func IsError(err error) bool

IsError check the error whether or not hes error

func SetFileConvertor

func SetFileConvertor(fn FileConvertor)

SetFileConvertor set file convertor

Types

type Error

type Error struct {

	// http status code
	StatusCode int `json:"statusCode,omitempty"`
	// error code
	Code string `json:"code,omitempty"`
	// category
	Category string `json:"category,omitempty"`
	// sub category
	SubCategory string `json:"subCategory,omitempty"`
	// title
	Title string `json:"title,omitempty"`
	// message
	Message string `json:"message,omitempty"`
	// exception error
	Exception bool `json:"exception,omitempty"`
	// original error
	Err error `json:"-"`
	// File caller file
	File string `json:"file,omitempty"`
	// Line caller line
	Line int `json:"line,omitempty"`
	// extra info for error
	Extra map[string]interface{} `json:"extra,omitempty"`
	// sub errors
	Errs []*Error `json:"errs,omitempty"`
	// contains filtered or unexported fields
}

Error http error

func New

func New(message string, category ...string) *Error

New create a http error

func NewMutex added in v0.5.0

func NewMutex(message string, category ...string) *Error

NewMutex create a http error with mutex

func NewWithCaller

func NewWithCaller(message string) *Error

NewWithCaller create a http error with caller

func NewWithError

func NewWithError(err error) *Error

NewWithError create a http error with error

func NewWithErrorStatusCode

func NewWithErrorStatusCode(err error, statusCode int) *Error

NewWithErrorStatusCode create a http error with error and status code

func NewWithException added in v0.5.1

func NewWithException(message string) *Error

NewWithException create a http error and set exception to true

func NewWithStatusCode

func NewWithStatusCode(message string, statusCode int, category ...string) *Error

NewWithStatusCode create a http error with status code

func Wrap

func Wrap(err error) *Error

Wrap wrap error

func (*Error) Add

func (e *Error) Add(errs ...error)

Add add error to error list

func (*Error) AddExtra added in v0.4.1

func (e *Error) AddExtra(key string, value interface{})

AddExtra add extra value to error

func (*Error) Clone

func (e *Error) Clone() *Error

Clone clone error

func (*Error) CloneWithMessage

func (e *Error) CloneWithMessage(message string) *Error

CloneWithMessage clone error and update message

func (*Error) Error

func (e *Error) Error() string

Error error interface

func (*Error) Format

func (e *Error) Format(s fmt.State, verb rune)

Format error format

func (*Error) IsEmpty

func (e *Error) IsEmpty() bool

IsEmpty check the error list is empty

func (*Error) IsNotEmpty

func (e *Error) IsNotEmpty() bool

IsNotEmpty check the error list is not empty

func (*Error) SetCaller

func (e *Error) SetCaller(skip int)

SetCaller set info of caller

func (*Error) ToJSON

func (e *Error) ToJSON() []byte

ToJSON error to json

type FileConvertor

type FileConvertor func(file string) string

Jump to

Keyboard shortcuts

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