Skip to content

Commit

Permalink
Merge pull request #10 from danielbprice/master
Browse files Browse the repository at this point in the history
feat: Add WrapSilent() to wrap an error but not include it in the JSON body
solves #11
  • Loading branch information
mschneider82 authored Nov 27, 2023
2 parents 3afa67c + 3c7541d commit 4cf8baf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ func Wrap(err error) Option {
})
}

// WrapSilent wraps an error inside of the Problem without placing the wrapped
// error into the problem's JSON body. Useful for cases where the underlying
// error needs to be preserved but not transmitted to the user.
func WrapSilent(err error) Option {
return optionFunc(func(problem *Problem) {
problem.reason = err
})
}

// Type sets the type URI (typically, with the "http" or "https" scheme) that identifies the problem type.
// When dereferenced, it SHOULD provide human-readable documentation for the problem type
func Type(uri string) Option {
Expand Down
10 changes: 10 additions & 0 deletions problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ func TestNestedErrors(t *testing.T) {
if errors.Unwrap(unwrappedProblem) != nil {
t.Fatalf("Expected unwrappedProblem has no reason")
}
// See wrapped error in 'reason'
if p.JSONString() != `{"reason":"{\"status\":404,\"title\":\"Root Problem\"}","title":"high level error msg"}` {
t.Fatalf("Unexpected contents %s in problem", p.JSONString())
}

p = problem.New(problem.WrapSilent(rootProblem), problem.Title("high level error msg"))
// We should not see a "reason" here
if p.JSONString() != `{"title":"high level error msg"}` {
t.Fatalf("Unexpected contents %s in problem", p.JSONString())
}
}

func TestOsErrorInProblem(t *testing.T) {
Expand Down

0 comments on commit 4cf8baf

Please sign in to comment.