Result vs throw
You may have heard the argument: "JavaScript can throw anywhere, and it's impossible to manage every error with Result, so there's no point in using Result at all."
We disagree. The key is that a Result only needs to represent anticipated errors.
There is no need to wrap every possible error in a Result.
Anticipated vs Unexpected Errors
Whether an error belongs in a Result or should be thrown depends on its nature.
Anticipated Errors: Use Result
Anticipated errors are part of your application's business logic.
They are expected to happen during normal operation, and the caller can respond to them meaningfully, for example by showing a message to the user or returning a 404 response:
- The requested post does not exist
- The user doesn't have permission to perform the action
- The submitted form contains invalid values
Represent these errors in the return type so that callers are forced to handle them:
Unexpected Errors: Let Them Throw
Unexpected errors come from the infrastructure or from bugs. The caller usually can't do anything about them other than report them:
- Database connection failures
- Network timeouts
- Out-of-memory errors
- Programming errors, such as reading a property of
undefined
Let these errors be thrown, and handle them in one place at the top level, such as a framework's error handler or an error monitoring service like Sentry:
When in doubt, ask yourself: "Will the caller handle this error differently from any other error?"
If the answer is yes, it is an anticipated error and belongs in a Result.
Converting Between the Two
In practice, the two kinds of errors meet at the boundaries of your code:
- From throw to
Result: When a library signals an anticipated failure by throwing, such asJSON.parseon user input, wrap it withResult.fnorResult.try. - From
Resultto throw: At the entry point of your application, handle the errors you expect and letResult.unwrapthrow the rest, so that your error monitoring can pick them up.
Wrapping Unexpected Errors for Better Stack Traces
Sometimes the stack trace of an unexpected error isn't helpful, because it points deep into a library's internals.
In that case, you can wrap the operation with Result.fn and convert any thrown error into a single UnexpectedError class.
First, define the error class:
For more details about @praha/error-factory, see Custom Error.
Then wrap the operation, keeping the original error as cause:
This approach has the following benefits:
- Readable stack traces: The new error is created in your own code, so its stack trace shows where the failure entered your application.
- Context: You can attach information that helps with debugging, such as the arguments of the operation.
- Nothing is lost: The original error remains accessible through the
causeproperty.
At the entry point of your application, you can still unwrap these errors to rethrow them to your error monitoring.
Conclusion
The goal is not to replace every throw with a Result, but to use each where it fits best.
Result excels at anticipated, business-level errors that require explicit handling, while throw remains the right tool for unexpected errors that should be handled at the infrastructure level.
This combination gives you explicit error handling where it matters most, without the burden of wrapping every possible error in your application.
