Skip to content

Commit 7fa812b

Browse files
authored
Merge pull request #8 from peczenyj/fix-docs-remove-mentions-to-DefaultProblem
Fix docs remove mentions to default problem
2 parents b59df3a + 8cab013 commit 7fa812b

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

README.md

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
# problems
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).
1+
# Problems
2+
3+
Problems is an RFC-7807 and RFC-9457 compliant library for describing HTTP errors.
4+
For more information see [RFC-9457](https://tools.ietf.org/html/rfc9457), and it's predecessor [RFC-7807](https://tools.ietf.org/html/rfc7807).
55

66
[![Build Status](https://travis-ci.org/moogar0880/problems.svg?branch=master)](https://travis-ci.org/moogar0880/problems)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/moogar0880/problems)](https://goreportcard.com/report/github.com/moogar0880/problems)
88
[![GoDoc](https://godoc.org/github.com/moogar0880/problems?status.svg)](https://godoc.org/github.com/moogar0880/problems)
99

1010
## Usage
11+
1112
The problems library exposes an assortment of types to aid HTTP service authors
1213
in defining and using HTTP Problem detail resources.
1314

1415
### Predefined Errors
16+
1517
You can define basic Problem details up front by using the `NewStatusProblem`
1618
function
1719

@@ -39,17 +41,18 @@ Which, when served over HTTP as JSON will look like the following:
3941
```
4042

4143
### Detailed Errors
44+
4245
New errors can also be created a head of time, or on the fly like so:
4346

4447
```go
4548
package main
4649

4750
import "github.com/moogar0880/problems"
4851

49-
func NoSuchUser() *problems.DefaultProblem {
50-
nosuch := problems.NewStatusProblem(404)
51-
nosuch.Detail = "Sorry, that user does not exist."
52-
return nosuch
52+
func NoSuchUser() *problems.Problem {
53+
nosuch := problems.NewStatusProblem(404)
54+
nosuch.Detail = "Sorry, that user does not exist."
55+
return nosuch
5356
}
5457
```
5558

@@ -65,6 +68,7 @@ Which, when served over HTTP as JSON will look like the following:
6568
```
6669

6770
### Extended Errors
71+
6872
The specification for these HTTP problems was designed to allow for arbitrary
6973
expansion of the problem resources. This can be accomplished through this
7074
library by either embedding the `Problem` struct in your extension type:
@@ -75,7 +79,7 @@ package main
7579
import "github.com/moogar0880/problems"
7680

7781
type CreditProblem struct {
78-
problems.Problem
82+
problems.Problem
7983

8084
Balance float64 `json:"balance"`
8185
Accounts []string `json:"accounts"`
@@ -100,24 +104,24 @@ Or by using the `problems.ExtendedProblem` type:
100104
package main
101105

102106
import (
103-
"net/http"
107+
"net/http"
104108

105-
"github.com/moogar0880/problems"
109+
"github.com/moogar0880/problems"
106110
)
107111

108112
type CreditProblemExt struct {
109-
Balance float64 `json:"balance"`
110-
Accounts []string `json:"accounts"`
113+
Balance float64 `json:"balance"`
114+
Accounts []string `json:"accounts"`
111115
}
112116

113117
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+
problems.NewExt[CreditProblemExt]().
119+
WithStatus(http.StatusForbidden).
120+
WithDetail("Your account does not have sufficient funds to complete this transaction").
121+
WithExtension(CreditProblemExt{
118122
Balance: 30,
119123
Accounts: []string{"/account/12345", "/account/67890"},
120-
})
124+
})
121125
}
122126
```
123127

@@ -129,13 +133,14 @@ Which, when served over HTTP as JSON will look like the following:
129133
"title": "Unauthorized",
130134
"status": 401,
131135
"extensions": {
132-
"balance": 30,
133-
"accounts": ["/account/12345", "/account/67890"]
136+
"balance": 30,
137+
"accounts": ["/account/12345", "/account/67890"]
134138
}
135139
}
136140
```
137141

138142
## Serving Problems
143+
139144
Additionally, RFC-7807 defines two new media types for problem resources,
140145
`application/problem+json"` and `application/problem+xml`. This library defines
141146
those media types as the constants `ProblemMediaType` and
@@ -149,18 +154,18 @@ functioning `HandlerFunc` that will server that error.
149154
package main
150155

151156
import (
152-
"net/http"
157+
"net/http"
153158

154159
"github.com/moogar0880/problems"
155160
)
156161

157162
var Unauthorized = problems.NewStatusProblem(401)
158163

159164
func main() {
160-
mux := http.NewServeMux()
161-
mux.HandleFunc("/secrets", problems.StatusProblemHandler(Unauthorized))
165+
mux := http.NewServeMux()
166+
mux.HandleFunc("/secrets", problems.ProblemHandler(Unauthorized))
162167

163-
server := http.Server{Handler: mux, Addr: ":8080"}
164-
server.ListenAndServe()
168+
server := http.Server{Handler: mux, Addr: ":8080"}
169+
server.ListenAndServe()
165170
}
166171
```

problem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewStatusProblem(status int) *Problem {
6161
return New().WithTitle(http.StatusText(status)).WithStatus(status)
6262
}
6363

64-
// NewDetailedProblem returns a new DefaultProblem with a Detail string set for
64+
// NewDetailedProblem returns a new Problem with a Detail string set for
6565
// a more detailed explanation of the problem being returned.
6666
func NewDetailedProblem(status int, details string) *Problem {
6767
return NewStatusProblem(status).WithDetail(details)

0 commit comments

Comments
 (0)