Skip to content

Commit 04359c3

Browse files
committed
feat(problems): customizable errors key
1 parent bb8116c commit 04359c3

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ for HTTP APIs (RFC 9457), making it easy to create standards-compliant APIs.
1212
allowing handlers to return arbitrary errors.
1313
- Error Handling with Responder Interface: Any returned error is checked for Responder
1414
implementation, providing flexibility in crafting responses.
15+
- Automatic error stack when a failure is provided. Errors and problems automatically craft
16+
an error stack in an specific field.
1517
- Built-In Response Builder: Quickly build and customize responses while adhering to best
1618
practices like setting appropriate HTTP headers.
1719
- Middleware Support: Seamlessly integrate custom or provided middlewares for cleaner,

problem.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ type ProblemControls struct {
141141
// of errors from the error it comes from (if any).
142142
Errors ProblemControlsResolver[bool]
143143

144+
// ErrorsKey determines the key to use when appending the
145+
// error stack when [ProblemControls]'s Errors returns true.
146+
ErrorsKey ProblemControlsResolver[string]
147+
144148
// Response allows customizing the actual Builder response
145149
// that a [Problem] should be resolved to.
146150
Response ProblemControlsResolver[Builder]
@@ -181,6 +185,10 @@ func defaultedProblemControls(controls ProblemControls) ProblemControls {
181185
controls.Errors = defaultProblemControlsErrors
182186
}
183187

188+
if controls.ErrorsKey == nil {
189+
controls.ErrorsKey = defaultProblemControlsErrorsKey
190+
}
191+
184192
if controls.Response == nil {
185193
controls.Response = defaultProblemControlsResponse
186194
}
@@ -237,6 +245,11 @@ func defaultProblemControlsErrors(problem Problem, request *http.Request) bool {
237245
return true
238246
}
239247

248+
// defaultProblemControlsInstance is the default value for the [ProblemControls] instance.
249+
func defaultProblemControlsErrorsKey(problem Problem, request *http.Request) string {
250+
return "errors"
251+
}
252+
240253
// ProblemControlsResponseFrom is a helper that generates a [ProblemControlsResolver] with
241254
// the given response content types. It is very useful to map mimes to responses.
242255
func ProblemControlsResponseFrom(responses map[string]Builder) ProblemControlsResolver[Builder] {
@@ -460,7 +473,10 @@ func (problem Problem) Defaulted(request *http.Request) Problem {
460473
messages[i] = trace.Error()
461474
}
462475

463-
problem = problem.With("errors", messages)
476+
problem = problem.With(
477+
controls.ErrorsKey(problem, request),
478+
messages,
479+
)
464480
}
465481

466482
if lower {

0 commit comments

Comments
 (0)