Skip to content

Commit 6301ca8

Browse files
committed
fix: adjust typo and handle mutex
1 parent 03f137f commit 6301ca8

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

problem_details.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package httpsuite
22

3+
import "sync"
4+
35
const BlankUrl = "about:blank"
46

5-
var problemBaseURL = BlankUrl
6-
var errorTypePaths = map[string]string{
7-
"validation_error": "/errors/validation-error",
8-
"not_found_error": "/errors/not-found",
9-
"server_error": "/errors/server-error",
10-
"bad_request_error": "/errors/bad-request",
11-
}
7+
var (
8+
mu sync.RWMutex
9+
problemBaseURL = BlankUrl
10+
errorTypePaths = map[string]string{
11+
"validation_error": "/errors/validation-error",
12+
"not_found_error": "/errors/not-found",
13+
"server_error": "/errors/server-error",
14+
"bad_request_error": "/errors/bad-request",
15+
}
16+
)
1217

1318
// ProblemDetails conforms to RFC 9457, providing a standard format for describing errors in HTTP APIs.
1419
type ProblemDetails struct {
@@ -52,6 +57,8 @@ func NewProblemDetails(status int, problemType, title, detail string) *ProblemDe
5257
//
5358
// If the base URL is not set, the default value for the "type" field will be "about:blank".
5459
func SetProblemBaseURL(baseURL string) {
60+
mu.Lock()
61+
defer mu.Unlock()
5562
problemBaseURL = baseURL
5663
}
5764

@@ -71,6 +78,8 @@ func SetProblemBaseURL(baseURL string) {
7178
//
7279
// "https://api.mycompany.com/errors/validation-error"
7380
func SetProblemErrorTypePath(errorType, path string) {
81+
mu.Lock()
82+
defer mu.Unlock()
7483
errorTypePaths[errorType] = path
7584
}
7685

@@ -91,6 +100,8 @@ func SetProblemErrorTypePath(errorType, path string) {
91100
//
92101
// This method overwrites any existing paths with the same keys.
93102
func SetProblemErrorTypePaths(paths map[string]string) {
103+
mu.Lock()
104+
defer mu.Unlock()
94105
for errorType, path := range paths {
95106
errorTypePaths[errorType] = path
96107
}

request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func ParseRequest[T RequestParamSetter](w http.ResponseWriter, r *http.Request,
9797
if err := request.SetParam(key, value); err != nil {
9898
problem := NewProblemDetails(
9999
http.StatusInternalServerError,
100-
GetProblemTypeURL("sever_error"),
100+
GetProblemTypeURL("server_error"),
101101
"Parameter Error",
102102
"Failed to set field "+key,
103103
)

0 commit comments

Comments
 (0)