bug
This commit is contained in:
@@ -1,26 +1,55 @@
|
|||||||
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) {
|
||||||
const ctx = host.switchToHttp();
|
// Check if this is a GraphQL context
|
||||||
const reply = ctx.getResponse<FastifyReply>();
|
if (host.getType<'graphql' | 'http'>() === 'graphql') {
|
||||||
// const request = ctx.getRequest<FastifyRequest>();
|
try {
|
||||||
const status = exception.getStatus() || HttpStatus.INTERNAL_SERVER_ERROR;
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const exceptionResponse = exception.getResponse();
|
// Handle HTTP/REST context
|
||||||
const message = this.extractMessage(exceptionResponse);
|
try {
|
||||||
|
const ctx = host.switchToHttp();
|
||||||
|
const reply = ctx.getResponse<FastifyReply>();
|
||||||
|
|
||||||
reply.status(status).send({
|
// Safety check: ensure reply has the status method
|
||||||
statusCode: status,
|
if (!reply || typeof reply.status !== 'function') {
|
||||||
success: false,
|
return;
|
||||||
error: {
|
}
|
||||||
message,
|
|
||||||
// timestamp: new Date().toISOString(),
|
// const request = ctx.getRequest<FastifyRequest>();
|
||||||
// path: request.url,
|
const status = exception.getStatus() || HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
},
|
|
||||||
});
|
const exceptionResponse = exception.getResponse();
|
||||||
|
const message = this.extractMessage(exceptionResponse);
|
||||||
|
|
||||||
|
reply.status(status).send({
|
||||||
|
statusCode: status,
|
||||||
|
success: false,
|
||||||
|
error: {
|
||||||
|
message,
|
||||||
|
// timestamp: new Date().toISOString(),
|
||||||
|
// 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[] {
|
||||||
|
|||||||
Reference in New Issue
Block a user