Skip to content

feat(download): add error notifications when document downloads fail#981

Open
ctron wants to merge 2 commits intoguacsec:mainfrom
ctron:TC-4045
Open

feat(download): add error notifications when document downloads fail#981
ctron wants to merge 2 commits intoguacsec:mainfrom
ctron:TC-4045

Conversation

@ctron
Copy link
Copy Markdown
Contributor

@ctron ctron commented Apr 9, 2026

Summary

  • Add .catch() error handlers to all three download functions in the useDownload hook (advisory, SBOM, SBOM licenses)
  • Display "danger" toast notifications with descriptive titles and server error messages extracted via getAxiosErrorMessage
  • Add 7 unit tests covering error notification and success path behavior

Implements TC-4045

Test plan

  • Unit test: pushNotification called with variant: "danger" when downloadAdvisory rejects
  • Unit test: pushNotification called with variant: "danger" when downloadSbom rejects
  • Unit test: pushNotification called with variant: "danger" when downloadSbomLicense rejects
  • Unit test: getAxiosErrorMessage used to extract the error message
  • Unit test: success path still calls saveAs for advisory, SBOM, and SBOM license downloads
  • npm run check passes (biome lint/format)
  • npm test passes (all 9 tests)

🤖 Generated with Claude Code

Summary by Sourcery

Add error-handling and user notifications to document download hooks while verifying behavior with new unit tests.

New Features:

  • Show danger toast notifications when advisory, SBOM, or SBOM license downloads fail, including server error messages extracted via getAxiosErrorMessage.

Tests:

  • Add unit tests covering error notification behavior on download failures and ensuring successful downloads still trigger file saves without notifications.

Add .catch() handlers to all three download functions in useDownload
hook (advisory, SBOM, SBOM licenses) that display danger toast
notifications with the error message extracted via getAxiosErrorMessage.

Implements TC-4045

Assisted-by: Claude Code
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Apr 9, 2026

Reviewer's Guide

Adds centralized error handling and danger toast notifications to the useDownload hook’s advisory, SBOM, and SBOM license download flows, and introduces unit tests to verify both error notification and successful download behavior.

Sequence diagram for failed advisory download with error notification

sequenceDiagram
  actor User
  participant ReactComponent
  participant useDownload
  participant downloadAdvisory
  participant NotificationsContext
  participant getAxiosErrorMessage

  User->>ReactComponent: clickDownloadAdvisory
  ReactComponent->>useDownload: downloadAdvisory(id, filename)
  useDownload->>downloadAdvisory: request advisory
  downloadAdvisory-->>useDownload: reject(error)
  useDownload->>NotificationsContext: notifyDownloadError(title, error)
  NotificationsContext->>getAxiosErrorMessage: getAxiosErrorMessage(error)
  getAxiosErrorMessage-->>NotificationsContext: message
  NotificationsContext-->>User: pushNotification{variant: danger, title, message}
Loading

Sequence diagram for successful SBOM license download

sequenceDiagram
  actor User
  participant ReactComponent
  participant useDownload
  participant downloadSbomLicense
  participant getFilenameFromContentDisposition
  participant saveAs

  User->>ReactComponent: clickDownloadSbomLicenses
  ReactComponent->>useDownload: downloadSBOMLicenses(id)
  useDownload->>downloadSbomLicense: request SBOM licenses
  downloadSbomLicense-->>useDownload: resolve(response)
  useDownload->>getFilenameFromContentDisposition: getFilenameFromContentDisposition(header)
  getFilenameFromContentDisposition-->>useDownload: filename
  useDownload->>saveAs: saveAs(Blob, filenameOrDefault)
  saveAs-->>User: browser downloads tarGzFile
Loading

Flow diagram for useDownload advisory download with error handling

