Skip to content
A go library that implements application/problem+json
Go
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit Aug 2, 2019
README.md renamed towriteer to writeto Aug 7, 2019
coverage_badge.png houston we have a problem! Aug 2, 2019
problem.go renamed towriteer to writeto Aug 7, 2019
problem_test.go renamed towriteer to writeto Aug 7, 2019

README.md

problem

GoDoc https://github.com/jpoles1/gopherbadger

A golang library that implements application/problem+json

Features

Usage

problem.New(problem.Title("Not Found"), problem.Status(404)).JSONString()

Will produce this:

{
  "status": 404,
  "title": "Not Found"
}

You can also append some more options:

p := problem.New(problem.Title("Not Found"), problem.Status(404))
p.Append(problem.Detail("some more details"))

// create json as []byte
jsonBytes := p.JSON()

Custom key/values are also supported:

problem.New(problem.Title("Not Found"), problem.Custom("key", "value"))

To write the Problem directly to a http.ResponseWriter:

http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    problem.New(problem.Type("https://example.com/404"), problem.Status(404)).WriteTo(w)
  })

If you are using gin you can simply reply the problem to the client:

func(c *gin.Context) {
  problem.New(problem.Title("houston! we have a problem"), problem.Status(http.StatusNotFound)).WriteTo(c.Writer)
}
You can’t perform that action at this time.