Skip to content

tjdavis3/problems

Folders and files

NameName
Last commit message
Last commit date

Latest commit

98336c0 · Sep 21, 2024

History

22 Commits
Sep 21, 2024
Aug 14, 2023
Sep 21, 2024
Aug 13, 2023
May 25, 2022
May 25, 2022
Sep 21, 2024
Jul 25, 2024
Jul 18, 2024
Jul 25, 2024

Repository files navigation

problems

import "github.com/tjdavis3/problems"

The problems package represents RFC7807 problem details.

Example

Basic example

{
	prob := New(500, "An Error has occurred")
	prob.Set("Title", "Test Error")
	prob.Set("Instance", "/error/test")
	prob.PrettyPrint()

}

Output

{
  "detail": "An Error has occurred",
  "instance": "/error/test",
  "status": 500,
  "title": "Test Error",
  "type": "about:blank"
}

Example (Array)

Example with extended attributes, including an array of problem fields. The type must be set to something other than about:blank to add extended attributes

{
	prob := New(500, "An Error has occurred")
	prob.Set("Title", "Test Error")
	prob.Set("Instance", "/error/test")
	prob.Set("Type", "uri:example:extended")
	prob.Set("TraceID", "12345-67890")
	issues := make(map[string]interface{})
	issues["field"] = "state"
	issues["message"] = "A valid state must be provided"
	prob.Set("invalid-params", []map[string]interface{}{issues})
	prob.PrettyPrint()

}

Output

{
  "detail": "An Error has occurred",
  "instance": "/error/test",
  "invalid-params": [
    {
      "field": "state",
      "message": "A valid state must be provided"
    }
  ],
  "status": 500,
  "title": "Test Error",
  "traceid": "12345-67890",
  "type": "uri:example:extended"
}

Example (Extended)

Example with extended attributes. The type must be set to something other than about:blank to add extended attributes

{
	prob := New(500, "An Error has occurred")
	prob.Set("Title", "Test Error")
	prob.Set("Instance", "/error/test")
	prob.Set("Type", "uri:example:extended")
	prob.Set("TraceID", "12345-67890")
	prob.PrettyPrint()

}

Output

{
  "detail": "An Error has occurred",
  "instance": "/error/test",
  "status": 500,
  "title": "Test Error",
  "traceid": "12345-67890",
  "type": "uri:example:extended"
}

Index

Constants

const (
    // ProblemMediaType is the default media type for a Problem response
    ProblemMediaType = "application/problem+json"
)

type Problem

Problem is an RFC7807 representation of an error

type Problem struct {
    // Type is a URI reference [RFC3986] that identifies the
    //   problem type.  This specification encourages that, when
    //   dereferenced, it provide human-readable documentation for the
    //   problem type (e.g., using HTML [W3C.REC-html5-20141028]).  When
    //   this member is not present, its value is assumed to be
    //   "about:blank".
    Type string `json:"type,omitempty"`
    // Title is a short, human-readable summary of the problem
    // type.  It SHOULD NOT change from occurrence to occurrence of the
    // problem, except for purposes of localization (e.g., using
    // proactive content negotiation; see [RFC7231], Section 3.4).
    Title string
    // Status is the HTTP status code ([RFC7231], Section 6)
    // generated by the origin server for this occurrence of the problem.
    Status int
    // Detail is a human-readable explanation specific to this
    // occurrence of the problem.
    Detail string
    // Instance is a URI reference that identifies the specific
    // occurrence of the problem.  It may or may not yield further
    // information if dereferenced.
    Instance string `json:"instance,omitempty"`
    // Attributes are extra fields/data that can be added to the problem.
    // They should be set with the `Set` method.  The `Type` MUST be set
    // and cannot be `about:blank`
    Attributes map[string]interface{} `json:"vars,omitempty" xml:"vars,omitempty"`
    // contains filtered or unexported fields
}

Generated by gomarkdoc

About

RFC7807 problem details implementation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages