usererrors

package
v0.0.0-...-f05d2e9 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2015 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package usererrors marshals and unmarshals structured errors bound for the end user of a service. It provides an interface for registering custom error types in addition to a set of common basic errors.

Custom errors must conform to the UserError interface and should call Register in the init function of the package in which they are defined. The Code returned by the error must be unique across the application and any packages it imports. The Message returned should be a complete and properly puntuated sentence that can be displayed directly to the user. It should be generated exclusively from the contents of the error type, not provided by the caller. The provides the client the option of either displaying the generated error message directly to the end user or generating a custom message.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSON

func MarshalJSON(uerr UserError) ([]byte, error)

MarshalJSON encodes the UserError as JSON. If the provided value is an array, map, slice or struct with at least one field it is marshalled into the `data` field.

func Register

func Register(uerr UserError) error

Register associates an error code string with a concrete type for unmarshalling.

Types

type ActionNotAllowed

type ActionNotAllowed struct {
	Action string `json:"action"`
}

ActionNotAllowed describes an action that is not permitted.

func (ActionNotAllowed) Code

func (e ActionNotAllowed) Code() string

Code returns "action_not_allowed"

func (ActionNotAllowed) Message

func (e ActionNotAllowed) Message() string

Message returns a string describing the disallowed action

type AuthInvalid

type AuthInvalid struct{}

AuthInvalid indicates that the authorization you provided is invalid.

func (AuthInvalid) Code

func (e AuthInvalid) Code() string

Code returns "auth_invalid"

func (AuthInvalid) Message

func (e AuthInvalid) Message() string

Message returns a generic unauthorized message.

type InternalFailure

type InternalFailure struct{}

InternalFailure represents a prviate internal error.

func (InternalFailure) Code

func (e InternalFailure) Code() string

Code returns "internal_failure"

func (InternalFailure) Message

func (e InternalFailure) Message() string

Message returns a generic internal error message

type InvalidParams

type InvalidParams []InvalidParamsEntry

InvalidParams represents a list of parameter validation errors. Each element in the list contains an explanation of the error and a list of the parameters that failed.

func (InvalidParams) Code

func (e InvalidParams) Code() string

Code returns "invalid_params"

func (InvalidParams) Message

func (e InvalidParams) Message() string

Message returns a joined representation of parameter messages. When possible, the underlying data should be used instead to separate errors by parameter.

type InvalidParamsEntry

type InvalidParamsEntry struct {
	Params  []string `json:"params"`
	Message string   `json:"message"`
}

InvalidParamsEntry represents a single error for InvalidParams

type NotFound

type NotFound struct{}

NotFound indicates that the requested resource could not be found.

func (NotFound) Code

func (e NotFound) Code() string

Code returns "not_found"

func (NotFound) Message

func (e NotFound) Message() string

Message returns a generic not found message.

type UserError

type UserError interface {
	Code() string
	Message() string
}

UserError represents an error that can be returned to the client. UserErrors should be instantiated at the package-level with constant error strings.

func UnmarshalJSON

func UnmarshalJSON(data []byte) (UserError, error)

UnmarshalJSON parses a JSON-encoded UserError. If the code of the error has been registered, the registered type is returned.

Jump to

Keyboard shortcuts

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