flowchart TD
  A[User triggers downloadAdvisory] --> B[useDownload called with id and filename]
  B --> C[Call downloadAdvisory with client and request options]
  C --> D{Promise resolved or rejected}
  D -->|resolved| E[Create Blob from response data]
  E --> F[Call saveAs with Blob and filename or idJson]
  F --> G[Browser downloads advisory file]
  D -->|rejected| H[Call notifyDownloadError with title and error]
  H --> I[Call getAxiosErrorMessage with error]
  I --> J[Construct notification with title, variant danger, message]
  J --> K[pushNotification shows danger toast to user]
Loading

File-Level Changes

Change Details Files
Add notification-driven error handling to all download functions in useDownload
  • Wrap advisory, SBOM, and SBOM license download calls in promise chains with .catch handlers
  • Introduce notifyDownloadError helper that uses getAxiosErrorMessage to build danger notifications
  • Wire NotificationsContext into the hook via React.useContext to access pushNotification
client/src/app/hooks/domain-controls/useDownload.ts
Add unit test coverage for success and failure paths of download functions
  • Mock file-saver, REST client functions, and utilities including getAxiosErrorMessage and getFilenameFromContentDisposition
  • Mock React.useContext to inject a test pushNotification implementation
  • Add tests asserting danger notifications on errors, getAxiosErrorMessage usage, and saveAs calls on success for all three download types
  • Introduce a flushPromises helper using act to ensure async chains settle before assertions
client/src/app/hooks/domain-controls/useDownload.test.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In the tests, consider rendering the hook within a real NotificationsContext.Provider instead of mocking React.useContext globally, to avoid tightly coupling the hook’s behavior to an implementation detail and to keep other potential useContext calls working as expected.
  • You can import useContext directly from react instead of importing the full React namespace (import { useContext } from "react";) to keep the hook’s dependencies a bit more focused.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the tests, consider rendering the hook within a real `NotificationsContext.Provider` instead of mocking `React.useContext` globally, to avoid tightly coupling the hook’s behavior to an implementation detail and to keep other potential `useContext` calls working as expected.
- You can import `useContext` directly from `react` instead of importing the full `React` namespace (`import { useContext } from "react";`) to keep the hook’s dependencies a bit more focused.

## Individual Comments

### Comment 1
<location path="client/src/app/hooks/domain-controls/useDownload.ts" line_range="16" />
<code_context>

+/** Hook providing download functions for advisories, SBOMs, and SBOM licenses with error notifications. */
 export const useDownload = () => {
+  const { pushNotification } = React.useContext(NotificationsContext);
+
+  /** Pushes a danger toast notification with the given title and the error message extracted from the error. */
</code_context>
<issue_to_address>
**question:** Consider how this hook behaves when used outside a NotificationsContext provider.

`React.useContext(NotificationsContext)` will throw if `useDownload` is used under a tree without that provider. If that’s acceptable, no change needed. Otherwise, consider either giving the context a safe default or guarding against a missing provider and no-op’ing notifications in that case.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


/** Hook providing download functions for advisories, SBOMs, and SBOM licenses with error notifications. */
export const useDownload = () => {
const { pushNotification } = React.useContext(NotificationsContext);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question: Consider how this hook behaves when used outside a NotificationsContext provider.

React.useContext(NotificationsContext) will throw if useDownload is used under a tree without that provider. If that’s acceptable, no change needed. Otherwise, consider either giving the context a safe default or guarding against a missing provider and no-op’ing notifications in that case.

@ctron ctron requested a review from carlosthe19916 April 9, 2026 10:39
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 9, 2026

Codecov Report

❌ Patch coverage is 46.66667% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.72%. Comparing base (2007d2c) to head (9f9fa53).

Files with missing lines Patch % Lines
...lient/src/app/hooks/domain-controls/useDownload.ts 46.66% 4 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #981      +/-   ##
==========================================
- Coverage   66.79%   66.72%   -0.07%     
==========================================
  Files         221      221              
  Lines        3882     3889       +7     
  Branches      898      898              
==========================================
+ Hits         2593     2595       +2     
- Misses        949      954       +5     
  Partials      340      340              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The error parameter was typed as `unknown` which caused a build-time
type error when passed to `getAxiosErrorMessage`.

Implements TC-4045

Assisted-by: Claude Code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant