Exception Pipeline
Introduction
Section titled “Introduction”The ExceptionPipeline is a CQRS behavior that handles values thrown while
executing the next command or query behavior, including the handler.
It returns a failed Result for both existing AppError instances and other
thrown values. It does not itself write logs or create an HTTP response.
XenoAppBuilder.addPipeline() enables the CQRS module, which adds
ExceptionPipeline as a common behavior for commands and queries. You do not
configure it separately.
Enable the Exception Pipeline
Section titled “Enable the Exception Pipeline”Before using the CQRS pipeline, configure it through XenoAppBuilder:
import { XenoAppBuilder } from '@xeno-js/core'
const app = new XenoAppBuilder() .addPipeline()
await app.build()addPipeline() queues the CQRS module, which is initialized during build().
The Exception Pipeline is registered automatically as part of this configuration.
There is no separate setting such as:
config.exception = { enabled: true,}How exception handling works
Section titled “How exception handling works”When a command or query runs through the configured CQRS pipeline, the
ExceptionPipeline awaits the next behavior. This continues through the
remaining behaviors and the handler. If that execution throws, the behavior
converts the thrown value according to its type.
Existing AppError
Section titled “Existing AppError”If the thrown value is already an AppError, Xeno.JS preserves that error.
For example:
throw AppError.create({ code: 'USER_NOT_FOUND', message: 'User not found', status: 404, name: 'GetUserQuery',})The resulting Result.fail contains the same AppError instance.
This preserves the error instance and its application-defined metadata.
Other thrown values
Section titled “Other thrown values”If the thrown value is not an AppError (including non-Error values),
Xeno.JS creates a system error.
For example:
throw new Error('Unexpected database failure')The Exception Pipeline converts it into an AppError with:
- code:
SYSTEM_ERROR; - status:
500(STATUS_CODES.INTERNAL_SERVER_ERROR); - the CQRS request’s
intentas the error name; - the original thrown value as the
cause.
The original thrown value is therefore available as the cause of the
resulting AppError. The status is error metadata; this behavior does not
itself create or send an HTTP response.
Use AppError for expected application errors
Section titled “Use AppError for expected application errors”Use AppError when the error is part of the expected behavior of your application.
For example, a use case may reject an operation because a requested resource does not exist:
import { AppError } from '@xeno-js/shared'
export class GetUserHandler { async execute(userId: string) { const user = await this.repository.findById(userId)
if (!user) { throw AppError.create({ code: 'USER_NOT_FOUND', message: 'User not found', status: 404, name: 'GetUserQuery', }) }
return user }}The Exception Pipeline preserves this error instead of replacing it with a generic system error.
This means callers can distinguish application errors from unexpected failures.
Handle unexpected errors
Section titled “Handle unexpected errors”You do not need to wrap every unexpected exception manually.
For example:
export class CreateUserHandler { async execute(request: CreateUserRequest) { const user = await this.repository.create(request)
return user }}If an unexpected exception is thrown by the repository or another dependency:
throw new Error('Database connection failed')the Exception Pipeline converts it into a failed Result containing a
SYSTEM_ERROR with status 500.
The original thrown value is retained as the cause.
Result of exception handling
Section titled “Result of exception handling”When the next behavior or handler throws, the Exception Pipeline returns a
failed Result rather than propagating that thrown value from this pipeline
call.
Conceptually:
Command / Query │ ▼Application execution │ ├── success ───────────────► Result.ok(...) │ └── exception │ ├── AppError │ └─────────────► Result.fail(existing AppError) │ └── other thrown value └─────────────► Result.fail( AppError( SYSTEM_ERROR, status = 500, cause = original value ) )This gives command and query dispatch a consistent failed Result for
application errors and other thrown values that reach this behavior.
Throwing an AppError
Section titled “Throwing an AppError”When creating an application-specific error, provide a meaningful error code, message, status, and request name.
import { AppError } from '@xeno-js/shared'
throw AppError.create({ code: 'ORDER_ALREADY_CANCELLED', message: 'The order has already been cancelled.', status: 409, name: 'CancelOrderCommand',})The error remains an AppError when it reaches the caller.
Unexpected errors
Section titled “Unexpected errors”For unexpected errors, normal JavaScript errors can be thrown directly:
throw new Error('Unexpected failure')Xeno.JS converts the error into a system AppError.
You should not manually convert every unexpected exception into SYSTEM_ERROR. The Exception Pipeline performs this conversion for you.
Command and query execution
Section titled “Command and query execution”The CQRS module adds ExceptionPipeline as a common behavior at priority 0,
so it applies to both command and query pipelines. The logging behavior is
registered separately; exception conversion itself does not log.
This means the behavior is available to:
- commands;
- queries;
- their handlers;
- dependencies called while those handlers or subsequent behaviors execute.
You therefore do not need separate exception handling configuration for commands and queries.
For creating the request itself, see:
Troubleshooting
Section titled “Troubleshooting”Exceptions are not being converted to Result.fail
Section titled “Exceptions are not being converted to Result.fail”Check that the application has enabled the CQRS pipeline:
const app = new XenoAppBuilder() .addPipeline()
await app.build()Without addPipeline(), the CQRS pipeline module is not initialized.
My custom AppError becomes a SYSTEM_ERROR
Section titled “My custom AppError becomes a SYSTEM_ERROR”Make sure the value being thrown is an actual AppError:
throw AppError.create({ code: 'USER_NOT_FOUND', message: 'User not found', status: 404, name: 'GetUserQuery',})The Exception Pipeline preserves errors that are instances of AppError.
My thrown value produces status 500
Section titled “My thrown value produces status 500”If the thrown value is not an AppError, the Exception Pipeline converts it
to a system error with status 500.
If the error represents an expected application condition, create and throw an appropriate AppError instead.
For example:
throw AppError.create({ code: 'RESOURCE_NOT_FOUND', message: 'Resource not found', status: 404, name: 'GetResourceQuery',})I need to inspect the original thrown value
Section titled “I need to inspect the original thrown value”Unexpected thrown values are stored as the cause of the generated
AppError.
When handling the failed result, inspect the generated AppError and its
cause. Logging is provided separately by the configured logging behavior.
Related documentation
Section titled “Related documentation”- Application Overview
- CQRS Overview
- Creating Commands
- Creating Queries
- Creating Handlers
- Logging Pipeline
- Performance Pipeline
Support Us
Section titled “Support Us”Xeno.JS is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you’d like to join them, please read more at support section
