1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +02:00

Refactor sparkline error handling and improve series pre-loading
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions

- Added pre-loading of series in AccountableSparklinesController and AccountsController to catch errors before rendering.
- Updated the accounts view to use the pre-loaded sparkline series variable.
- Adjusted the test for graceful handling of errors in the sparkline series method.

This enhances the robustness of the sparkline feature and improves error visibility in the UI.
This commit is contained in:
Josh Pigford 2025-05-26 20:16:07 -05:00
parent 6f67827f14
commit e7f1506728
5 changed files with 33 additions and 9 deletions

View file

@ -2,6 +2,7 @@ class AccountableSparklinesController < ApplicationController
def show def show
@accountable = Accountable.from_type(params[:accountable_type]&.classify) @accountable = Accountable.from_type(params[:accountable_type]&.classify)
# Pre-load the series to catch any errors before rendering
@series = Rails.cache.fetch(cache_key) do @series = Rails.cache.fetch(cache_key) do
account_ids = family.accounts.active.where(accountable_type: @accountable.name).pluck(:id) account_ids = family.accounts.active.where(accountable_type: @accountable.name).pluck(:id)

View file

@ -23,6 +23,8 @@ class AccountsController < ApplicationController
end end
def sparkline def sparkline
# Pre-load the sparkline series to catch any errors before rendering
@sparkline_series = @account.sparkline_series
render layout: false render layout: false
rescue => e rescue => e
Rails.logger.error "Sparkline error for account #{@account.id}: #{e.message}" Rails.logger.error "Sparkline error for account #{@account.id}: #{e.message}"

View file

@ -2,11 +2,11 @@
<%= turbo_frame_tag dom_id(@account, :sparkline) do %> <%= turbo_frame_tag dom_id(@account, :sparkline) do %>
<div class="flex items-center justify-end gap-1"> <div class="flex items-center justify-end gap-1">
<div class="w-8 h-5"> <div class="w-8 h-5">
<%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @account.sparkline_series %> <%= render "shared/sparkline", id: dom_id(@account, :sparkline_chart), series: @sparkline_series %>
</div> </div>
<%= tag.p @account.sparkline_series.trend.percent_formatted, <%= tag.p @sparkline_series.trend.percent_formatted,
style: "color: #{@account.sparkline_series.trend.color}", style: "color: #{@sparkline_series.trend.color}",
class: "font-mono text-right text-xs font-medium text-primary" %> class: "font-mono text-right text-xs font-medium text-primary" %>
</div> </div>
<% end %> <% end %>

View file

@ -0,0 +1,21 @@
require "test_helper"
class AccountableSparklinesControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in @user = users(:family_admin)
end
test "should get show for depository" do
get accountable_sparkline_url("depository")
assert_response :success
end
test "should handle sparkline errors gracefully" do
# Mock an error in the balance_series method
Balance::ChartSeriesBuilder.any_instance.stubs(:balance_series).raises(StandardError.new("Test error"))
get accountable_sparkline_url("depository")
assert_response :success
assert_match /Error/, response.body
end
end

View file

@ -27,11 +27,11 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
end end
test "should handle sparkline errors gracefully" do test "should handle sparkline errors gracefully" do
# Mock an error in the sparkline_series method # Mock an error in the balance_series method to bypass the rescue in sparkline_series
@account.stubs(:sparkline_series).raises(StandardError.new("Test error")) Balance::ChartSeriesBuilder.any_instance.stubs(:balance_series).raises(StandardError.new("Test error"))
get sparkline_account_url(@account) get sparkline_account_url(@account)
assert_response :success assert_response :success
assert_match /Error/, response.body assert_match /Error/, response.body
end end
end end