@nestjs/graphql#GqlExceptionFilter TypeScript Examples
The following examples show how to use
@nestjs/graphql#GqlExceptionFilter.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GqlResolverExceptions.filter.ts From amplication with Apache License 2.0 | 5 votes |
@Catch()
export class GqlResolverExceptionsFilter implements GqlExceptionFilter {
constructor(
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
private readonly configService: ConfigService
) {}
catch(exception: Error, host: ArgumentsHost): Error {
const requestData = this.prepareRequestData(host);
let clientError: Error;
/**@todo: Complete the list or expected error codes */
if (
exception instanceof Prisma.PrismaClientKnownRequestError &&
exception.code === PRISMA_CODE_UNIQUE_KEY_VIOLATION
) {
// Convert PrismaClientKnownRequestError to UniqueKeyException and pass the error to the client
const fields = (exception.meta as { target: string[] }).target;
clientError = new UniqueKeyException(fields);
this.logger.info(clientError.message, { requestData });
} else if (exception instanceof AmplicationError) {
// Convert AmplicationError to ApolloError and pass the error to the client
clientError = new ApolloError(exception.message);
this.logger.info(clientError.message, { requestData });
} else if (exception instanceof HttpException) {
// Return HTTP Exceptions to the client
clientError = exception;
this.logger.info(clientError.message, { requestData });
} else {
// Log the original exception and return a generic server error to client
// eslint-disable-next-line
// @ts-ignore
exception.requestData = requestData;
this.logger.error(exception);
clientError =
this.configService.get('NODE_ENV') === 'production'
? new InternalServerError()
: new ApolloError(exception.message);
}
return clientError;
}
prepareRequestData(host: ArgumentsHost): RequestData | null {
const { req } = GqlArgumentsHost.create(host).getContext();
return req ? createRequestData(req) : null;
}
}