Skip to content

fix: api errors#3150

Merged
gupta-piyush19 merged 3 commits intomainfrom
fix/api-errors
Sep 18, 2024
Merged

fix: api errors#3150
gupta-piyush19 merged 3 commits intomainfrom
fix/api-errors

Conversation

@pandeymangg
Copy link
Contributor

@pandeymangg pandeymangg commented Sep 17, 2024

What does this PR do?

Fixes the error messages when the sync routes are called with invalid environmentId

How should this be tested?

  • Call the sync endpoints with a wrong environmentId
  • It should return a 404 not found response

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read How we Code at Formbricks
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand bits
  • Ran pnpm build
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues
  • First PR at Formbricks? Please sign the CLA! Without it we wont be able to merge it 🙏

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Formbricks Docs if changes were necessary

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for resource not found scenarios in various API endpoints, providing clearer feedback on missing resources.
    • Introduced additional properties to the ResourceNotFoundError class for improved error reporting.
    • Added mock data for testing purposes, improving the reliability of test cases.
  • Bug Fixes

    • Adjusted control flow in the environment state checks to ensure proper validation order.
    • Added validation for the existence of environments in user ID queries.
  • Documentation

    • Updated error handling mechanisms to improve clarity and robustness of API responses.

@vercel
Copy link

vercel bot commented Sep 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
formbricks-cloud ⬜️ Ignored (Inspect) Visit Preview Sep 18, 2024 9:01am
formbricks-docs ⬜️ Ignored (Inspect) Visit Preview Sep 18, 2024 9:01am

@github-actions
Copy link
Contributor

github-actions bot commented Sep 17, 2024

Thank you for following the naming conventions for pull request titles! 🙏

@jonas-hoebenreich
Copy link
Contributor

just a short comment: using the environmentId as the resourceId for product & organization is not the most elegant solution ;) Maybe you could just pass null here.

@mattinannt
Copy link
Member

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 17, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 17, 2024

Walkthrough

The changes involve modifications to error handling and control flow within several TypeScript files related to API functionality. The getEnvironmentState function's logic has been adjusted to prioritize environment validation before organization checks. A new error handling mechanism for ResourceNotFoundError has been implemented in multiple route files, enhancing the specificity of responses when resources are not found. The ResourceNotFoundError class has also been updated to include new properties for better context in error reporting, and mock data has been introduced to improve testing capabilities.

Changes

File Change Summary
.../environment/lib/environmentState.ts Repositioned the check for organization after the environment check in getEnvironmentState.
.../people/[userId]/attributes/route.ts Added error handling for ResourceNotFoundError, returning a not found response with resource details.
.../website/environment/lib/environmentState.ts Repositioned the check for organization after the environment check in getEnvironmentState.
packages/types/errors.ts Introduced resourceId and resourceType properties to ResourceNotFoundError class; modified constructor accordingly.
packages/lib/person/service.ts Added validation for environment existence in getPersonByUserId, throwing ResourceNotFoundError if not found.
packages/lib/display/tests/__mocks__/data.mock.ts Added mockEnvironment export conforming to TEnvironment type for testing.
packages/lib/display/tests/display.test.ts Mocked prisma.environment.findUnique to return mockEnvironment in tests for createDisplay and updateDisplay.
packages/lib/response/tests/constants.ts Added mockId and mockEnvironment constants for structured mock data in tests.
packages/lib/response/tests/response.test.ts Utilized mockEnvironment in test cases for createResponse service to simulate environment presence.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API
    participant ResourceHandler

    Client->>API: GET request for resource
    API->>ResourceHandler: Validate resource
    alt Resource found
        ResourceHandler-->>API: Resource data
        API-->>Client: 200 OK with resource data
    else Resource not found
        ResourceHandler-->>API: ResourceNotFoundError
        API-->>Client: 404 Not Found with resource details
    end
Loading

🐇 In the code we hop and play,
With errors caught along the way.
A missing resource? Not a fright,
We’ll tell you wrongs and make it right!
With checks in place, we bounce with glee,
Our API shines, as bright as can be! 🌟


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>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 and render them as a table.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
packages/lib/display/tests/display.test.ts (1)

17-17: Utilize the imported ResourceNotFoundError.

The import statement for ResourceNotFoundError is correct. However, consider utilizing this error type in the relevant test cases to ensure comprehensive error handling coverage.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5bc5292 and 5b0e766.

Files selected for processing (4)
  • packages/lib/display/tests/mocks/data.mock.ts (2 hunks)
  • packages/lib/display/tests/display.test.ts (5 hunks)
  • packages/lib/response/tests/constants.ts (2 hunks)
  • packages/lib/response/tests/response.test.ts (4 hunks)
Additional comments not posted (9)
packages/lib/response/tests/constants.ts (3)

1-2: LGTM!

The import statement is correctly defined and the TEnvironment type is likely used to define the structure of an environment object later in the file.


3-4: LGTM!

The mockId constant is defined correctly and the name suggests it is used for testing purposes.


14-22: LGTM!

The mockEnvironment constant is defined correctly with the TEnvironment type and its properties are initialized with appropriate values. The use of the mockId constant ensures consistency in the mock data, and setting createdAt and updatedAt to the current date provides a realistic timestamp for testing. The type property being set to "production" suggests this mock environment represents a production environment, while the appSetupCompleted and websiteSetupCompleted properties being set to false indicate that the app and website setup are not completed in this mock environment. Overall, this constant provides a well-structured mock environment for testing purposes.

packages/lib/display/tests/__mocks__/data.mock.ts (1)

53-61: LGTM!

The introduction of the mockEnvironment constant is a valuable addition to the mock data available for testing. By providing a structured representation of an environment that aligns with the TEnvironment type, it enhances the testing capabilities and ensures type safety.

The specific property values assigned to mockEnvironment, such as mockId for id and productId, current date for createdAt and updatedAt, "production" for type, and false for appSetupCompleted and websiteSetupCompleted, provide a realistic and consistent mock environment object that can be utilized in various test scenarios.

This addition improves test coverage, reliability, and maintainability by centralizing the mock environment data and ensuring its adherence to the expected structure and type constraints.

packages/lib/display/tests/display.test.ts (2)

10-10: LGTM!

The import statement for mockEnvironment is correct and consistent with the other mock data imports.


98-98: LGTM!

The addition of the prisma.environment.findUnique mock setup in the createDisplay and updateDisplay test cases is correct and ensures that the necessary environment data is available during testing. This enhances the robustness and reliability of the tests.

Also applies to: 118-118, 142-142, 153-153

packages/lib/response/tests/response.test.ts (3)

46-46: LGTM!

The import statement is syntactically correct and necessary for the test cases to run successfully.


208-208: LGTM!

The mocking of prisma.environment.findUnique is syntactically correct and necessary for the test case to run successfully.


220-220: LGTM!

The mocking of prisma.environment.findUnique is syntactically correct and necessary for the test case to run successfully.

Copy link
Contributor

@gupta-piyush19 gupta-piyush19 left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@gupta-piyush19 gupta-piyush19 added this pull request to the merge queue Sep 18, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 18, 2024
@gupta-piyush19 gupta-piyush19 added this pull request to the merge queue Sep 18, 2024
Merged via the queue into main with commit 10255aa Sep 18, 2024
@gupta-piyush19 gupta-piyush19 deleted the fix/api-errors branch September 18, 2024 10:01
@coderabbitai coderabbitai bot mentioned this pull request Sep 19, 2024
12 tasks
@coderabbitai coderabbitai bot mentioned this pull request Oct 1, 2024
12 tasks
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.

4 participants