apierror

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 7 Imported by: 10

Documentation

Index

Constants

View Source
const (
	ContentTypeV1           = "application/vnd.mysterium.error+json"
	ErrCodeNotFound         = "not_found"
	ErrCodeInternal         = "internal"
	ErrCodeParseFailed      = "parse_failed"
	ErrCodeValidationFailed = "validation_failed"
	ErrCodeUnavailable      = "unavailable"
	ErrCodeUnauthorized     = "shall_not_pass"
)
View Source
const (
	ValidateErrRequired   = "required"
	ValidateErrInvalidVal = "invalid_value"
)

Variables

View Source
var DefaultErrStatic, _ = json.Marshal(defaultErr())

Functions

func ErrorHandler

func ErrorHandler(c *gin.Context)

ErrorHandler gets the first error from request context and formats it to an error response.

func TryParse added in v0.3.0

func TryParse(response *http.Response) error

TryParse parses http.Response into an APIError closing (consuming) http.Response.Body. If header matches and error can be parsed in to APIError it will return that. If No such header exists a regular error is returned with it's contents being a response body.

Types

type APIError

type APIError struct {
	Err    Err    `json:"error"`
	Status int    `json:"status"`
	Path   string `json:"path"`
}

APIError represents an error response from REST API service.

func BadRequest

func BadRequest(message string, code string) *APIError

func BadRequestField

func BadRequestField(message string, code string, field string) *APIError

func BadRequestFields

func BadRequestFields(fields map[string]FieldError) *APIError

func Conflict

func Conflict(message string, code string, field string) *APIError

func Error

func Error(status int, message string, code string) *APIError

func Forbidden

func Forbidden(message string, code string) *APIError

func Internal

func Internal(message string, code string) *APIError

func InternalDefault

func InternalDefault() *APIError

func NotFound

func NotFound(message string) *APIError

func Parse

func Parse(response *http.Response) *APIError

Parse parses http.Response into an APIError closing (consuming) http.Response.Body.

func ParseFailed

func ParseFailed() *APIError

func ServiceUnavailable

func ServiceUnavailable() *APIError

func Unauthorized

func Unauthorized() *APIError

func Unprocessable

func Unprocessable(message string, code string) *APIError

func (*APIError) Detail added in v0.2.0

func (e *APIError) Detail() string

Detail returns a detailed message for humans.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) Message

func (e *APIError) Message() string

Message returns a message for humans.

type Err

type Err struct {
	Code    string                `json:"code"`
	Message string                `json:"message"`
	Detail  string                `json:"detail,omitempty"`
	Fields  map[string]FieldError `json:"fields,omitempty"`
}

Err contains the error details.

type FieldError

type FieldError struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

FieldError contains the reason why a field failed validation.

type Validator

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

func NewValidator

func NewValidator() *Validator

func (*Validator) Err

func (v *Validator) Err() *APIError

func (*Validator) Fail

func (v *Validator) Fail(field string, errorCode string, message string)

func (*Validator) Invalid

func (v *Validator) Invalid(field string, message string)

func (*Validator) Required

func (v *Validator) Required(field string)

Jump to

Keyboard shortcuts

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