This commit is contained in:
2025-11-21 17:19:30 +03:30
parent a8f2f4b50c
commit e2a04e6a50
+30 -1
View File
@@ -1,11 +1,36 @@
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus } from '@nestjs/common'; import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus, ExecutionContext } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { FastifyReply } from 'fastify'; import { FastifyReply } from 'fastify';
@Catch(HttpException) @Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter { export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) { catch(exception: HttpException, host: ArgumentsHost) {
// Check if this is a GraphQL context
if (host.getType<'graphql' | 'http'>() === 'graphql') {
try {
// In GraphQL context, host is actually an ExecutionContext
const gqlContext = GqlExecutionContext.create(host as ExecutionContext);
if (gqlContext && gqlContext.getInfo()) {
console.log('GraphQL context**');
// For GraphQL, let the exception propagate - GraphQL will handle it automatically
// We can't use HTTP response methods in GraphQL context
return;
}
} catch {
// Not a GraphQL context, continue with HTTP handling
}
}
// Handle HTTP/REST context
try {
const ctx = host.switchToHttp(); const ctx = host.switchToHttp();
const reply = ctx.getResponse<FastifyReply>(); const reply = ctx.getResponse<FastifyReply>();
// Safety check: ensure reply has the status method
if (!reply || typeof reply.status !== 'function') {
return;
}
// const request = ctx.getRequest<FastifyRequest>(); // const request = ctx.getRequest<FastifyRequest>();
const status = exception.getStatus() || HttpStatus.INTERNAL_SERVER_ERROR; const status = exception.getStatus() || HttpStatus.INTERNAL_SERVER_ERROR;
@@ -21,6 +46,10 @@ export class HttpExceptionFilter implements ExceptionFilter {
// path: request.url, // path: request.url,
}, },
}); });
} catch {
// If we can't handle it, let it propagate
return;
}
} }
private extractMessage(response: string | Record<string, any>): string | string[] { private extractMessage(response: string | Record<string, any>): string | string[] {