Skip to content

Bound CNPJ generator retries after check-digit failures #19

Description

@coderabbitai

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

Metadata

Metadata

Assignees

Labels

enhancementNew minor or major features.refactoringNon-functional improvements or changes.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions