Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): catch prisma error globally #87

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wet-yaks-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@snipcode/backend': patch
---

Catch Prisma technical error and return user friendly message
45 changes: 42 additions & 3 deletions apps/backend/src/configs/exception.filter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import { ArgumentsHost, Catch } from '@nestjs/common';
import { AbstractHttpAdapter, BaseExceptionFilter } from '@nestjs/core';
import { GqlArgumentsHost, GqlContextType } from '@nestjs/graphql';
import {
PrismaClientInitializationError,
PrismaClientKnownRequestError,
PrismaClientRustPanicError,
PrismaClientUnknownRequestError,
PrismaClientValidationError,
} from '@prisma/client/runtime/library';
import { isAppError } from '@snipcode/utils';
import { Response } from 'express';
import { GraphQLError } from 'graphql';

import { INTERNAL_SERVER_ERROR } from '../utils/constants';

type PrismaError =
| PrismaClientInitializationError
| PrismaClientKnownRequestError
| PrismaClientRustPanicError
| PrismaClientUnknownRequestError
| PrismaClientValidationError;

const isPrismaError = (error: unknown): error is PrismaError => {
return (
error instanceof PrismaClientInitializationError ||
error instanceof PrismaClientKnownRequestError ||
error instanceof PrismaClientRustPanicError ||
error instanceof PrismaClientUnknownRequestError ||
error instanceof PrismaClientValidationError
);
};

@Catch()
export class ApplicationExceptionFilter extends BaseExceptionFilter {
constructor(httpAdapter: AbstractHttpAdapter) {
Expand All @@ -22,18 +48,31 @@ export class ApplicationExceptionFilter extends BaseExceptionFilter {
},
originalError: exception,
});
} else if (isPrismaError(exception)) {
throw new GraphQLError(INTERNAL_SERVER_ERROR, {
extensions: {
code: 'DATABASE_ERROR',
},
});
}
} else {
if (isAppError(exception)) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();

if (isAppError(exception)) {
response.status(400).json({
code: exception.code,
message: exception.message,
timestamp: new Date().toISOString(),
});
} else if (isPrismaError(exception)) {
response.status(500).json({
code: 'DATABASE_ERROR',
message: INTERNAL_SERVER_ERROR,
timestamp: new Date().toISOString(),
});
}

super.catch(exception, host);
}
}
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const AUTH_USER_NOT_FOUND_CODE = 'AUTH_USER_NOT_FOUND';
export const AUTH_SUCCESS_URL = (webAuthSuccessUrl: string, sessionToken: string): string => {
return `${webAuthSuccessUrl}?token=${sessionToken}`;
};

export const INTERNAL_SERVER_ERROR = 'An internal error occurred, please try again later.';