errors

package module
v0.0.0-...-d696bdc Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2016 License: MIT Imports: 1 Imported by: 1

README

jsonapi-errors GoDoc Build Status Coverage Status Go ReportCard

This package provides error bindings based on the JSON API reference.

The package provides two main structs that you can use on your application, the Error and Bag structs. When returning errors from your API you should return a Bag containing one or more errors.

bag := NewBagWithError(502, "Oops =(")
jsonStr, _ := json.Marshal(bag)

The above code will return the following JSON structure:

{
  "errors": [
    {
      "detail": "Oops =(",
      "status": "502"
    }
  ],
  "status": "502"
}

Multiple errors

This package adds the possibility to add multiple errors with different status codes. The package will check for the range of the errors and will set the main status key to the lower bound of the error class.

Eg: If add an error 501 and 502, the main status key will be 500.

bag := NewBag()
bag.AddError(501, "Server Error 1")
bag.AddError(502, "Server Error 2")

jsonStr, _ := json.Marshal(bag)

Will return:

{
    "errors": [
        {
            "detail": "Server Error 1",
            "status": "501"
        },
        {
            "detail": "Server Error 2",
            "status": "502"
        }
    ],
    "status": "500"
}

It's also possible to have errors of different classes(400 and 500 for example), in this case the package will silently return 400 as the main status.

bag := NewBag()
bag.AddError(401, "Client Error 1")
bag.AddError(502, "Server Error 1")

jsonStr, _ := json.Marshal(bag)
{
    "errors": [
        {
            "detail": "Client Error 1",
            "status": "401"
        },
        {
            "detail": "Server Error 1",
            "status": "502"
        }
    ],
    "status": "400"
}

Documentation

Overview

Errors package implements the JSON API v1.0 format for errors. Ref: http://jsonapi.org/format/#errors

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetErrorClass

func GetErrorClass(err int) int

GetErrorClass returns 400 or 500 depending of the error code passed. If the error code is less than 400 or greater or equal to 600 it returns 0.

Types

type Bag

type Bag struct {
	Errors []*Error `json:"errors"`
	Status int      `json:"status,string,omitempty"`
}

func NewBag

func NewBag() *Bag

NewBag returns a new Bag

func NewBagWithError

func NewBagWithError(status int, detail string) *Bag

NewBagWithError is a shorthand to `errors.NewBag().Add(status, detail)`

func (*Bag) Add

func (b *Bag) Add(err *Error) *Error

func (*Bag) AddError

func (b *Bag) AddError(status int, detail string) *Error

type Error

type Error struct {
	Code   int                    `json:"code,string,omitempty"`
	Debug  map[string]interface{} `json:"debug,omitempty"`
	Detail string                 `json:"detail"`
	ID     string                 `json:"id,omitempty"`
	Links  *Link                  `json:"links,omitempty"`
	Meta   map[string]interface{} `json:"meta,omitempty"`
	Source *Source                `json:"source,omitempty"`
	Status int                    `json:"status,string"`
	Title  string                 `json:"title,omitempty"`
}

Error provides additional information about problems encountered while performing an operation.

func NewError

func NewError(status int, detail string) *Error

func (*Error) AddSourceNode

func (e *Error) AddSourceNode() *Source

func (*Error) Error

func (e *Error) Error() string
func (e *Error) SetAboutLink(link string)

func (*Error) SetParameter

func (e *Error) SetParameter(parameter string) *Error

func (*Error) SetPointer

func (e *Error) SetPointer(pointer string) *Error
type Link struct {
	About string `json:"about,omitempty"`
}

Link should lead to further details about this error

type Source

type Source struct {
	Pointer   string `json:"pointer,omitempty"`
	Parameter string `json:"parameter,omitempty"`
}

Source contain references to the source of the error

Jump to

Keyboard shortcuts

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