Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dmmulroy/better-result/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Panic represents an unrecoverable error that indicates a defect in user code, not a expected failure condition. When thrown, a Panic indicates that something fundamentally wrong has occurred, such as:
- A callback throwing inside a Result combinator
- Generator cleanup (finally blocks) throwing errors
- Resource disposal (Symbol.dispose/asyncDispose) failing
- Other violations of the Result abstraction’s invariants
Class Definition
Panic is a TaggedError with tag "Panic" and required message property.
Properties
Discriminator tag, always
"Panic".Description of what went wrong.
Optional underlying cause of the panic (the original thrown value).
Error name, set to
"Panic".Stack trace. If
cause is an Error, includes “Caused by:” chain.Creating Panics
Constructor
panic() Function
Helper function that creates and throws a Panic:When Panics Occur
Combinator Callbacks Throwing
When user callbacks in Result methods throw errors:Generator Cleanup Throwing
When finally blocks throw during Result.gen execution:Panics from cleanup code indicate resource disposal failures - serious bugs that must be fixed.
Success Path Cleanup Throwing
Cleanup throwing even when the operation succeeded:Result.try catch Handler Throwing
When the custom error handler itself throws:Generator Body Throwing Directly
When generator code throws before any yield:Resource Disposal Throwing
When Symbol.dispose or Symbol.asyncDispose throws:shouldRetry Predicate Throwing
When retry logic throws:Type Guard
Error Messages
Panic errors include descriptive messages indicating what operation failed:| Panic Message | Cause |
|---|---|
"map callback threw" | User callback in .map() threw |
"andThen callback threw" | User callback in .andThen() threw |
"generator cleanup threw" | Finally block threw during cleanup |
"generator body threw" | Generator threw before yielding |
"Result.try catch handler threw" | Custom catch handler threw |
"shouldRetry predicate threw" | Retry predicate threw |
"Unreachable: Err yielded in Result.gen but generator continued" | Internal invariant violation |
Stack Traces
Panic errors include full stack traces with cause chaining:JSON Serialization
Handling Strategy
What to Do When Panic Occurs
- Let it crash: Allow the Panic to propagate and crash the operation
- Fix the bug: The Panic indicates a defect in your code
- Log and alert: In production, log Panics for investigation
Production Error Monitoring
Log Panics for monitoring, but don’t try to recover:Panic vs Expected Errors
| Scenario | Use | Example |
|---|---|---|
| User provides invalid input | Result.err() | Validation failure |
| External service unavailable | Result.err() | Network timeout |
| Callback throws inside map | Panic | Bug in callback |
| Cleanup code throws | Panic | Resource disposal bug |
| Type guard fails | Panic | Invariant violation |
Best Practices
-
Never catch Panics: Let them crash and fix the underlying bug
-
Don’t throw in callbacks: Use
Result.err()for expected failures -
Test cleanup code: Ensure finally blocks and disposal don’t throw
-
Validate assumptions: Use type guards and assertions
See Also
- UnhandledException - For expected exceptions from Result.try
- TaggedError - Base class for Panic
- Result.try - Catching expected exceptions
- Result.gen - Generator-based composition