Summary
Bound retries when CNPJ check-digit generation raises CnpjDV::DomainError.
CnpjGen::CnpjGenerator#with_check_digits currently retries by recursively re-entering #generate. If generated bases continue to be rejected, this can exhaust the Ruby call stack with SystemStackError rather than producing a deterministic failure.
Required changes
- Refactor the retry flow in
packages/cnpj-gen/src/cnpj-gen/cnpj_generator.rb into a private helper that tracks the number of attempts.
- Preserve the original per-call
options argument and keyword overrides for every retry.
- Define a finite retry limit and raise a deterministic terminal error once it is reached.
- Add focused tests for successful retry, retry-limit exhaustion, and preservation of the original call arguments/options.
Rationale
This prevents unbounded recursion while retaining the existing behavior of regenerating a CNPJ when check-digit calculation rejects a generated base.
Affected area
packages/cnpj-gen/src/cnpj-gen/cnpj_generator.rb
- Associated generator specs under
packages/cnpj-gen/tests/
Acceptance criteria
- A repeated
CnpjDV::DomainError cannot cause unbounded recursion or SystemStackError.
- Retries are limited to a documented/configured finite number of attempts.
- Successful retries retain the original option resolution behavior.
- Exhausting the limit raises a predictable, test-covered error.
Backlinks
Summary
Bound retries when CNPJ check-digit generation raises
CnpjDV::DomainError.CnpjGen::CnpjGenerator#with_check_digitscurrently retries by recursively re-entering#generate. If generated bases continue to be rejected, this can exhaust the Ruby call stack withSystemStackErrorrather than producing a deterministic failure.Required changes
packages/cnpj-gen/src/cnpj-gen/cnpj_generator.rbinto a private helper that tracks the number of attempts.optionsargument and keyword overrides for every retry.Rationale
This prevents unbounded recursion while retaining the existing behavior of regenerating a CNPJ when check-digit calculation rejects a generated base.
Affected area
packages/cnpj-gen/src/cnpj-gen/cnpj_generator.rbpackages/cnpj-gen/tests/Acceptance criteria
CnpjDV::DomainErrorcannot cause unbounded recursion orSystemStackError.Backlinks
cnpj-gen] Create package to generate valid CNPJ #17cnpj-gen] Create package to generate valid CNPJ #17 (comment)