Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error encapsulation #2080

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions concepts/errors/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,13 @@ if _, err := os.Getwd(); err != nil {
[stackoverflow-errors]: https://stackoverflow.com/a/50333850
[line-of-sight]: https://medium.com/@matryer/line-of-sight-in-code-186dd7cdea88

## Encapsulating error types and values

While it is advised above, to early return most errors that occur. There are occasions where defining behavior upon a specific type, or exported value of error is more appropriate. When that happens, only the specified error types or values would not be immediately returned. This Encapsulation of errors is not unique to golang, but can be thought of as analogous to, catching an Exception in other languages. The purpose might be to differentiate between things in-scope of a library or framework; and things that have not (perhaps yet) been considered; or are deliberately excluded from a domain or context.

```go
if err != nil && err != ErrSpecificCaseWeHandleOrEncapsulateWithOurOwnError {
return 0.0, err
}
// do something else with the error.
```