Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mvmaasakkers/go-problemdetails
Problem json implementation (https://tools.ietf.org/html/rfc7807) package for go
https://github.com/mvmaasakkers/go-problemdetails
Last synced: about 1 month ago
JSON representation
Problem json implementation (https://tools.ietf.org/html/rfc7807) package for go
- Host: GitHub
- URL: https://github.com/mvmaasakkers/go-problemdetails
- Owner: mvmaasakkers
- License: mit
- Created: 2019-05-16T05:42:14.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-17T11:12:12.000Z (almost 5 years ago)
- Last Synced: 2024-08-03T19:10:00.804Z (5 months ago)
- Language: Go
- Size: 14.6 KB
- Stars: 17
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-extra - go-problemdetails - 05-16T05:42:14Z|2020-02-17T11:12:12Z| (Utilities / Fail injection)
README
# Golang Problem Details
[![Build Status](https://travis-ci.com/mvmaasakkers/go-problemdetails.svg?branch=master)](https://travis-ci.com/mvmaasakkers/go-problemdetails)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[![GoDoc](https://godoc.org/github.com/mvmaasakkers/go-problemdetails?status.svg)](https://godoc.org/github.com/mvmaasakkers/go-problemdetails)
[![Go Report Card](https://goreportcard.com/badge/github.com/mvmaasakkers/go-problemdetails)](https://goreportcard.com/report/github.com/mvmaasakkers/go-problemdetails)
[![CodeFactor](https://www.codefactor.io/repository/github/mvmaasakkers/go-problemdetails/badge)](https://www.codefactor.io/repository/github/mvmaasakkers/go-problemdetails)
[![Coverage Status](https://coveralls.io/repos/github/mvmaasakkers/go-problemdetails/badge.svg?branch=master)](https://coveralls.io/github/mvmaasakkers/go-problemdetails?branch=master)Problem details implementation (https://tools.ietf.org/html/rfc7807) package for go.
`go get github.com/mvmaasakkers/go-problemdetails`
## How to use
The `ProblemDetails` struct can be used as `error` because it implements the `error` interface. The `ProblemType`
interface can be used to create predefined `ProblemDetails` with extensions and also implements the `error` interface.The struct is setup to be used by the [json](https://golang.org/pkg/encoding/json/) and
[xml](https://golang.org/pkg/encoding/xml/) marshaler from the stdlib and will marshal into `application/problem+json`
or `application/problem+xml` compliant data as defined in the [RFC 7807](https://tools.ietf.org/html/rfc7807).To generate a `ProblemDetails` based on just a HTTP Status Code you can create one using `NewHTTP(statusCode int)`:
```go
problemDetails := problemdetails.NewHTTP(http.StatusNotFound)
```This will generate a `ProblemDetails` struct that marshals as follows:
```json
{
"type": "about:blank",
"title": "Not Found",
"status": 404
}
``````xml
about:blank
Not Found
404```
or use the more verbose `New(statusCode int, problemType, title, detail, instance string)`:
```go
problemDetails := problemdetails.New(http.StatusNotFound, "https://example.net/problem/object_not_found", "Object not found", "Object with id 1234 was not found, another id should be given.", "https://api.example.net/objects/1234")
```This will generate a `ProblemDetails` struct that marshals as follows:
```json
{
"type": "https://example.net/problem/object_not_found",
"title": "Object not found",
"status": 404,
"detail": "Object with id 1234 was not found, another id should be given.",
"instance": "https://api.example.net/objects/1234"
}
``````xml
https://example.net/problem/object_not_found
Object not found
404
Object with id 1234 was not found, another id should be given.
https://api.example.net/objects/1234```
## Http helpers
For ease of use there are two output handlers available. `ProblemDetails.ServeJSON` for JSON and `ProblemDetails.ServeXML` for XML.
A shorthand for generating a 404 statuscode in Problem Details JSON to the ResponseWriter you can:
```go
problemdetails.NewHTTP(http.StatusNotFound).ServeJSON(w, r)
```