Skip to content

Conversation

groyoh
Copy link
Contributor

@groyoh groyoh commented Aug 27, 2025

Context

The CreditNote::Create service was catching all ArgumentError exceptions and converting them to reason validation errors, which could mask legitimate errors from other parts of the code. This made debugging difficult and could hide real bugs.

Description

This fixes the issue by adding an explicit validation.

…alidation

## Context

The service was catching all ArgumentError exceptions and converting them to reason validation errors, which could mask legitimate errors from other parts of the code. This made debugging difficult and could hide real bugs.

## Description

- Add explicit reason validation using invalid_reason? method before model assignment
- Validate reason against CreditNote.reasons.keys to ensure only valid enum values are accepted
- Remove broad ArgumentError rescue that was silently converting any ArgumentError to reason validation failure
- Add early validation return to prevent invalid reasons from reaching model assignment
- Remove aggregate_failures from test cases for better test isolation
- Add test case to verify proper error handling for invalid reason parameter
@groyoh groyoh changed the title fix(credit-notes): prevent ArgumentError masking with proper reason validation fix(credit-notes): Prevent ArgumentError masking with proper reason validation Aug 27, 2025
@groyoh groyoh marked this pull request as ready for review August 27, 2025 14:45
Copy link
Collaborator

@vincent-pochet vincent-pochet left a comment

Choose a reason for hiding this comment

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

Good catch, It's a good fix!

Have you also considered adding the validate: true to the reason enum?
It was introduce after the creation of the Credit Note feature and it has the advantage on relying on the standard rails validation that is already in place in this service.
The downside is that it could have some unexpected side-effects but it should be very limited.

@groyoh
Copy link
Contributor Author

groyoh commented Aug 28, 2025

Have you also considered adding the validate: true to the reason enum?

Well, TIL about validate: true. @julienbourdeau I guess you're not alone 😄

Thanks for the hint @vincent-pochet, I'll try and update it.

@groyoh groyoh merged commit 5586d7e into main Aug 28, 2025
14 checks passed
@groyoh groyoh deleted the fix/credit-note-reason branch August 28, 2025 09:55
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.

3 participants