Documentation ¶
Overview ¶
Package errors provide an error type with code.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type E ¶
type E struct { Message string `json:"message,omitempty"` Name string `json:"name,omitempty"` Code int `json:"code,omitempty"` // contains filtered or unexported fields }
E define custom error that wrap underlying error with custom code, message, and name.
The Code field is required, used to communicate the HTTP response code. The Message field is optional, it's used to communicate the actual error message from server, to be readable by human. The Name field is optional, intended to be consumed by program, for example, to provide a key as translation of Message into user's locale defined language.
func InvalidInput ¶
InvalidInput generate an error for invalid input.
func (*E) Is ¶ added in v0.50.0
Is return true if the target error is instance of *E and the value of field Code and Name match with values in e.
Example ¶
package main import ( "encoding/json" "errors" "fmt" "log" liberrors "github.com/shuLhan/share/lib/errors" ) func main() { var ( errFileNotFound = &liberrors.E{ Code: 400, Name: `ERR_NOT_FOUND`, Message: `file not found`, } errResNotFound = &liberrors.E{ Code: 404, Name: `ERR_NOT_FOUND`, Message: `resource not found`, } rawJson = `{"code":400,"name":"ERR_NOT_FOUND","message":"file not found"}` e *liberrors.E err error ) err = json.Unmarshal([]byte(rawJson), &e) if err != nil { log.Fatal(err) } var gotErr error = e fmt.Println(errors.Is(gotErr, errFileNotFound)) fmt.Println(errors.Is(gotErr, errResNotFound)) }
Output: true false