Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/AmuzaTkts/jsonapi-errors
Go bindings based on the JSON API errors reference
https://github.com/AmuzaTkts/jsonapi-errors
go golang json-api jsonapi-errors
Last synced: 14 days ago
JSON representation
Go bindings based on the JSON API errors reference
- Host: GitHub
- URL: https://github.com/AmuzaTkts/jsonapi-errors
- Owner: AmuzaTkts
- License: mit
- Created: 2016-07-08T10:08:58.000Z (over 8 years ago)
- Default Branch: develop
- Last Pushed: 2016-11-17T16:02:12.000Z (almost 8 years ago)
- Last Synced: 2024-07-31T20:52:11.372Z (3 months ago)
- Topics: go, golang, json-api, jsonapi-errors
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 14
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - jsonapi-errors - Go bindings based on the JSON API errors reference. (JSON / Search and Analytic Databases)
- awesome-go - jsonapi-errors - Go bindings based on the JSON API errors reference - ★ 4 (Utilities)
- awesome-go-extra - jsonapi-errors - 07-08T10:08:58Z|2016-11-17T16:02:12Z| (JSON / Advanced Console UIs)
README
# jsonapi-errors [![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](http://godoc.org/github.com/AmuzaTkts/jsonapi-errors) [![Build Status](https://travis-ci.org/AmuzaTkts/jsonapi-errors.svg?branch=master)](https://travis-ci.org/AmuzaTkts/jsonapi-errors) [![Coverage Status](https://coveralls.io/repos/github/AmuzaTkts/jsonapi-errors/badge.svg?branch=master)](https://coveralls.io/github/AmuzaTkts/jsonapi-errors?branch=master) [![Go ReportCard](https://goreportcard.com/badge/github.com/AmuzaTkts/jsonapi-errors)](https://goreportcard.com/report/github.com/AmuzaTkts/jsonapi-errors)
This package provides error bindings based on the [JSON API](http://jsonapi.org/format/#errors)
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.```Go
bag := NewBagWithError(502, "Oops =(")
jsonStr, _ := json.Marshal(bag)
```The above code will return the following JSON structure:
```JSON
{
"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`.
```Go
bag := NewBag()
bag.AddError(501, "Server Error 1")
bag.AddError(502, "Server Error 2")jsonStr, _ := json.Marshal(bag)
```Will return:
```JSON
{
"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.```Go
bag := NewBag()
bag.AddError(401, "Client Error 1")
bag.AddError(502, "Server Error 1")jsonStr, _ := json.Marshal(bag)
``````JSON
{
"errors": [
{
"detail": "Client Error 1",
"status": "401"
},
{
"detail": "Server Error 1",
"status": "502"
}
],
"status": "400"
}
```