11package httpsuite
22
3+ import "sync"
4+
35const 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.
1419type 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".
5459func 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"
7380func 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.
93102func SetProblemErrorTypePaths (paths map [string ]string ) {
103+ mu .Lock ()
104+ defer mu .Unlock ()
94105 for errorType , path := range paths {
95106 errorTypePaths [errorType ] = path
96107 }
0 commit comments