Skip to content

Rules to classify and decode application errors in Go

License

Notifications You must be signed in to change notification settings

iamrgon/errdecode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

errdecode godoc Build Status coverage

Package errdecode provides a way to represent rules for classifying error values returned by your application.

Example Usage

import "github.com/iamrgon/errdecode"

var ErrInvalidToken = errors.New("invalid token")
var ErrMissingEmail = errors.New("missing email")
var ErrMissingPassword = errors.New("missing password")

func myAppDomainFunc() error {
  // Do some work, then return error...
  return ErrInvalidToken
}

func main() {
  rules := []errdecode.Rule{
  	{
  		Code: 1001,
  		Message: "The provided token is not valid.",
  		Errors: []error{ErrInvalidToken},
  	},
  	{
  		Code: 1002,
  		Message: "An email and password is required.",
  		Errors: []error{ErrMissingEmail, ErrMissingPassword},
  	},
  }

	decoder := errdecode.New(rules)
	if err := myAppDomainFunc(); err != nil {
		log.Fatal(decoder.Translate(err))
	}
}

License

Apache

About

Rules to classify and decode application errors in Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages