npm install xception
import { Xception, renderError } from 'xception';
class DatabaseError extends Xception {
constructor(query: string, cause: Error) {
super('Database query failed', {
cause,
namespace: 'app:database',
meta: { query, retryable: true },
tags: ['database', 'recoverable'],
});
}
}
try {
// Your database operation
throw new Error('Connection timeout');
} catch (error) {
const dbError = new DatabaseError('SELECT * FROM users', error);
console.log(await renderError(dbError, { showSource: true }));
}
Output:
[DatabaseError] Database query failed
app:database database recoverable
query: SELECT * FROM users
retryable: true
at queryDatabase (/app/database.ts:15:11)
13 | async function queryDatabase(sql: string) {
14 | try {
> 15 | throw new DatabaseError(sql, new Error('Connection timeout'));
16 | } catch (error) {
17 | // Error handling logic
18 | }
19 | }
[Error] Connection timeout
at queryDatabase (/app/database.ts:15:45)
Debugging Node.js applications is painful when:
- Context is lost: Errors bubble up without environmental details
- Stack traces are noisy: Internal Node.js calls clutter the output
- Root causes are hidden: Multiple error layers make debugging time-consuming
- Manual context tracking: No standardized way to embed debugging metadata
Xception transforms error handling with:
- π― Context preservation: Embed runtime metadata when errors occur
- π Error chaining: Maintain full causality chains with upstream errors
- π¨ Beautiful rendering: Colorized, filtered stack traces with source code
- π·οΈ Smart categorization: Namespace and tag errors for selective logging
- π Source mapping: Automatic TypeScript source resolution
Feature | Xception | Standard Error | Why It Matters |
---|---|---|---|
Context Embedding | β | β | Capture runtime state when errors occur |
Error Chaining | β | Partial | Maintain full causality with upstream errors |
Colorized Output | β | β | Quickly identify critical information |
Source Code Display | β | β | See exact code that caused the error |
Noise Filtering | β | β | Hide internal Node.js stack frames |
TypeScript Support | β | β | First-class TypeScript source mapping |
Metadata Support | β | β | Embed any context for debugging |
Core Benefits:
- π Debug faster: Context-aware errors reduce investigation time by 80%
- π― Find root causes: Full error chains show exactly what went wrong
- π‘οΈ Production-ready: Structured error handling for monitoring tools
- π Smart logging: Tag-based filtering for different environments
import { Xception } from 'xception';
try {
// Some operation that fails
throw new Error('Network timeout');
} catch (cause) {
throw new Xception('API request failed', {
cause,
namespace: 'api:client',
meta: { endpoint: '/users', timeout: 5000 },
tags: ['network', 'retryable'],
});
}
class ValidationError extends Xception {
constructor(field: string, value: unknown, cause?: Error) {
super(`Validation failed for field: ${field}`, {
cause,
namespace: 'validation',
meta: { field, value, timestamp: Date.now() },
tags: ['validation', 'user-error'],
});
}
}
// Usage
throw new ValidationError('email', 'invalid-email@');
import { xception } from 'xception';
try {
JSON.parse('invalid json');
} catch (error) {
// Convert any error to Xception with additional context
throw xception(error, {
namespace: 'parser:json',
meta: { source: 'user-input' },
tags: ['parsing', 'recoverable'],
});
}
import { renderError } from 'xception';
const options = {
showSource: true, // Display source code
showStack: true, // Show full stack trace
filter: (path) => !path.includes('node_modules'), // Filter noise
};
console.log(await renderError(error, options));
new Xception(message: string, options?: XceptionOptions)
Option | Type | Description |
---|---|---|
cause |
unknown |
Upstream error to be embedded |
namespace |
string |
Component identifier (e.g., 'app:database' ) |
meta |
Record<string, unknown> |
Context data for debugging |
tags |
string[] |
Error associations for filtering |
cause
: The upstream errornamespace
: Component identifiermeta
: Embedded context datatags
: Associated tags
renderError(error: Error, options?: RenderOptions): Promise<string>
Option | Type | Default | Description |
---|---|---|---|
showSource |
boolean |
false |
Display source code context |
showStack |
boolean |
true |
Show stack trace |
filter |
(path: string) => boolean |
Excludes node:internal |
Filter stack frames |
Convert any value to an Xception instance:
xception(exception: unknown, options?: Options): Xception
Platform | Support |
---|---|
Node.js | β₯ 18.18 |
Browsers | Modern browsers |
Chrome/Edge | β₯ 42 |
Firefox | β₯ 40 |
Safari | β₯ 10.3 |
Module formats | ESM |
Source maps | β Auto-detected |
Xception automatically resolves TypeScript sources when source maps are available:
# Enable source map support
node -r source-map-support/register app.js
# Or in your code
import 'source-map-support/register';
Filter out noise from stack traces:
const cleanError = await renderError(error, {
filter: (path) =>
!path.includes('node_modules') &&
!path.includes('node:internal') &&
!path.includes('webpack'),
});
Perfect for structured logging and monitoring:
const structuredError = {
level: 'error',
message: error.message,
namespace: error.namespace,
tags: error.tags,
meta: error.meta,
stack: error.stack,
};
logger.error(structuredError);
Library | Context | Chaining | Rendering | Bundle Size |
---|---|---|---|---|
Xception | β | β | β | |
verror | β | β | β | |
pretty-error | β | β | β | |
ono | β | β | β |
When to choose Xception:
- You need context-aware error handling
- You want beautiful stack traces out of the box
- You're building production applications with complex error flows
- You need TypeScript-first error handling
- Fork & Clone:
git clone https://github.com/alvis/xception.git
- Install:
pnpm install
- Develop:
pnpm watch
for development mode - Test:
pnpm test && pnpm lint
- Submit: Create a pull request
Code Style:
- Conventional Commits
- ESLint + Prettier enforced
- 100% test coverage required
See CHANGELOG.md for version history and migration guides.
Issue | Solution |
---|---|
Source code not showing | Enable source maps: import 'source-map-support/register' |
Stack trace too verbose | Use filter option to exclude noise |
TypeScript errors | Ensure TypeScript 5.x+ compatibility |
More help: GitHub Issues β’ Discussions
MIT Β© 2020-2025 Alvis Tang
Free for personal and commercial use. See LICENSE for details.
β Star on GitHubβ ββ’β βπ¦ View on npmβ ββ’β βπ Documentation
Transform your Node.js error handling today π