httperror

package
v0.0.0-...-6d4bf48 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package httperror defines the format for HTTP error responses from Chain services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter struct {
	Default     Info
	IsTemporary func(info Info, err error) bool
	Errors      map[error]Info
}

Formatter defines rules for mapping errors to the Chain error response format.

func (Formatter) Format

func (f Formatter) Format(err error) (body Response)

Format builds an error Response body describing err by consulting the f.Errors lookup table. If no entry is found, it returns f.Default.

func (Formatter) Log

func (f Formatter) Log(ctx context.Context, err error)

Log writes a structured log entry to the chain/log logger with information about the error and the HTTP response.

func (Formatter) Write

func (f Formatter) Write(ctx context.Context, w http.ResponseWriter, err error)

Write writes a json encoded Response to the ResponseWriter. It uses the status code associated with the error.

Write may be used as an ErrorWriter in the httpjson package.

type Info

type Info struct {
	HTTPStatus int    `json:"-"`
	ChainCode  string `json:"code"`
	Message    string `json:"message"`
}

Info contains a set of error codes to send to the user.

type Response

type Response struct {
	Info
	Detail    string                 `json:"detail,omitempty"`
	Data      map[string]interface{} `json:"data,omitempty"`
	Temporary bool                   `json:"temporary"`
}

Response defines the error response for a Chain error.

func Parse

func Parse(r io.Reader) (*Response, bool)

Parse reads an error Response from the provided reader.

Jump to

Keyboard shortcuts

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