Kind: Interface
Source: packages/common/exceptions/http.exception.ts
Part of: Common
HttpExceptionOptions defines optional metadata that can be attached to an HTTP exception. It provides a human-readable description and an underlying cause value, helping applications preserve diagnostic context when errors are created and handled.
Properties
| Property | Type |
|---|---|
cause | unknown |
description | string |
Diagram
mermaidgraph LR A[Application error] --> B[HttpExceptionOptions] B --> C[cause: unknown] B --> D[description: string] B --> E[HttpException] E --> F[Exception handler / HTTP response]
Usage
tsimport { HttpException, HttpStatus } from '@nestjs/common';
function findUser(userId: string) {
try {
// Example operation that may fail
throw new Error(`User ${userId} was not found`);
} catch (error) {
throw new HttpException(
'Unable to retrieve user',
HttpStatus.NOT_FOUND,
{
description: 'The requested user does not exist or cannot be accessed.',
cause: error,
},
);
}
}
AI Coding Instructions
- Pass
HttpExceptionOptionsas the third argument when constructing anHttpException. - Use
descriptionfor stable, developer-facing context; do not rely on it as the client response message. - Preserve the original thrown value in
causeto support logging, tracing, and error debugging. - Treat
causeasunknown; narrow its type before accessing properties such asmessageorstack. - Avoid placing sensitive data, credentials, or internal implementation details in exception descriptions.
Was this page helpful?