refactor: enforce enum machine codes
Use owner enums for error and T-Bank protocol values with ESLint regression checks. Preserve open wire fallbacks and keep protocol imports independent of DI and Prisma. Verified npm ci, verify, 226 unit tests, 262 isolated e2e tests and unchanged OpenAPI.
This commit is contained in:
@@ -7,11 +7,13 @@ import {
|
||||
getSchemaPath,
|
||||
} from '@nestjs/swagger';
|
||||
|
||||
import { HttpErrorCode } from '../error-code';
|
||||
|
||||
export class ProblemDetailsDto {
|
||||
@ApiProperty({ example: 401 })
|
||||
declare status: number;
|
||||
|
||||
@ApiProperty({ example: 'UNAUTHORIZED' })
|
||||
@ApiProperty({ example: HttpErrorCode.Unauthorized })
|
||||
declare code: string;
|
||||
|
||||
// eslint-disable-next-line @darraghor/nestjs-typed/api-property-returning-array-should-set-array -- Raw OpenAPI items schema already declares type: array.
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
export enum HttpErrorCode {
|
||||
BadRequest = 'BAD_REQUEST',
|
||||
InternalServerError = 'INTERNAL_SERVER_ERROR',
|
||||
NotFound = 'NOT_FOUND',
|
||||
Unauthorized = 'UNAUTHORIZED',
|
||||
UnsupportedMediaType = 'UNSUPPORTED_MEDIA_TYPE',
|
||||
ValidationError = 'VALIDATION_ERROR',
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import {
|
||||
ValidationError,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { HttpErrorCode } from '../error-code';
|
||||
|
||||
@Catch()
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
catch(exception: unknown, host: ArgumentsHost): void {
|
||||
@@ -25,7 +27,7 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
(payload as { message?: unknown })?.message,
|
||||
);
|
||||
const code = isValidation
|
||||
? 'VALIDATION_ERROR'
|
||||
? HttpErrorCode.ValidationError
|
||||
: this.codeFor(status, payload);
|
||||
response.locals ??= {};
|
||||
response.locals.publicErrorCode = code;
|
||||
@@ -53,12 +55,12 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
return (
|
||||
// eslint-disable-next-line security/detect-object-injection -- The lookup key is a numeric HTTP status, never a property name from the request.
|
||||
{
|
||||
400: 'BAD_REQUEST',
|
||||
401: 'UNAUTHORIZED',
|
||||
404: 'NOT_FOUND',
|
||||
415: 'UNSUPPORTED_MEDIA_TYPE',
|
||||
500: 'INTERNAL_SERVER_ERROR',
|
||||
}[status] ?? 'INTERNAL_SERVER_ERROR'
|
||||
400: HttpErrorCode.BadRequest,
|
||||
401: HttpErrorCode.Unauthorized,
|
||||
404: HttpErrorCode.NotFound,
|
||||
415: HttpErrorCode.UnsupportedMediaType,
|
||||
500: HttpErrorCode.InternalServerError,
|
||||
}[status] ?? HttpErrorCode.InternalServerError
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user