11# problems
2- Problems is an RFC-7807 compliant library for describing HTTP errors, written
3- purely in Go. For more information see [ RFC-7807] ( https://tools.ietf.org/html/rfc7807 ) .
2+ Problems is an RFC-7807 and RFC-9457 compliant library for describing HTTP
3+ errors. For more information see [ RFC-9457] ( https://tools.ietf.org/html/rfc9457 ) ,
4+ and it's predecessor [ RFC-7807] ( https://tools.ietf.org/html/rfc7807 ) .
45
56[ ![ Build Status] ( https://travis-ci.org/moogar0880/problems.svg?branch=master )] ( https://travis-ci.org/moogar0880/problems )
67[ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/moogar0880/problems )] ( https://goreportcard.com/report/github.com/moogar0880/problems )
78[ ![ GoDoc] ( https://godoc.org/github.com/moogar0880/problems?status.svg )] ( https://godoc.org/github.com/moogar0880/problems )
89
910## Usage
10- The problems library exposes an assortment of interfaces, structs, and functions
11- for defining and using HTTP Problem detail resources.
11+ The problems library exposes an assortment of types to aid HTTP service authors
12+ in defining and using HTTP Problem detail resources.
1213
1314### Predefined Errors
1415You can define basic Problem details up front by using the ` NewStatusProblem `
1516function
1617
1718``` go
19+ package main
20+
1821import " github.com/moogar0880/problems"
1922
2023var (
@@ -39,6 +42,8 @@ Which, when served over HTTP as JSON will look like the following:
3942New errors can also be created a head of time, or on the fly like so:
4043
4144``` go
45+ package main
46+
4247import " github.com/moogar0880/problems"
4348
4449func NoSuchUser () *problems .DefaultProblem {
@@ -59,31 +64,60 @@ Which, when served over HTTP as JSON will look like the following:
5964}
6065```
6166
62- ### Expanded Errors
67+ ### Extended Errors
6368The specification for these HTTP problems was designed to allow for arbitrary
6469expansion of the problem resources. This can be accomplished through this
65- library by implementing the ` Problem ` interface :
70+ library by either embedding the ` Problem ` struct in your extension type :
6671
6772``` go
73+ package main
74+
6875import " github.com/moogar0880/problems"
6976
7077type CreditProblem struct {
71- problems.DefaultProblem
78+ problems.Problem
79+
80+ Balance float64 ` json:"balance"`
81+ Accounts []string ` json:"accounts"`
82+ }
83+ ```
84+
85+ Which, when served over HTTP as JSON will look like the following:
7286
73- Balance float64
74- Accounts []string
87+ ``` json
88+ {
89+ "type" : " about:blank" ,
90+ "title" : " Unauthorized" ,
91+ "status" : 401 ,
92+ "balance" : 30 ,
93+ "accounts" : [" /account/12345" , " /account/67890" ]
7594}
95+ ```
96+
97+ Or by using the ` problems.ExtendedProblem ` type:
98+
99+ ``` go
100+ package main
101+
102+ import (
103+ " net/http"
104+
105+ " github.com/moogar0880/problems"
106+ )
76107
77- func (cp *CreditProblem ) ProblemType () (*url .URL , error ) {
78- u , err := url.Parse (cp.Type )
79- if err != nil {
80- return nil , err
81- }
82- return u, nil
108+ type CreditProblemExt struct {
109+ Balance float64 ` json:"balance"`
110+ Accounts []string ` json:"accounts"`
83111}
84112
85- func (cp *CreditProblem ) ProblemTitle () string {
86- return cp.Title
113+ func main () {
114+ problems.NewExt [CreditProblemExt]().
115+ WithStatus (http.StatusForbidden ).
116+ WithDetail (" Your account does not have sufficient funds to complete this transaction" ).
117+ WithExtension (CreditProblemExt{
118+ Balance: 30 ,
119+ Accounts: []string {" /account/12345" , " /account/67890" },
120+ })
87121}
88122```
89123
@@ -93,9 +127,11 @@ Which, when served over HTTP as JSON will look like the following:
93127{
94128 "type" : " about:blank" ,
95129 "title" : " Unauthorized" ,
96- "status" : 401 ,
97- "balance" : 30 ,
98- "accounts" : [" /account/12345" , " /account/67890" ]
130+ "status" : 401 ,
131+ "extensions" : {
132+ "balance" : 30 ,
133+ "accounts" : [" /account/12345" , " /account/67890" ]
134+ }
99135}
100136```
101137
0 commit comments