chore: strengthen backend eslint profile
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { ArgumentsHost } from '@nestjs/common';
|
||||
|
||||
import { HttpExceptionFilter } from './http-exception.filter';
|
||||
|
||||
describe('HttpExceptionFilter', () => {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { Response } from 'express';
|
||||
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch,
|
||||
@@ -6,7 +8,6 @@ import {
|
||||
HttpStatus,
|
||||
ValidationError,
|
||||
} from '@nestjs/common';
|
||||
import type { Response } from 'express';
|
||||
|
||||
@Catch()
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
@@ -14,32 +15,33 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
const context = host.switchToHttp();
|
||||
const response = context.getResponse<Response>();
|
||||
const status =
|
||||
exception instanceof HttpException
|
||||
? exception.getStatus()
|
||||
: HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
exception instanceof HttpException ?
|
||||
exception.getStatus() :
|
||||
HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
|
||||
const payload =
|
||||
exception instanceof HttpException ? exception.getResponse() : undefined;
|
||||
const isValidation = Array.isArray(
|
||||
(payload as { message?: unknown })?.message,
|
||||
);
|
||||
const code = isValidation
|
||||
? 'VALIDATION_ERROR'
|
||||
: this.codeFor(status, payload);
|
||||
(response.locals ??= {}).publicErrorCode = code;
|
||||
const code = isValidation ?
|
||||
'VALIDATION_ERROR' :
|
||||
this.codeFor(status, payload);
|
||||
response.locals ??= {};
|
||||
response.locals.publicErrorCode = code;
|
||||
response
|
||||
.status(status)
|
||||
.type('application/problem+json')
|
||||
.json({
|
||||
status,
|
||||
code,
|
||||
...(isValidation
|
||||
? {
|
||||
...(isValidation ?
|
||||
{
|
||||
errors: this.validationErrors(
|
||||
(payload as { message: ValidationError[] }).message,
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
} :
|
||||
{}),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -49,6 +51,7 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
if (typeof code === 'string') return code;
|
||||
}
|
||||
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',
|
||||
@@ -62,7 +65,7 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
private validationErrors(
|
||||
errors: ValidationError[],
|
||||
prefix = '#',
|
||||
): { pointer: string; detail: string }[] {
|
||||
): { detail: string; pointer: string }[] {
|
||||
return errors.flatMap((error) => {
|
||||
if (!error || typeof error.property !== 'string') return [];
|
||||
const pointer = `${prefix}/${error.property.replaceAll('~', '~0').replaceAll('/', '~1')}`;
|
||||
|
||||
Reference in New Issue
Block a user