The Safely Pattern
The Safely Pattern is a simple one. It allows you to tag non-critical code by wrapping it in a function. It’s built on top of exception handling and follows these rules:
- Raise exceptions in development and test environments
- Catch and report exceptions in other environments
Here’s a basic implementation in JavaScript:
function safely(nonCriticalCode) {
try {
nonCriticalCode();
} catch (e) {
if (env === "development" || env === "test") {
throw(e);
}
report(e);
}
}
Its advantages over typical exception handling are:
- It’s easier to write and debug code when errors aren’t caught in development and test environments
- It allows you to keep reporting DRY
It’s recommended to mark exceptions when reporting so it’s clear they were handled with this pattern. One way of doing this is to prefix the exception message with [safely]
.
There are currently implementations in Ruby and JavaScript.