Initial commit: Complete NodeJS-native setup
- Migrated from Python pre-commit to NodeJS-native solution - Reorganized documentation structure - Set up Husky + lint-staged for efficient pre-commit hooks - Fixed Dockerfile healthcheck issue - Added comprehensive documentation index
This commit is contained in:
43
services/auth/auth.error.ts
Normal file
43
services/auth/auth.error.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextFunction, Request, Response } from 'express';
|
||||
|
||||
/**
|
||||
* Custom AuthError class that extends Error with HTTP status code
|
||||
* Security: Provides consistent error handling for authentication issues
|
||||
*/
|
||||
export class AuthError extends Error {
|
||||
statusCode: number;
|
||||
|
||||
constructor(message: string, statusCode: number = 401) {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
this.name = 'AuthError';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Middleware to handle AuthError exceptions
|
||||
* Security: Centralized error handling for authentication errors
|
||||
*/
|
||||
export const handleAuthError = (
|
||||
err: Error,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
if (err instanceof AuthError) {
|
||||
return res.status(err.statusCode).json({
|
||||
error: err.message,
|
||||
statusCode: err.statusCode,
|
||||
});
|
||||
}
|
||||
|
||||
// Handle JWT verification errors
|
||||
if (err.name === 'JsonWebTokenError' || err.name === 'TokenExpiredError') {
|
||||
return res.status(401).json({
|
||||
error: 'Invalid or expired token',
|
||||
statusCode: 401,
|
||||
});
|
||||
}
|
||||
|
||||
next(err);
|
||||
};
|
||||
Reference in New Issue
Block a user