Skip to content

pass in empty string if no resources are defined#910

Merged
GeorgeGoodall-GovUk merged 5 commits into
mainfrom
763-bug-cannot-read-properties-of-undefined-reading-resource
Feb 24, 2025
Merged

pass in empty string if no resources are defined#910
GeorgeGoodall-GovUk merged 5 commits into
mainfrom
763-bug-cannot-read-properties-of-undefined-reading-resource

Conversation

@GeorgeGoodall-GovUk
Copy link
Copy Markdown
Contributor

@GeorgeGoodall-GovUk GeorgeGoodall-GovUk commented Feb 21, 2025

Preview Link

https://submit-pr-910.herokuapp.com/

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

This PR addresses a potential issue with accessing the resource property of req.resources[0]. The change ensures that if req.resources[0] is undefined, the query will not break by providing a default empty string. This improves the robustness of the SQL query in the fetchIssueCount function.
if no resources are defined, the middleware chain will go on to find now issues and therefor show a 404 page instead of a generic error page

Related Tickets & Documents

QA Instructions, Screenshots, Recordings

navigating to here: https://submit-pr-910.herokuapp.com/organisations/local-authority:BUC/tree-preservation-order/reference%20values%20are%20not%20unique/reference/entry/368
now shows a 404 instead of a general error

Added/updated tests?

  • Yes
  • No, and this is why: The change is a minor bug fix that does not require additional tests.
  • I need help with writing tests

QA sign off

  • Code has been checked and approved
  • Design has been checked and approved
  • Product and business logic has been checked and proved

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling to ensure that when resource data is missing, a default value is returned. This enhancement prevents runtime issues and contributes to a more stable user experience.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 21, 2025

Walkthrough

The update introduces a conditional check in the fetchIssueCount function within src/middleware/entryIssueDetails.middleware.js. Previously, the function directly accessed req.resources[0].resource, risking runtime errors if no resource was present. The new implementation verifies the existence of req.resources[0] and returns a default SQL query (SELECT 0 AS count) when no resource is available. There are no changes to exported or publicly declared entities.

Changes

File(s) Change Summary
src/middleware/entryIssueDetails.middleware.js Updated error handling in fetchIssueCount by introducing a conditional check to verify req.resources[0] before constructing the SQL query.

Suggested reviewers

  • DilwoarH

Poem

In the meadows of code where errors once did roam,
I’ve added a check to guide each query safely home.
With a hop and a skip, no null shall make me frown,
Default values bloom when no resource is found.
Celebrate the change with a joyful, rabbit cheer!
🐇✨ Happy coding, my dear friends, far and near!


📜 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 50f539c and 730beca.

📒 Files selected for processing (1)
  • src/middleware/entryIssueDetails.middleware.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/middleware/entryIssueDetails.middleware.js
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: run-tests / test

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.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 21, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 65.98% 5066 / 7678
🔵 Statements 65.98% 5066 / 7678
🔵 Functions 65.15% 230 / 353
🔵 Branches 81.26% 707 / 870
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/middleware/entryIssueDetails.middleware.js 81.29% 76% 57.14% 81.29% 22-24, 46-57, 83-86, 126-132
Generated in workflow #859 for commit 730beca by the Vitest Coverage Report Action

Copy link
Copy Markdown
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: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 95ec802 and 49c730a.

📒 Files selected for processing (1)
  • src/middleware/entryIssueDetails.middleware.js (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: run-tests / test

Comment thread src/middleware/entryIssueDetails.middleware.js Outdated
Copy link
Copy Markdown
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

🧹 Nitpick comments (3)
src/views/check/results/shareResults.html (3)

36-55: Review of copyShareLink Function Implementation
The implementation correctly uses the Clipboard API with a fallback to ensure robust copy functionality. One suggestion is to validate that the button element selected by document.querySelector('.app-c-button--secondary-quiet') is not null before accessing its properties (e.g. innerText). This extra check will safeguard against potential runtime errors in cases where the button element might not exist in the DOM for any reason.

Suggested diff:

- const button = document.querySelector('.app-c-button--secondary-quiet');
- const originalText = button.innerText;
+ const button = document.querySelector('.app-c-button--secondary-quiet');
+ if (!button) {
+   console.error("Button element for copying share link was not found.");
+   return;
+ }
+ const originalText = button.innerText;

57-92: Review of fallbackCopyText Function Implementation
The fallback function is robust in its strategy for copying text via a temporary textarea element and includes well-managed error handling. As a minor refinement, consider utilising the modern textArea.remove() method instead of document.body.removeChild(textArea) for cleaning up the element. This can improve code readability and aligns with modern browser APIs, provided that browser compatibility is ensured.

Optional diff:

- document.body.removeChild(textArea);
+ textArea.remove();

94-99: Review of updateButtonText Helper Function
This helper function is clearly implemented and effectively provides user feedback by updating the button text temporarily. In a similar vein to the earlier suggestion, you might consider adding a safeguard check to confirm that the button element is defined before modifying its innerText, thereby preventing potential runtime errors if the function is inadvertently called with an undefined button.

Optional safeguard addition:

 function updateButtonText(temporaryText, originalText, button) {
-  button.innerText = temporaryText;
+  if (!button) return;
+  button.innerText = temporaryText;
   setTimeout(() => {
-    button.innerText = originalText;
+    if(button) { button.innerText = originalText; }
   }, 2000);
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 49c730a and 50f539c.

📒 Files selected for processing (1)
  • src/views/check/results/shareResults.html (1 hunks)

rosado
rosado previously approved these changes Feb 24, 2025
Copy link
Copy Markdown
Contributor

@rosado rosado left a comment

Choose a reason for hiding this comment

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

Look OK.

from issue i
LEFT JOIN issue_type it ON i.issue_type = it.issue_type
WHERE resource = '${req.resources[0].resource}'
WHERE resource = '${req.resources[0]?.resource || ''}'
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.

Actually this will return all resources with empty - we probably want to return 0 values

@GeorgeGoodall-GovUk GeorgeGoodall-GovUk merged commit 05981b4 into main Feb 24, 2025
@DilwoarH DilwoarH deleted the 763-bug-cannot-read-properties-of-undefined-reading-resource branch February 24, 2025 16:16
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.

Bug: Cannot read properties of undefined (reading 'resource')

5 participants