A golang library that implements application/problem+json



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"))

// Use the Marshaler interface to get the problem json as []byte
jsonBytes, err := json.Marshal(p)

// or simpler (ignores the error)
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) {

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

func(c *gin.Context) {
    problem.Title("houston! we have a problem"),

Creates a Problem from an existing error

  _, err := ioutil.ReadFile("non-existing")
  if err != nil {
    p := problem.New(
      problem.Title("Internal Error"),
    if !errors.Is(p, os.ErrNotExist) {
      t.Fatalf("expected not existing error")




View Source
const (
	// ContentTypeJSON
	ContentTypeJSON = "application/problem+json"
	// ContentTypeXML
	ContentTypeXML = "application/problem+xml"


This section is empty.


This section is empty.


type Option

type Option interface {
	// contains filtered or unexported methods

    An Option configures a Problem using the functional options paradigm popularized by Rob Pike.

    func Custom

    func Custom(key string, value interface{}) Option

      Custom sets a custom key value

      func Detail

      func Detail(detail string) Option

        Detail A human readable explanation specific to this occurrence of the problem.

        func Instance

        func Instance(uri string) Option

          Instance an absolute URI that identifies the specific occurrence of the problem.

          func Status

          func Status(status int) Option

            Status sets the HTTP status code generated by the origin server for this occurrence of the problem.

            func Title

            func Title(title string) Option

              Title sets a title that appropriately describes it (think short) Written in english and readable for engineers (usually not suited for non technical stakeholders and not localized); example: Service Unavailable

              func Type

              func Type(uri string) Option

                Type sets the type URI (typically, with the "http" or "https" scheme) that identifies the problem type. When dereferenced, it SHOULD provide human-readable documentation for the problem type

                func Wrap

                func Wrap(err error) Option

                  Wrap an error to the Problem

                  type Problem

                  type Problem struct {
                  	// contains filtered or unexported fields

                  func New

                  func New(opts ...Option) *Problem

                    New generates a new Problem

                    func (*Problem) Append

                    func (p *Problem) Append(opts ...Option) *Problem

                      Append an Option to a existing Problem

                      func (Problem) Error

                      func (p Problem) Error() string

                        Error implements the error interface, so a Problem can be used as an error

                        func (Problem) Is

                        func (p Problem) Is(err error) bool

                          Is compares Problem.Error() with err.Error()

                          func (Problem) JSON

                          func (p Problem) JSON() []byte

                            JSON returns the Problem as json bytes

                            func (Problem) JSONString

                            func (p Problem) JSONString() string

                              JSONString returns the Problem as json string

                              func (Problem) MarshalJSON

                              func (p Problem) MarshalJSON() ([]byte, error)

                                MarshalJSON implements the json.Marshaler interface

                                func (Problem) UnmarshalJSON

                                func (p Problem) UnmarshalJSON(b []byte) error

                                  UnmarshalJSON implements the json.Unmarshaler interface

                                  func (Problem) Unwrap

                                  func (p Problem) Unwrap() error

                                    Unwrap returns the result of calling the Unwrap method on err, if err implements Unwrap. Otherwise, Unwrap returns nil.

                                    func (Problem) WriteTo

                                    func (p Problem) WriteTo(w http.ResponseWriter) (int, error)

                                      WriteTo writes the Problem to a http Response Writer

                                      Source Files