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 throwing error when an error is thrown in the backend. #288

Merged
merged 1 commit into from
Feb 24, 2024

Conversation

mohamedsalem401
Copy link
Contributor

@mohamedsalem401 mohamedsalem401 commented Feb 21, 2024

  • When an error occurs in the backend, the server responds with 200 and doesn't throw/show an error. This PR fixes this.

Summary by CodeRabbit

  • Refactor
    • Improved error handling logic to provide more specific exceptions based on the type of error encountered.

Copy link

changeset-bot bot commented Feb 21, 2024

⚠️ No Changeset found

Latest commit: 2a932d9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Feb 21, 2024

Walkthrough

The recent update introduces a refined error handling mechanism within the handleServiceError function, enhancing the application's robustness. By differentiating error types, it now ensures more precise exception throwing, tailoring responses to the nature of the encountered issue.

Changes

File(s) Summary
packages/api/src/@core/utils/errors.ts Updated the handleServiceError function to differentiate error types.

🐇✨
In the land of code where the snippets do roam,
A rabbit hopped in, making errors its home.
"Fear not," it whispered, with a magical tone,
For it sorted the chaos, with wisdom it's shown.
Now each mistake, by its type, is known.
🌟🐾

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?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@ghost
Copy link

ghost commented Feb 21, 2024

👇 Click on the image for a new way to code review

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map legend

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7de1818 and 2a932d9.
Files selected for processing (1)
  • packages/api/src/@core/utils/errors.ts (1 hunks)

Comment on lines +83 to +89
if (error instanceof HttpException) {
throw error;
}

if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new HttpException(errorMessage, statusCode);
}
Copy link
Contributor

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.

Comment on lines +82 to +90

if (error instanceof HttpException) {
throw error;
}

if (error instanceof Prisma.PrismaClientKnownRequestError) {
throw new HttpException(errorMessage, statusCode);
}

Copy link
Contributor

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.

@naelob naelob merged commit 7b0426c into panoratech:main Feb 24, 2024
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants