@sisu-ai/mw-error-boundary
v10.0.0
Published
Prevent pipeline crashes by catching middleware errors and returning controlled fallbacks.
Maintainers
Readme
@sisu-ai/mw-error-boundary
Prevent pipeline crashes by catching middleware errors and returning controlled fallbacks.
Setup
npm i @sisu-ai/mw-error-boundaryExports
errorBoundary(onError: (err, ctx, next) => Promise<void>)- Call early in your stack to ensure all downstream errors are caught.
- Inside
onError, you typically:- log the error (
ctx.log.error(err)), - push a friendly assistant message to
ctx.messages, - optionally write to
ctx.streamif you’re mid‑stream.
- log the error (
What It Does
- Wraps the rest of your pipeline in a
try/catch. - Invokes your handler with the error and context, so you can log and produce a user‑friendly fallback.
- Prevents crashes from bubbling to the caller while keeping control in your app.
How It Works
errorBoundary(onError) returns middleware that does:
try {
await next();
} catch (err) {
await onError(err, ctx, async () => {}); // next is a no-op in error state
}Your onError receives (err, ctx, next) where next is a no‑op to signal the boundary is terminal for that request.
Usage
import { Agent } from '@sisu-ai/core';
import { errorBoundary } from '@sisu-ai/mw-error-boundary';
const app = new Agent()
.use(errorBoundary(async (err, ctx) => {
ctx.log.error(err);
// If streaming UI, consider writing to the stream instead/as well.
ctx.messages.push({ role: 'assistant', content: 'Sorry, something went wrong.' });
}))
// ... other middlewarePlacement & Ordering
- Put
errorBoundaryat or near the top to catch as much as possible. - Combine with tracing/usage middleware as needed; if placed before them, those middlewares may not observe the error.
- If you use a server adapter, ensure it wraps request handling so per‑request errors are isolated.
When To Use
- Any production app to avoid unhandled rejections crashing the process.
- CLIs and demos where you want graceful failure and a helpful message.
- Around tool‑calling loops where third‑party tools may throw.
When Not To Use
- If you want errors to propagate to an outer boundary (e.g., framework handler) and be handled there.
- Highly controlled test scenarios where you prefer tests to fail fast instead of being swallowed.
Notes & Gotchas
- The boundary swallows the error after
onErrorruns; rethrow insideonErrorif you want upstream handling. - Be careful not to leak secrets when logging errors — consider the redacting logger
createRedactingLoggerfrom@sisu-ai/core. - If you are streaming tokens, write an error notice to
ctx.streamand callctx.stream.end()to close the client stream cleanly. - Keep
onErrorfast and robust; avoid throwing inside it.
Community & Support
Discover what you can do through examples or documentation. Check it out at https://github.com/finger-gun/sisu. Example projects live under examples/ in the repo.
Documentation
Core — Package docs · Error types
Adapters — OpenAI · Anthropic · Ollama
- @sisu-ai/mw-agent-run-api
- @sisu-ai/mw-context-compressor
- @sisu-ai/mw-control-flow
- @sisu-ai/mw-conversation-buffer
- @sisu-ai/mw-cors
- @sisu-ai/mw-error-boundary
- @sisu-ai/mw-guardrails
- @sisu-ai/mw-invariants
- @sisu-ai/mw-orchestration
- @sisu-ai/mw-rag
- @sisu-ai/mw-react-parser
- @sisu-ai/mw-register-tools
- @sisu-ai/mw-tool-calling
- @sisu-ai/mw-trace-viewer
- @sisu-ai/mw-usage-tracker
- @sisu-ai/tool-aws-s3
- @sisu-ai/tool-azure-blob
- @sisu-ai/tool-extract-urls
- @sisu-ai/tool-github-projects
- @sisu-ai/tool-rag
- @sisu-ai/tool-summarize-text
- @sisu-ai/tool-terminal
- @sisu-ai/tool-web-fetch
- @sisu-ai/tool-web-search-duckduckgo
- @sisu-ai/tool-web-search-google
- @sisu-ai/tool-web-search-openai
- @sisu-ai/tool-wikipedia
Anthropic — hello · control-flow · stream · weather
Ollama — hello · stream · vision · weather · web-search
OpenAI — hello · weather · stream · vision · reasoning · react · control-flow · branch · parallel · graph · orchestration · orchestration-adaptive · guardrails · error-handling · rag-chroma · web-search · web-fetch · wikipedia · terminal · github-projects · server · aws-s3 · azure-blob
Contributing
We build Sisu in the open. Contributions welcome.
Contributing Guide · Report a Bug · Request a Feature · Code of Conduct
Star on GitHub if Sisu helps you build better agents.
Quiet, determined, relentlessly useful.
