usrerr

package
v0.0.0-...-4263410 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: GPL-3.0 Imports: 1 Imported by: 0

Documentation

Overview

Package usrerr provides error type and predefined values for user-facing HTTP errors.

Since our application makes heavy use of Go error wrapping, many errors end up very long in the form of err: err: err: err: something happened which is not very user-friendly. A custom user message can be provided using usrerr.New function and such error can be wrapped into other errors.

In addition, HTTP code can be provided too which will be used by the rendering package as the result REST HTTP code.

While HTTP code is required (must be not zero), user message can be empty which means the error message itself will be used as user error.

Do not wrap multiple usrerr.Error types into a single error value as only the first one (the top one) will be used for the rendering.

Use all-lowercase style for both errors and user messages.

Some predefined error values are available like ErrUnauthorized401 for use.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadRequest400   = New(400, "bad request", "")
	ErrNotFound404     = New(404, "not found", "")
	ErrUnauthorized401 = New(401, "unauthorized", "")
	ErrForbidden403    = New(403, "forbidden", "")
)

Functions

func New

func New(code int, err, msg string) error

New creates new error with additional information like HTTP code or user-facing message

Types

type Error

type Error struct {
	StatusCode  int    // HTTP status code
	UserMessage string // User facing optional message
	// contains filtered or unexported fields
}

func (Error) Error

func (h Error) Error() string

func (Error) Unwrap

func (h Error) Unwrap() error

Jump to

Keyboard shortcuts

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