Error Handling
Vestig provides robust utilities for handling and logging errors safely. These utilities support ES2022 error cause chains, extract common error properties, and prevent sensitive information from leaking into logs.
Overview
When logging errors in production, you need to:
- Serialize safely - Avoid circular references and non-serializable data
- Preserve context - Keep error names, messages, and stack traces
- Handle cause chains - Support ES2022
causeproperty - Extract metadata - Capture
code,statusCode, and system error properties
Vestig handles all of this automatically when you pass errors to log methods.
Basic Error Logging
Output:
Error Utilities
serializeError
Convert any error to a safe, serializable format:
Handles many input types:
isError
Check if a value is an Error or error-like object:
getErrorMessage
Extract the error message from any value:
Error Cause Chains (ES2022)
Vestig fully supports ES2022 error cause chains:
The cause chain is preserved in serialization:
Nested Cause Chains
Depth Limit
To prevent infinite loops with circular references, cause chains are limited to 10 levels:
System Error Properties
Vestig automatically extracts common error properties:
Output:
Extracted Properties
| Property | Type | Description |
|---|---|---|
code |
string | number |
Error code (e.g., 'ENOENT', 'ETIMEDOUT') |
statusCode |
number |
HTTP status code |
status |
number |
Alternative HTTP status |
errno |
number |
System error number |
syscall |
string |
System call that failed |
path |
string |
File path for FS errors |
address |
string |
Network address |
port |
number |
Network port |
HTTP Error Handling
Output:
Error Boundaries (React)
With @vestig/next, use the error boundary component:
Best Practices
1. Always Wrap Unknown Errors
2. Use Appropriate Log Levels
3. Include Context
4. Handle Rejected Promises
5. Don't Log Sensitive Data
API Reference
Types
Functions
| Function | Description |
|---|---|
serializeError(error, depth?) |
Serialize error with cause chain |
isError(value) |
Check if value is error-like |
getErrorMessage(error) |
Extract message from any value |
Integration with Transports
All Vestig transports receive serialized errors: