Explorar el Código

fix: enhance error handling in AuthService and HttpExceptionFilter

Dave hace 5 horas
padre
commit
d439b5c55f

+ 11 - 2
apps/box-mgnt-api/src/mgnt-backend/core/auth/auth.service.ts

@@ -3,6 +3,8 @@ import {
   Injectable,
   UnauthorizedException,
   Logger,
+  HttpException,
+  HttpStatus,
 } from '@nestjs/common';
 import { JwtService } from '@nestjs/jwt';
 import { LoginStatus, LoginType, User } from '@prisma/mysql/client';
@@ -242,8 +244,15 @@ export class AuthService {
 
     if (!matchesMgmt && !matchesOAuth) {
       this.logger.log('Token mismatch');
-      throw new UnauthorizedException(
-        'Your session has been kicked out or expired.',
+
+      throw new HttpException(
+        {
+          statusCode: 401,
+          message: `Your session has been kicked out or expired.`,
+          code: 'SESSION_KICKED_OUT',
+          retryAfter: 0,
+        },
+        HttpStatus.UNAUTHORIZED,
       );
     }
 

+ 6 - 2
libs/common/src/filters/http-exception.filter.ts

@@ -27,6 +27,7 @@ export class HttpExceptionFilter implements ExceptionFilter {
       exception instanceof HttpException ? exception.getResponse() : null;
 
     let message: string;
+    let code: string;
     if (typeof exceptionResponse === 'object' && exceptionResponse !== null) {
       const responseObj = exceptionResponse as any;
       if (Array.isArray(responseObj.message)) {
@@ -36,17 +37,20 @@ export class HttpExceptionFilter implements ExceptionFilter {
       } else {
         message = (exception as Error).message || 'Unknown error';
       }
+      // Extract custom code if provided in exception
+      code = responseObj.code || this.mapStatusToCode(status);
     } else {
       message =
         exception instanceof Error
           ? exception.message
           : 'Internal server error';
+      code = this.mapStatusToCode(status);
     }
 
     const apiResponse: ApiResponse<null> = {
       error: message,
       status: 0,
-      code: this.mapStatusToCode(status),
+      code,
       data: null,
       timestamp: new Date().toISOString(),
     };
@@ -63,7 +67,7 @@ export class HttpExceptionFilter implements ExceptionFilter {
       );
     }
 
-    response.status(status).send(apiResponse);
+    response.status(200).send(apiResponse);
   }
 
   private mapStatusToCode(status: number): string {