apperrors

package
v0.0.0-...-59258b7 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Status

func Status(err error) int

Status checks the runtime type of the error and returns an http status code if the error is model.Error

Types

type Error

type Error struct {
	Type    Type   `json:"type"`
	Message string `json:"message"`
}

Error holds a custom error for the application which is helpful in returning a consistent error type/message from API endpoints

func NewAuthorization

func NewAuthorization(reason string) *Error

NewAuthorization to create a 401

func NewBadRequest

func NewBadRequest(reason string) *Error

NewBadRequest to create 400 errors (validation, for example)

func NewConflict

func NewConflict(name string, value string) *Error

NewConflict to create an error for 409

func NewInternal

func NewInternal() *Error

NewInternal for 500 errors and unknown errors

func NewNotFound

func NewNotFound(name string, value string) *Error

NewNotFound to create an error for 404

func NewPayloadTooLarge

func NewPayloadTooLarge(maxBodySize int64, contentLength int64) *Error

NewPayloadTooLarge to create an error for 413

func NewServiceUnavailable

func NewServiceUnavailable() *Error

NewServiceUnavailable to create an error for 503

func NewUnsupportedMediaType

func NewUnsupportedMediaType(reason string) *Error

NewUnsupportedMediaType to create an error for 415

func (*Error) Error

func (e *Error) Error() string

Error satisfies standard error interface we can return errors from this package as a regular old go _error_

func (*Error) Status

func (e *Error) Status() int

Status is a mapping errors to status codes Of course, this is somewhat redundant since our errors already map http status codes

type Type

type Type string

Type holds a type string and integer code for the error

const (
	Authorization        Type = "AUTHORIZATION"          // Authentication Failures -
	BadRequest           Type = "BAD_REQUEST"            // Validation errors / BadInput
	Conflict             Type = "CONFLICT"               // Already exists (eg, create account with existent email) - 409
	Internal             Type = "INTERNAL"               // Server (500) and fallback errors
	NotFound             Type = "NOT_FOUND"              // For not finding resource
	PayloadTooLarge      Type = "PAYLOAD_TOO_LARGE"      // for uploading tons of JSON, or an image over the limit - 413
	ServiceUnavailable   Type = "SERVICE_UNAVAILABLE"    // For long running handlers
	UnsupportedMediaType Type = "UNSUPPORTED_MEDIA_TYPE" // for http 415
)

"Set" of valid errorTypes

Jump to

Keyboard shortcuts

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