GoError is a simple and pluggable Go package that allows developers to create and handle errors in Go more efficiently and gracefully, while also preserving the context of every error.
- Plug and Play (compatible with existing go errors package)
- Allows you to specify error types
- Easy conversion of error into Map or JSON.
- Add context to error for passing the human-readable error message to client-side applications.
- Wraps and unwraps error stack.
Let's look at some code snippets. But before this, make sure to download the package using
go get -v github.com/dipaksaini98/goerror
and import the package as
import (
errors "github.com/dipaksaini98/goerror"
)
Syntax:
New(message string, error_type Type) error
Example:
err := errors.New("error message", nil)
fmt.Println(err)
>> Error message
Note: when you don't specify any error type NoType is set by default.
Example:
err := errors.New("error message", &errors.BadRequest)
fmt.Println(err)
>> Error message
Syntax:
SetContext(error_object error, key string, value interface{}) error
Example:
err := errors.New("programmer friendly error message", nil)
err := errors.SetContext(err, "user-email", "email is missing")
err := errors.New("programmer friendly error message", nil)
err := errors.SetContext(err, "email", map[string]string{"email_domain": "invalid email domain", "email_local_part": "invalid local part (must start with `a` alphabet)"})
Syntax:
GetContext(error_object error) map[string]interface{}
Example:
ctx := errors.GetContext(error)
Syntax:
SetType(error_object error, type *Type) error
Example:
err := errors.SetType(err, &errors.NotFound)
var customErrorType errors.Type = "CustomErrorType"
err := errors.SetType(err, &customErrorType)
1. NoType
2. BadRequest
3. NotFound
4. DBError
5. Unauthorized
6. PermissionDenied
7. SomethingWentWrong
Syntax:
GetType(error_object error) Type
Example:
errType := errors.GetType(err)
Example:
var CustomErrorType errors.Type = "CustomErrorType"
Syntax:
errors.Is(error_1 error, error_2 error) bool
Example:
err1 := errors.New("error1", nil)
err2 := errors.New("error2", nil)
isSame := errors.Is(err1, err2)
fmt.Println(isSame)
>>false
err3 := err1
isSame = errors.Is(err3, err1)
fmt.Println(isSame)
>>true
Syntax:
errors.As(error_1 error, error_2 error) bool
Example:
err1 := errors.New("error1", nil)
err2 := errors.New("error2", nil)
isSameType := errors.As(err1, err2)
fmt.Println(isSameType)
>>true
// Because of NoType (default)
err3 := errors.New("error2", &errors.BadRequest)
isSameType = errors.As(err3, err1)
fmt.Println(isSameType)
>>false
Syntax:
errors.Map(error_1 error) map[string]interface{}
Example:
err1 := errors.New("error1", nil)
errMap := errors.Map(err1)
fmt.Println(errMap)
>> map[message:error1 type:NoType]
err1 = errors.SetContext(err1, "email", "email not found")
errMap = errors.Map(err1)
fmt.Println(errMap)
>> map[field:email message:email not found type:NoType]
Syntax:
errors.Map(error_1 error) map[string]interface{}
Example:
err1 := errors.New("error1", nil)
errJSON := errors.JSON(err1)
fmt.Println(string(errJSON))
>> {"message":"error1","type":"NoType"}
err1 = errors.SetContext(err1, "email", "email not found")
errJSON = errors.JSON(err1)
fmt.Println(string(errJSON))
>> {"field": "email", "message": "email not found", "type": "NoType"}