Error Handling Guide
Throwing Errors
@Tool({ name: 'get_user' })
async getUser(input: any) {
const user = await this.userService.findById(input.user_id);
if (!user) {
throw new Error('User not found');
}
return user;
}
Custom Errors
export class UserNotFoundError extends Error {
constructor(userId: string) {
super(\`User \${userId} not found\`);
this.name = 'UserNotFoundError';
}
}
@Tool({ name: 'get_user' })
async getUser(input: any) {
const user = await this.userService.findById(input.user_id);
if (!user) {
throw new UserNotFoundError(input.user_id);
}
return user;
}
Exception Filters
@ExceptionFilter()
export class HttpExceptionFilter {
catch(exception: any, context: ExecutionContext) {
return {
statusCode: exception.status || 500,
message: exception.message,
timestamp: new Date().toISOString()
};
}
}
@Tool({ name: 'risky_operation' })
@UseFilters(HttpExceptionFilter)
async riskyOperation(input: any) {
}
Best Practices
- Be specific - Use custom error classes
- Log errors - Use context.logger.error()
- Don't swallow - Always re-throw or handle
- User-friendly messages - Don't expose internals
- Use filters - Centralized error handling
Next Steps