Fix: TypeError (MAYBE-MARKETING-30) #242
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The error
TypeError: nil can't be coerced into Float
occurring on line 52 of the early mortgage payoff calculator indicates that we're attempting to perform floating-point arithmetic with a nil value. This is happening in the calculation of the remaining balance after making extra payments.The root cause appears to be that the
interest_rate
attribute can become nil in certain scenarios, particularly when the mortgage rate cache doesn't return a value and the default of 0.0 isn't properly handling the calculation chain. A 0.0 interest rate also doesn't make practical sense for mortgage calculations.Changes made to fix this:
Changed the default
interest_rate
from 0.0 to 5.0 to provide a reasonable starting point that reflects typical mortgage rates. This prevents unwanted nil or zero scenarios that could break calculations.Added a
min: 0.0
constraint to theinterest_rate
attribute to prevent negative interest rates which would cause unrealistic calculations.Modified the
mortgage_rate_30
method to return a default value of 5.0 if the cache returns nil, ensuring we always have a workable interest rate.Updated the blank? method to check for nil values in addition to zero values, providing better validation.
These changes maintain the original intent of the calculator while making it more robust against nil values and unrealistic interest rate scenarios. The default 5.0% interest rate is a reasonable starting point that allows the calculator to function even when external rate data is unavailable.
Error Details
Summary:
Stacktrace:
Tip
You can make revisions or ask questions of Revise.dev by using
/revise
in any comment or review!/revise Add a comment above the method to explain why we're making this change.
/revise Why did you choose to make this change specifically?
Important
If something doesn’t look right, click to retry this interaction.
Quick links: View in Sentry • View in Revise