apperror

package module
v0.0.0-...-e9378b7 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2015 License: MIT Imports: 2 Imported by: 56

README

go-apperror

Go error implementation for applications that supports nested errors.

Go s error implementation is rather simplistic, and leaves a lot to be desired for applications that require more complex errors.

This package provides an Error interface and and an apperror.Err implementation to provide rich errors, both internally and to a possibly public frontend.

The Error interface of course implements the go Error interface, so you can always use use app errors as plain errors when required.

Errors can contain the following data:

  • Code: a string code that uniquely identifies the error.
  • Status: an integer value to identify the type of error (for example an http status code, or a unique error number).
  • Message: an explicative message intended for humans.
  • Data: arbitrary data related to the error.
  • Errors: nested errors
  • Public: specifies whether the error may be presented publically (for example to an api user or website)

Usage

import(
	"fmt"
	"github.com/theduke/go-apperror"
)

// Creating a new error.
func createString() (string, apperror.Error) {
	// Return an error with 'code' "my_application_error", 'status' 22 and a message.
	return "", apperror.New("my_application_error", 22, "Could not create string")
}

// Checking for errors.

func CreateString() string {
	s, err := createString()

	// Check if the error has a specific code.
	if apperror.IsCode("my_application_error") {
		fmt.Printf("XX error occured")
	} 

	// Check for error status.
	if apperror.IsStatus(33) {
		fmt.Printf("Error status 33")
  }

  return s
}

// Wrapping nested errors.

func dbQuery(q string) ([]string, apperror.Error) {
	result, err := db.Query(q)
	if err != nil {
		// Wrap the database error with a generic PUBLIC error.
		// The "true" argument makes the error public, which allows the error to be presented
		// to users.
		publicErr := apperror.Wrap(err, "db_query_error", "Database query failed", true)

		// The original error can be accessed with err.GetErrors()
		for _, err := range  publicErr.GetErrors() {
			fmt.Printf("Nested error: %v\n", err)
		}

		// Additional nested errors can be added.
		publicErr.AddError(errors.New("other_error"))
	}
	return result, nil
}

// Working with errors.

func handleError(err apperror.Error) {
	code := err.GetCode()
	status := err.GetStatus()
	msg := err.GetMessage()
	nestedErrors := err.GetErrors()
	data := err.GetData()

	isPublic := err.IsPublic()

	// Handle specific errors...
}

Reference

apperror.New()

Create a new error.

// For apperror.New(), only the  code argument is  required.
// All additional arguments can be in any order.

err := apperror.New(code string, [status int,] [message string,] [data interface{}, ] [isPublic bool,] [nestedErrors []error])
apperror.Wrap()

Wrap an error.

// For apperror.Wrap(), the original error and the code argument are required.
// All additional arguments can be in any order.

 apperror.Wrap(originalError error, code string, [status int,] [message string,] [data interface{}, ] [isPublic bool,]) apperror.Error
apperror.IsCode

Check if an error has a code.

apperror.IsCode(err error, code string) bool
apperror.IsStatus

Check if an error has a status.

apperror.IsStatus(err error, status int) bool
Manually creating errors.

You can also manually create the error.

err := &apperror.Err{
	Code: "code",
	Status: 111,
	Message: "msg",
	Data: []string{"1", "2"},
	Errors: []error{errors.New("xxx")},
	Public: true,
}
return err

Marshal

Err implements the json Marshal interface to provide more advanced marshalling for public errors:

The marshaled error will not include nested errors.

If marshaling a non-public error, all data will be stripped and the error will just contain the Code "app_error" and a generic error message.

Tipps

  • Always use the apperror.Error interface in function signatures, not the implementation apperror.Err. This allows you to use different implementations if appropriate.
  • Set errors intended for end users to Public, and check to only show public errors.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsCode

func IsCode(err error, code string) bool

func IsStatus

func IsStatus(err error, status int) bool

Types

type Err

type Err struct {
	Code    string      `json:"code,omitempty"`
	Status  int         `json:"status,omitempty"`
	Message string      `json:"title,omitempty"`
	Data    interface{} `json:"data"`
	Errors  []error     `json:"errors,omitempty"`
	Public  bool        `json:"-"`
}

func New

func New(code string, args ...interface{}) *Err

Create a new error. only required argument is string. Other arguments may be: a string to set the message, a bool to set the error to public, an int to set the status, a slice of errors to set the nested errors, and an arbitrary interface{} to set the error.Data.

func Wrap

func Wrap(err error, code string, args ...interface{}) *Err

Wrap an error with an Err. The required arguments are the error to wrap an an error code. Additionally you can supply another string argument as the message, a bool to set if the error is public, and an arbitrary interface{} value to set as data. If you do not supply a message, the original error will be converted to string and used as the message.

func (*Err) AddError

func (e *Err) AddError(err error)

func (Err) Error

func (e Err) Error() string

func (Err) GetCode

func (e Err) GetCode() string

func (Err) GetData

func (e Err) GetData() interface{}

func (Err) GetErrors

func (e Err) GetErrors() []error

func (Err) GetMessage

func (e Err) GetMessage() string

func (Err) GetStatus

func (e Err) GetStatus() int

func (Err) IsPublic

func (e Err) IsPublic() bool

func (Err) MarshalJSON

func (e Err) MarshalJSON() ([]byte, error)

Implement the json Marshaler interface.

func (*Err) SetErrors

func (e *Err) SetErrors(errs []error)

type Error

type Error interface {
	GetCode() string
	GetStatus() int
	GetMessage() string
	GetData() interface{}

	IsPublic() bool

	GetErrors() []error
	SetErrors(errs []error)
	AddError(err error)

	Error() string
}

Jump to

Keyboard shortcuts

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