middleware icon indicating copy to clipboard operation
middleware copied to clipboard

fix: Added Json error handling instead of HTML

Open harshit078 opened this issue 8 months ago • 5 comments

Proposed changes

  • This PR focuses on solving the current issue of handling the errors thrown from backend as HTML instead of JSON for better readability and usability.
  • Added Flask's Error handling methods and converted HTML errors into JSON.
  • Added Dedicated handler for HTTP exceptions and generic errors.

Linked Issue(s)

  • #561

Further comments

  • Doc Referred - https://flask.palletsprojects.com/en/stable/errorhandling/

Summary by CodeRabbit

  • New Features
    • Enhanced error handling with structured JSON responses for HTTP and general errors, including detailed context and optional debug tracebacks.
    • Added new test routes and comprehensive tests to verify error handling across different scenarios and modes.
  • Bug Fixes
    • Improved application startup reliability by explicitly converting the server port to an integer.

harshit078 avatar Mar 07 '25 10:03 harshit078

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Mar 07 '25 10:03 CLAassistant

Walkthrough

The changes introduce two new error handling functions within the Flask application. The handle_http_exception function formats HTTP exceptions into a JSON response with the error message, status code, and request path. The handle_exception function handles all other exceptions, providing optional traceback details in debug mode. Additionally, the application run method now explicitly converts the port number to an integer. Corresponding tests have been added to verify these error handlers. Similar error handling logic is also added to the synchronous Flask app variant.

Changes

File Change Summary
backend/analytics_server/app.py - Added handle_http_exception(e) for handling HTTP exceptions with JSON responses.
- Added handle_exception(e) for general exception handling with optional traceback in debug mode.
- Updated port conversion using int(ANALYTICS_SERVER_PORT).
backend/analytics_server/sync_app.py - Added handle_http_exception(e) and handle_exception(e) error handlers for JSON-based error responses replacing default HTML errors.
backend/analytics_server/tests/tests_error_handling.py - Created new test file for error handling.
- Added handle_http_exception(e) and handle_exception(e) for testing.
- Defined routes for simulating BadRequest and general exceptions.
- Implemented TestErrorHandlers class for unit tests validating error handler responses.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant A as Analytics Server (Flask App)
    
    C->>A: Send Request
    A->>A: Process Request
    alt HTTPException Occurs
        A->>A: handle_http_exception(e)
        A->>C: Return JSON (error message, status code, path)
    else Other Exception Occurs
        A->>A: handle_exception(e)
        alt Debug Mode Enabled
            A->>A: Append traceback to response
        end
        A->>C: Return JSON (error message, status code 500, path, exception type)
    end

Poem

Oh, what joy in each new line,
My code hops agile, feeling fine.
I guard errors with a gentle perk,
Like a bunny nibbles on a carrot's work.
In debug or release, no bug shall hide—
With swift hops of code, I lead the stride!
🐰 Carrot cheers to code that’s bona fide!


📜 Recent review details

Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43a21c2f98139ac77f28f23f741621f5b0cb18e8 and 20378404f90bcd212970bc4d29a4a415e62d2100.

📒 Files selected for processing (1)
  • backend/analytics_server/app.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • backend/analytics_server/app.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

coderabbitai[bot] avatar Mar 07 '25 10:03 coderabbitai[bot]

@harshit078 Thanks for the PR. It would be great if you can include some tests for your changes

adnanhashmi09 avatar Mar 18 '25 12:03 adnanhashmi09

@harshit078 Thanks for the changes. I noticed that you haven't added the error handling for the sync_api. It still returns html errors. check sync_app.py file. The rest of the changes in the PR work as expected. Nice work!

adnanhashmi09 avatar Apr 15 '25 15:04 adnanhashmi09

@adnanhashmi09, Added error handling in sync_app.py.

harshit078 avatar Apr 16 '25 06:04 harshit078