Skip to content

Avoid caching when API call fails#164

Merged
frenck merged 2 commits intohassio-addons:mainfrom
agners:avoid-caching-if-api-call-fails
Apr 22, 2025
Merged

Avoid caching when API call fails#164
frenck merged 2 commits intohassio-addons:mainfrom
agners:avoid-caching-if-api-call-fails

Conversation

@agners
Copy link
Contributor

@agners agners commented Apr 17, 2025

Proposed Changes

Check the return code of the bash function call and only store the result in the cache if the call was successful. This avoids caching empty values.

Related Issues

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced error handling across multiple system info retrieval functions to properly report failures and prevent processing of invalid data.

Check the return code of the bash function call and only store the
result in the cache if the call was successful. This avoids caching
empty values.
@coderabbitai
Copy link

coderabbitai bot commented Apr 17, 2025

Walkthrough

The changes introduce consistent error handling across multiple functions in various lib/*.sh scripts that interact with the Supervisor API. After each API call, the exit status is now checked; if the call fails, an error message is logged and the function returns immediately with a failure code. This prevents caching or further processing of invalid or missing data. No changes were made to existing function signatures or declarations.

Changes

File(s) Change Summary
lib/info.sh Added error handling in bashio::info() after Supervisor API call; logs error and returns on failure.
lib/addons.sh, lib/core.sh, lib/audio.sh, lib/cli.sh, lib/dns.sh, lib/hardware.sh, lib/host.sh, lib/multicast.sh, lib/network.sh, lib/observer.sh, lib/os.sh, lib/repositories.sh, lib/services.sh, lib/supervisor.sh Added error handling after Supervisor API calls in multiple functions to check exit status, log errors, and return on failure.

Poem

🐇 A hop, a skip, a careful check,
The API calls now have a tech!
If something fails, no silent slip,
The error logs, then quick to skip.
This bunny’s code keeps perfect pace! 🌿🐰


📜 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 4aa73b3 and 717ff46.

📒 Files selected for processing (14)
  • lib/addons.sh (2 hunks)
  • lib/audio.sh (2 hunks)
  • lib/cli.sh (2 hunks)
  • lib/core.sh (2 hunks)
  • lib/dns.sh (2 hunks)
  • lib/hardware.sh (1 hunks)
  • lib/host.sh (1 hunks)
  • lib/multicast.sh (2 hunks)
  • lib/network.sh (2 hunks)
  • lib/observer.sh (2 hunks)
  • lib/os.sh (1 hunks)
  • lib/repositories.sh (2 hunks)
  • lib/services.sh (1 hunks)
  • lib/supervisor.sh (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (14)
  • lib/observer.sh
  • lib/services.sh
  • lib/dns.sh
  • lib/os.sh
  • lib/multicast.sh
  • lib/repositories.sh
  • lib/network.sh
  • lib/hardware.sh
  • lib/host.sh
  • lib/cli.sh
  • lib/audio.sh
  • lib/addons.sh
  • lib/core.sh
  • lib/supervisor.sh

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 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

@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)
lib/info.sh (1)

33-38: Simplify and strengthen exit-code checking for API call.

Rather than capturing the output then immediately inspecting $?, you can use the if ! …; then pattern to both capture stdout and test the success in one step. It’s more concise and eliminates any ambiguity about which command’s exit code is being checked:

-if info=$(bashio::api.supervisor GET /info false)
-then
-  :
-else
-  bashio::log.error "Failed to get info from Supervisor API"
-  return "${__BASHIO_EXIT_NOK}"
-fi
+if ! info=$(bashio::api.supervisor GET /info false); then
+  bashio::log.error "Failed to get info from Supervisor API"
+  return "${__BASHIO_EXIT_NOK}"
+fi
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d70505a and 2557939.

📒 Files selected for processing (1)
  • lib/info.sh (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
lib/info.sh (1)
lib/log.sh (1)
  • bashio::log.error (190-195)
🔇 Additional comments (1)
lib/info.sh (1)

33-38: Correctly prevent caching when Supervisor API call fails.

The added error handling ensures that the "info" cache is only set on successful API responses, preventing invalid or empty data from being stored. This aligns perfectly with the PR’s goal to avoid caching failed API calls.

@agners
Copy link
Contributor Author

agners commented Apr 17, 2025

Without this, the first call prints an error like Unable to access the API, forbidden, but subsequent calls simply return an empty value (e.g. for $(bashio::info.homeassistant)).

There are more such API calls, so If that error handling looks good, I'll add it to all API call sites which set the cache subsequently.

frenck
frenck previously approved these changes Apr 19, 2025
Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks, @agners 👍

../Frenck

@frenck
Copy link
Member

frenck commented Apr 19, 2025

Without this, the first call prints an error like Unable to access the API, forbidden, but subsequent calls simply return an empty value (e.g. for $(bashio::info.homeassistant)).

There are more such API calls, so If that error handling looks good, I'll add it to all API call sites which set the cache subsequently.

Do you want to add them all to this PR?

../Frenck

@agners
Copy link
Contributor Author

agners commented Apr 22, 2025

Do you want to add them all to this PR?

Yes, let me do it for all sites.

@agners agners changed the title RFC: Avoid caching when API call fails Avoid caching when API call fails Apr 22, 2025
@agners agners force-pushed the avoid-caching-if-api-call-fails branch from 4aa73b3 to 717ff46 Compare April 22, 2025 08:00
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.

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 1

🛑 Comments failed to post (1)
lib/supervisor.sh (1)

185-203: ⚠️ Potential issue

Critical: Fix variable assignment in supervisor.country
The function mistakenly assigns the JSON payload to channel instead of country, leading to no payload being sent for the country setter. This will break the POST call's body.

Apply this diff to correct the variable name:

-function bashio::supervisor.country() {
+function bashio::supervisor.country() {
     local country=${1:-}
     bashio::log.trace "${FUNCNAME[0]}:" "$@"

     if bashio::var.has_value "${country}"; then
-        channel=$(bashio::var.json country "${country}")
-        bashio::api.supervisor POST /supervisor/options "${country}"
+        country=$(bashio::var.json country "${country}")
+        bashio::api.supervisor POST /supervisor/options "${country}"
         bashio::cache.flush_all
     else
         bashio::supervisor 'supervisor.info.country' '.country'
     fi
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

# ------------------------------------------------------------------------------
# Returns or sets the current country of the system.
#
# Arguments:
#   $1 Country to set (optional).
# ------------------------------------------------------------------------------
function bashio::supervisor.country() {
    local country=${1:-}

    bashio::log.trace "${FUNCNAME[0]}:" "$@"

    if bashio::var.has_value "${country}"; then
        country=$(bashio::var.json country "${country}")
        bashio::api.supervisor POST /supervisor/options "${country}"
        bashio::cache.flush_all
    else
        bashio::supervisor 'supervisor.info.country' '.country'
    fi
}

@agners
Copy link
Contributor Author

agners commented Apr 22, 2025

Coderabbitai references a change I was still working on and accidentally pushed. I dropped that again.

@frenck done, PR should be complete now.

Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks, @agners 👍

../Frenck

@frenck frenck merged commit 445707a into hassio-addons:main Apr 22, 2025
4 checks passed
@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementors. label Apr 22, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Dec 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants