-
Notifications
You must be signed in to change notification settings - Fork 196
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 throwing error when an error is thrown in the backend. #288
Conversation
|
WalkthroughThe recent update introduces a refined error handling mechanism within the Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (error instanceof HttpException) { | ||
throw error; | ||
} | ||
|
||
if (error instanceof Prisma.PrismaClientKnownRequestError) { | ||
throw new HttpException(errorMessage, statusCode); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updated logic in the handleServiceError
function now throws exceptions directly for instances of HttpException
and wraps Prisma.PrismaClientKnownRequestError
in an HttpException
before throwing. This approach ensures that errors are communicated more accurately to the client, aligning with the PR's objectives. However, consider adding specific handling for other custom errors defined in this file, such as AppError
, NotFoundError
, BadRequestError
, UnauthorizedError
, and NotUniqueRecord
, to ensure consistency in error handling across the application.
|
||
if (error instanceof HttpException) { | ||
throw error; | ||
} | ||
|
||
if (error instanceof Prisma.PrismaClientKnownRequestError) { | ||
throw new HttpException(errorMessage, statusCode); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for handling Prisma.PrismaClientKnownRequestError
errors throws a new HttpException
with the errorMessage
and statusCode
derived from the Prisma error. This is a significant improvement in error handling, ensuring that Prisma errors are communicated effectively to the client. However, it's important to ensure that the statusCode
set for Prisma errors is appropriate for the type of error encountered. For example, a unique constraint violation might be better represented with a 400
(Bad Request) or 409
(Conflict) status code rather than a generic 500
(Internal Server Error). Consider refining the logic to map specific Prisma error codes to more appropriate HTTP status codes.
The function returns an object with data
, error
, message
, and statusCode
fields when an error does not result in an exception being thrown. This structured response is beneficial for client-side error handling. However, ensure that the errorMessage
included in the response is sanitized and does not expose sensitive information or implementation details that could pose a security risk. Additionally, consider standardizing the error response format across different parts of the application to facilitate easier error handling on the client side.
Summary by CodeRabbit