apperror

package
v0.0.0-...-6907ca9 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2018 License: MIT Imports: 8 Imported by: 4

README

AppError

How to deal with errors

A known and common way to deal with errors is to handle them gracefully. Unfortunately in an API this is not that easy and the error still needs a context (Status Code, field name, etc.) to let the clients know why a request failed. The best way to handle this is by dealing with errors in the lowest possible function (as opposite to the highest function), because the function failing knows the best why it's failing.

If a higher level function wants to handle the error, apperror.Is* can be used to determine the kind of the error.

Documentation

Index

Constants

View Source
const (
	// ErrDup contains the errcode of a unique constraint violation
	ErrDup = "23505"
)

Variables

This section is empty.

Functions

func GRPCStatusCode

func GRPCStatusCode(code Code) codes.Code

GRPCStatusCode returns the GRPC Code corresponding to the provided app error status code

func HTTPStatusCode

func HTTPStatusCode(code Code) int

HTTPStatusCode returns the HTTP Code corresponding to the provided app error status code

func IsBadRequest

func IsBadRequest(e error) bool

IsBadRequest checks if an error is caused by a bad request

func IsConflict

func IsConflict(e error) bool

IsConflict checks if an error is caused by a conflict

func IsForbidden

func IsForbidden(e error) bool

IsForbidden checks if an error is caused by a forbidden access

func IsInternalServerError

func IsInternalServerError(e error) bool

IsInternalServerError checks if an error is caused by an internal error

func IsInvalidParam

func IsInvalidParam(e error) bool

IsInvalidParam checks if an error is caused by an invalid param

func IsNotFound

func IsNotFound(e error) bool

IsNotFound checks if an error is the NotFound type

func IsUnauthorized

func IsUnauthorized(e error) bool

IsUnauthorized checks if an error is caused by an Unauthorized access

func NewFromError

func NewFromError(err error) error

NewFromError returns an api error based on an error the provided error will be returned if it doesn't match any known error

func NewFromSQL

func NewFromSQL(err error) error

NewFromSQL returns an error based on a pq.Error the provided error will be returned if it's not a pq.Error instance, or if the error cannot be matched to

func StatusText

func StatusText(errCode Code) string

StatusText returns

Types

type AppError

type AppError struct {
	// contains filtered or unexported fields
}

AppError represents an error with a status code and an optional field

func Convert

func Convert(e error) *AppError

Convert takes an error an turns it into an AppError

func NewBadRequest

func NewBadRequest(field string, message string, args ...interface{}) *AppError

NewBadRequest returns an error caused by a user. Example: A missing param

func NewConflict

func NewConflict(field string) *AppError

NewConflict returns an error caused by a conflict with the current state of the app. Example: A duplicate slug

func NewConflictR

func NewConflictR(field string, message string, args ...interface{}) *AppError

NewConflictR returns an error caused by a conflict with the current state of the app. A reason is sent back to the user.

func NewError

func NewError(code Code, field string, message string, args ...interface{}) *AppError

NewError returns an error with an associated code

func NewForbidden

func NewForbidden() *AppError

NewForbidden returns an error caused by a user trying to access a protected resource.

func NewForbiddenR

func NewForbiddenR(reason string) *AppError

NewForbiddenR returns an error caused by a user trying to access a protected resource. A reason is sent back to the user.

func NewInvalidParam

func NewInvalidParam(field string, message string, args ...interface{}) *AppError

NewInvalidParam is an alias for NewBadRequest

func NewNotFound

func NewNotFound() *AppError

NewNotFound returns an error caused by a user trying to access a resource that does not exists

func NewNotFoundField

func NewNotFoundField(field string, reason string) *AppError

NewNotFoundField returns an error caused by a user trying to access a resource that does not exists. A reason is sent back to the user.

func NewNotFoundR

func NewNotFoundR(reason string) *AppError

NewNotFoundR returns an error caused by a user trying to access a resource that does not exists. A reason is sent back to the user.

func NewServerError

func NewServerError(message string, args ...interface{}) *AppError

NewServerError returns an Internal Error.

func NewUnauthorized

func NewUnauthorized() *AppError

NewUnauthorized returns an error caused by a anonymous user trying to access a protected resource

func NewUnauthorizedR

func NewUnauthorizedR(reason string) *AppError

NewUnauthorizedR returns an error caused by a anonymous user trying to access a protected resource. A reason is sent back to the user.

func (*AppError) Field

func (err *AppError) Field() string

Field returns the HTTP param associated to the error

func (*AppError) Origin

func (err *AppError) Origin() error

Origin returns the original error

func (*AppError) StatusCode

func (err *AppError) StatusCode() Code

StatusCode returns the HTTP code associated to the error

type Code

type Code uint

Code represent an Error code

const (
	// NoError is a "zero value" representation of an error.
	// Useful in tests
	NoError Code = 0

	// InvalidArgument is returned when a user provided data is invalid
	InvalidArgument Code = 100

	// NotFound indicates a requested entity was not found
	NotFound Code = 101

	// AlreadyExists indicates an attempt to create an entity failed because
	// it already exists
	AlreadyExists Code = 102

	// Unauthenticated indicates the request does not have a valid
	// authentication credentials
	Unauthenticated Code = 103

	// PermissionDenied indicates the requester does not have the right
	// permissions to execute the request
	PermissionDenied Code = 104

	// Internal indicates something the service is internally broken
	Internal Code = 1000
)

type Error

type Error interface {
	error

	// StatusCode return the HTTP code of the error
	StatusCode() Code

	// Field returns the http/sql/etc. field associated to the error
	Field() string

	// Origin returns the original error if there's one
	Origin() error
}

Error represents an error with a code attached.

Jump to

Keyboard shortcuts

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