Skip to content

Commit

Permalink
Add Usage section to the README
Browse files Browse the repository at this point in the history
Explains how to use this package in three scenarios:
1. as a library author of a wrapped exception type
2. In client code, as a user of a potentially wrapped exception (e.g. any concurrent code)
3. In tests of potentially wrapped exceptions
  • Loading branch information
NHDaly authored Apr 1, 2020
1 parent 17c27ff commit 8f3ff38
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ that caused that Task to fail. Another example is the exception types in

- `unwrap_exception_to_root(normal_exception) -> normal_exception`

## Usage

If your library provides a wrapped exception type, you should register it
with this package by simply adding a method to `unwrap_exception`:
```julia
ExceptionUnwrapping.unwrap_exception(e::MyWrappedException) = e.exception
```

In client code, you should use `has_wrapped_exception` and `unwrap_exception_until`
in catch blocks:
```julia
try
...
catch e
if has_wrapped_exception(e, BoundsError)
be = unwrap_exception_until(e, BoundsError)
# ...Use BoundsError...
else
rethrow()
end
end
```

Finally, you can improve robustness in client tests via `@test_throws_wrapped`:
```julia
@test_throws_wrapped AssertionError my_possibly_multithreaded_function()
```

## Motivating Example: Stable Exception Handling
### A Problem: Adding Concurrency to a Library Can Break Users' Exception Handling
As we all start using concurrency more, exception handling can get a bit weird. Julia's
Expand Down

0 comments on commit 8f3ff38

Please sign in to comment.