| 1 |
# Application error handling support |
| 2 |
|
| 3 |
This crate provides support for other crates to effectively report errors. |
| 4 |
Because app-services components get embedded in various apps, written in |
| 5 |
multiple languages, we face several challenges: |
| 6 |
|
| 7 |
- Rust stacktraces are generally not available so we often need to provide |
| 8 |
extra context to help debug where an error is occurring. |
| 9 |
- Without stack traces, Sentry and other error reporting systems don't do a |
| 10 |
great job at auto-grouping errors together, so we need to manually group them. |
| 11 |
- We can't hook directly into the error reporting system or even depend on a |
| 12 |
particular error reporting system to be in use. This means the system |
| 13 |
needs to be simple and flexible enough to plug in to multiple systems. |
| 14 |
|
| 15 |
## Breadcrumbs as context |
| 16 |
|
| 17 |
We use a breadcrumb system as the basis for adding context to errors. |
| 18 |
Breadcrumbs are individual messages that form a log-style stream, and the most |
| 19 |
recent breadcrumbs get attached to each error reported. There are a lot of |
| 20 |
other ways to provide context to an error, but we just use breadcrumbs for |
| 21 |
everything because it's a relatively simple system that's easy to hook up to |
| 22 |
error reporting platforms. |
| 23 |
|
| 24 |
## Basic error reporting tools |
| 25 |
|
| 26 |
Basic error reporting is handled using several macros: |
| 27 |
|
| 28 |
- `report_error!()` creates an error report. It inputs a `type_name` as the |
| 29 |
first parameter, and `format!` style arguments afterwards. `type_name` is |
| 30 |
used to group errors together and show up as error titles/headers. Use the |
| 31 |
format-style args to create is a long-form description for the issue. Most |
| 32 |
of the time you won't need to call this directly, since can automatically |
| 33 |
do it when converting internal results to public ones. However, it can be |
| 34 |
useful in the case where you see an error that you want |
| 35 |
to report, but want to try to recover rather than returning an error. |
| 36 |
- `breadcrumb!()` creates a new breadcrumb that will show up on future errors. |
| 37 |
- `trace_error!()` inputs a `Result<>` and creates a breadcrumb if it's an |
| 38 |
`Err`. This is useful if when you're trying to track down where an error |
| 39 |
originated from, since you can wrap each possible source of the error with |
| 40 |
`trace_error!()`. `trace_error!()` returns the result passed in to it, |
| 41 |
which makes wrapping function calls easy. |
| 42 |
|
| 43 |
|
| 44 |
## Public/Internal errors and converting between them |
| 45 |
|
| 46 |
Our components generally create 2 error enums: one for internal use and one for |
| 47 |
the public API. They are typically named `Error` and |
| 48 |
`[ComponentName]ApiError`. The internal error typically carries a lot of |
| 49 |
low-level details and lots of variants which is useful to us app-services |
| 50 |
developers. The public error typically has less variants with the variants |
| 51 |
often just storing a reason string rather than low-level error codes. There |
| 52 |
are also two `Result<>` types that correspond to these two errors, typically |
| 53 |
named `Result` and `ApiResult`. |
| 54 |
|
| 55 |
This means we need to convert from internal errors to public errors, which has |
| 56 |
the nice side benefit of giving us a centralized spot to make error reports for |
| 57 |
selected public errors. This is done with the `ErrorHandling` type and |
| 58 |
`GetErrorHandling` trait in `src/handling.rs`. The basic system is that you |
| 59 |
convert between one error to another and choose if you want to report the error |
| 60 |
and/or log a warning. When reporting an error you can choose a type name to |
| 61 |
group the error with. This system is extremely flexible, since you can inspect |
| 62 |
the internal error and use error codes or other data to determine if it should |
| 63 |
be reported or not, which type name to report it with, etc. Eventually we also |
| 64 |
hope to allow expected errors to be counted in telemetry (think things like |
| 65 |
network errors, shutdown errors, etc.). |
| 66 |
|
| 67 |
To assist this conversion, the `handle_error` procedural macro can be used to |
| 68 |
automatically convert between `Result` and `ApiResult` using |
| 69 |
`GetErrorHandling`. Note that this depends on having the `Result` type |
| 70 |
imported in your module with a `use` statement. |
| 71 |
|
| 72 |
See the `logins::errors` and `logins::store` modules for an example of how this |
| 73 |
all fits together. |
| 74 |
|
| 75 |
## ⚠️ Personally Identifiable Information ⚠️ |
| 76 |
|
| 77 |
When converting internal errors to public errors, we should ensure that there |
| 78 |
is no personally identifying information (PII) in any error reports. We should |
| 79 |
also ensure that no PII is contained in the public error enum, since consumers |
| 80 |
may end up uses those for their own error reports. |
| 81 |
|
| 82 |
We operate on a best-effort basis to ensure this. Our error details often come |
| 83 |
from an error from one of our dependencies, which makes it very difficult to be |
| 84 |
completely sure though. For example, `rusqlite::Error` could include data from |
| 85 |
a user's database in their errors, which would then appear in our error |
| 86 |
variants. However, we've never seen that in practice so we are comfortable |
| 87 |
including the `rusqlite` error message in our error reports, without attempting |
| 88 |
to sanitize them. |
| 89 |
|
| 90 |
# Logging support |
| 91 |
|
| 92 |
This crate also supplies macros for logging, like `trace!()`, `error!()` etc, |
| 93 |
which act just like the same macros from the `log` and `tracing` crates. Using |
| 94 |
these macros means you don't need to take additional dependencies, nor know exactly |
| 95 |
what logging system is in-place - it will arrange to be the correct logging system |
| 96 |
for the application the crate is being used in. |
| 97 |
|
| 98 |
Avoiding PII in logging is up to the crate doing the logging. In general, components |
| 99 |
might log PII (eg, URLs etc) at trace level. |