Error handling
Authentify It implements a centralized and structured error handling strategy to ensure system resilience, developer observability, and client transparency.
General Methods
try/catch blocks are used in critical services to capture and gracefully handle unexpected errors.
Centralized exception filters in NestJS provide a unified mechanism to process, log, and format errors before sending them back to clients.
This guarantees that sensitive system details are never exposed, while still providing clients with actionable information.
Example: centralized exception filter in NestJS
import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus, Logger } from '@nestjs/common';
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
private readonly logger = new Logger(AllExceptionsFilter.name);
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
const message =
exception instanceof HttpException ? exception.getResponse() : exception;
// Log error internally (can be sent to Sentry, Slack, etc.)
this.logger.error(`Error occurred: ${JSON.stringify(message)}`);
// Return safe error response to client
response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
message,
});
}
}Error Types
Internal Errors (500)
Logged in detail.
Alerts sent to Sentry for real-time monitoring.
Sanitized response returned to clients to prevent information leakage.
Client Errors (400, 401, 403, etc.)
Handled explicitly within business logic and controllers.
Returned to clients with clear, descriptive messages (e.g., invalid input, unauthorized access).
Last updated