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: resilience evidence attachment missing #1455

Merged
merged 5 commits into from
Feb 7, 2025

Conversation

Axxiar
Copy link
Contributor

@Axxiar Axxiar commented Jan 30, 2025

avoid crashing when the attachment is brutally removed from /db/attachments/

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved attachment handling to prevent errors when accessing non-existent files.
    • Enhanced error detection and user feedback for attachment retrieval.
  • New Features

    • Added specific error messaging for attachment-related issues.
    • Introduced a property to indicate the existence of attachment files.

The changes focus on improving the reliability and user experience when working with file attachments across the application.

Copy link

coderabbitai bot commented Jan 30, 2025

Warning

Rate limit exceeded

@nas-tabchiche has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 9 minutes and 34 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 675cef6 and 0184c5b.

📒 Files selected for processing (4)
  • backend/core/models.py (1 hunks)
  • backend/core/views.py (1 hunks)
  • frontend/messages/en.json (1 hunks)
  • frontend/messages/fr.json (1 hunks)

Walkthrough

The pull request enhances error handling and attachment retrieval across the application. It includes backend modifications to improve checks for attachment existence in storage, as well as frontend updates to provide clearer user feedback when attachments cannot be found. New properties and conditional rendering are introduced in the frontend components, along with a localization message for attachment-related errors.

Changes

File Change Summary
backend/core/models.py Enhanced get_size method to check attachment existence in storage.
backend/core/views.py Added storage existence check in attachment action of EvidenceViewSet.
frontend/messages/en.json Added new error message for attachment not found scenario.
frontend/src/lib/components/ModelTable/EvidenceFilePreview.svelte Added fileExists property to Attachment interface and implemented error handling.
frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/+page.svelte Updated Attachment interface to include fileExists and added conditional rendering for fetch failures.

Sequence Diagram

sequenceDiagram
    participant Frontend
    participant Backend
    participant Storage
    
    Frontend->>Backend: Request Attachment
    Backend->>Storage: Check Attachment Existence
    alt Attachment Exists
        Storage-->>Backend: Attachment Found
        Backend-->>Frontend: Return Attachment
    else Attachment Not Found
        Storage-->>Backend: Attachment Missing
        Backend-->>Frontend: Return 404 Error
        Frontend->>Frontend: Display Error Message
    end
Loading

Poem

🐰 Attachments, oh attachments, where art thou?
Checked with care, no file shall slip now
Backend guards, frontend warns with grace
A robust dance in our digital space
Error messages bloom like spring's first flower 🌼


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 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. (Beta)
  • @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.

@nas-tabchiche
Copy link
Contributor

@coderabbitai full review

Copy link

coderabbitai bot commented Jan 31, 2025

✅ Actions performed

Full review triggered.

Copy link

@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: 1

🧹 Nitpick comments (2)
backend/core/views.py (2)

3134-3134: Remove debug print statement.

The commented-out print statement should be removed as it's not needed in production code.

-        # print("==> ATTACHEMENT CALLED")

3138-3141: Remove debug print statement and fix typo.

The commented-out print statement should be removed and there's a typo in "ATTACHEMENT".

-            # print("=============\n",evidence.attachment.name,  evidence.attachment.storage.exists(evidence.attachment.name), "\n========")
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d37a5c1 and 84186dd.

📒 Files selected for processing (5)
  • backend/core/models.py (1 hunks)
  • backend/core/views.py (1 hunks)
  • frontend/messages/en.json (1 hunks)
  • frontend/src/lib/components/ModelTable/EvidenceFilePreview.svelte (2 hunks)
  • frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/+page.svelte (3 hunks)
🔇 Additional comments (9)
frontend/src/lib/components/ModelTable/EvidenceFilePreview.svelte (3)

Line range hint 7-11: LGTM! Interface enhancement for better error handling.

The addition of couldFetch to the Attachment interface enables proper tracking of attachment fetch status.


19-23: LGTM! Robust fetch status check.

The fetch status check is comprehensive, verifying both:

  • HTTP response status via res.ok
  • Error message in blob content

49-50: LGTM! User-friendly error feedback.

Good addition of error message display when attachment fetch fails, using the localized message.

frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/+page.svelte (3)

19-22: LGTM! Consistent interface enhancement.

The addition of couldFetch to the Attachment interface maintains consistency with EvidenceFilePreview.svelte.


53-57: LGTM! Consistent fetch status check.

The fetch status check implementation matches EvidenceFilePreview.svelte, ensuring consistent behavior.


190-194: LGTM! Consistent error handling UI.

The error message display implementation aligns with EvidenceFilePreview.svelte, providing a consistent user experience.

backend/core/views.py (1)

3139-3141: LGTM! Robust attachment existence check.

The addition of storage.exists() check prevents the application from crashing when an attachment file is missing.

frontend/messages/en.json (1)

777-777: LGTM! Clear error message.

The added message "Error: Could not find attachment." is clear and user-friendly.

backend/core/models.py (1)

1722-1724: LGTM! Good defensive programming.

The additional check for file existence in storage is a robust solution that prevents crashes when the file is missing from the storage system.

backend/core/models.py Outdated Show resolved Hide resolved
@nas-tabchiche nas-tabchiche marked this pull request as ready for review February 7, 2025 18:49
Copy link

@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

🧹 Nitpick comments (1)
frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/+page.svelte (1)

50-58: Add error handling for fetch operations.

While using res.ok is good, the fetch operation should include error handling to gracefully handle network failures or invalid responses.

Consider adding try-catch blocks:

 const fetchAttachment = async () => {
+  try {
     const res = await fetch(`./${data.evidence.id}/attachment`);
+    if (!res.ok) {
+      return {
+        type: '',
+        url: '',
+        fileExists: false
+      };
+    }
     const blob = await res.blob();
     return {
       type: blob.type,
       url: URL.createObjectURL(blob),
       fileExists: res.ok
     };
+  } catch (error) {
+    console.error('Failed to fetch attachment:', error);
+    return {
+      type: '',
+      url: '',
+      fileExists: false
+    };
+  }
 };
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 84186dd and 675cef6.

📒 Files selected for processing (4)
  • backend/core/models.py (1 hunks)
  • backend/core/views.py (1 hunks)
  • frontend/src/lib/components/ModelTable/EvidenceFilePreview.svelte (2 hunks)
  • frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/+page.svelte (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • frontend/src/lib/components/ModelTable/EvidenceFilePreview.svelte
  • backend/core/views.py
  • backend/core/models.py
🔇 Additional comments (2)
frontend/src/routes/(app)/(third-party)/evidences/[id=uuid]/+page.svelte (2)

19-23: LGTM! Interface update aligns with PR objective.

The addition of the fileExists boolean property to the Attachment interface is a good approach to track attachment availability.


189-195: LGTM! Clear user feedback for missing attachments.

The conditional rendering provides appropriate feedback to users when attachments are missing, using localized messages.

Copy link
Contributor

@nas-tabchiche nas-tabchiche left a comment

Choose a reason for hiding this comment

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

LGTM. Merging.

Copy link
Collaborator

@eric-intuitem eric-intuitem left a comment

Choose a reason for hiding this comment

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

LGTM

@eric-intuitem eric-intuitem merged commit afd0622 into main Feb 7, 2025
21 checks passed
@eric-intuitem eric-intuitem deleted the CA-845-resilience-evidence-attachment-missing branch February 7, 2025 21:17
@github-actions github-actions bot locked and limited conversation to collaborators Feb 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants