Skip to content

Commit

Permalink
Add oops.AsMust
Browse files Browse the repository at this point in the history
  • Loading branch information
cpl committed Mar 7, 2024
1 parent e01d3cf commit 3038f43
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/oops/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ func AsAny(err error) (Error, bool) {
return v, ok
}

// AsMust will cast the given error as an Error, if the error does not implement Error, then it will become ErrUncaught.
func AsMust(err error) Error {
if err == nil {
return nil
}

v, ok := err.(Error)
if !ok {
return ErrUncaught.Wrap(err)
}

return v
}

// DeepAs will check if the given err is an Error and if the Error.Source matches the target ErrorDefined, at which
// point err gets returned as an Error. If the given err is not an Error, it will attempt to traverse the unwrap chain
// until an Error is found or nil is reached. Once an Error is found, the check is repeated strictly on Error.Nested
Expand Down

0 comments on commit 3038f43

Please sign in to comment.