cr
This commit is contained in:
@@ -5,10 +5,10 @@ import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Logger,
|
||||
} from '@nestjs/common';
|
||||
import { Request, Response } from 'express';
|
||||
import { I18nService, I18nContext } from 'nestjs-i18n';
|
||||
import { ApiResponse, createErrorResponse } from '../types/api-response.type';
|
||||
} from "@nestjs/common";
|
||||
import { Request, Response } from "express";
|
||||
import { I18nService, I18nContext } from "nestjs-i18n";
|
||||
import { ApiResponse, createErrorResponse } from "../types/api-response.type";
|
||||
|
||||
/**
|
||||
* Global exception filter that catches all exceptions
|
||||
@@ -27,23 +27,23 @@ export class GlobalExceptionFilter implements ExceptionFilter {
|
||||
|
||||
// Determine status and message
|
||||
let status = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
let message = 'Internal server error';
|
||||
let message = "Internal server error";
|
||||
let errors: string[] = [];
|
||||
|
||||
if (exception instanceof HttpException) {
|
||||
status = exception.getStatus();
|
||||
const exceptionResponse = exception.getResponse();
|
||||
|
||||
if (typeof exceptionResponse === 'string') {
|
||||
if (typeof exceptionResponse === "string") {
|
||||
message = exceptionResponse;
|
||||
} else if (typeof exceptionResponse === 'object') {
|
||||
} else if (typeof exceptionResponse === "object") {
|
||||
const responseObj = exceptionResponse as Record<string, unknown>;
|
||||
message = (responseObj.message as string) || exception.message;
|
||||
|
||||
// Handle validation errors (class-validator)
|
||||
if (Array.isArray(responseObj.message)) {
|
||||
errors = responseObj.message as string[];
|
||||
message = 'VALIDATION_FAILED';
|
||||
message = "VALIDATION_FAILED";
|
||||
}
|
||||
}
|
||||
} else if (exception instanceof Error) {
|
||||
@@ -57,22 +57,22 @@ export class GlobalExceptionFilter implements ExceptionFilter {
|
||||
let lang = i18nContext?.lang;
|
||||
|
||||
if (!lang) {
|
||||
const acceptLanguage = request.headers['accept-language'];
|
||||
const xLang = request.headers['x-lang'];
|
||||
const acceptLanguage = request.headers["accept-language"];
|
||||
const xLang = request.headers["x-lang"];
|
||||
|
||||
if (xLang) {
|
||||
lang = Array.isArray(xLang) ? xLang[0] : xLang;
|
||||
} else if (acceptLanguage) {
|
||||
// Take first preferred language: "tr-TR,en;q=0.9" -> "tr"
|
||||
lang = acceptLanguage.split(',')[0].split(';')[0].split('-')[0];
|
||||
lang = acceptLanguage.split(",")[0].split(";")[0].split("-")[0];
|
||||
}
|
||||
}
|
||||
|
||||
lang = lang || 'en';
|
||||
lang = lang || "en";
|
||||
|
||||
// Translate validation error specially
|
||||
if (message === 'VALIDATION_FAILED') {
|
||||
message = this.i18n.translate('errors.VALIDATION_FAILED', { lang });
|
||||
if (message === "VALIDATION_FAILED") {
|
||||
message = this.i18n.translate("errors.VALIDATION_FAILED", { lang });
|
||||
} else {
|
||||
// Try dynamic translation
|
||||
const translatedMessage = this.i18n.translate(`errors.${message}`, {
|
||||
@@ -95,7 +95,7 @@ export class GlobalExceptionFilter implements ExceptionFilter {
|
||||
);
|
||||
|
||||
// Build response
|
||||
const isDevelopment = process.env.NODE_ENV === 'development';
|
||||
const isDevelopment = process.env.NODE_ENV === "development";
|
||||
const errorResponse: ApiResponse<null> = createErrorResponse(
|
||||
message,
|
||||
status,
|
||||
|
||||
Reference in New Issue
Block a user