Skip to content

fix: Ensure DANFSE artifacts are correctly generated with improved error logging#143

Merged
vitormattos merged 1 commit into
mainfrom
fix/danfse-error-logging
Jul 2, 2026
Merged

fix: Ensure DANFSE artifacts are correctly generated with improved error logging#143
vitormattos merged 1 commit into
mainfrom
fix/danfse-error-logging

Conversation

@vitormattos

Copy link
Copy Markdown
Member

Problem

In production, DANFSE (PDF) generation failed intermittently:

  1. Certificate files were deleted before being used by the gateway client
  2. HTTP 400 errors from gateway with insufficient error context
  3. Production environments with restricted /dev/shm access failed silently

Root Causes

Certificate Cleanup Timing

Transport certificates (/dev/shm/nfse_tls_cert_*) were deleted before storeArtifacts() completed:

1. makeClient() → creates cert files
2. try block completes → finally deletes certs ❌
3. storeArtifacts() → getDanfse() fails (certs gone)

Limited Error Visibility

Gateway HTTP 400 errors lacked debugging context:

  • No exception class/code information
  • No stack trace showing failure point
  • No XML payload details

Directory Availability Issues

/dev/shm may be unavailable in some production environments.

Solutions Implemented

1. Smart Temporary Directory Selection

// Try /dev/shm first (fast, in-memory)
// Fallback to sys_get_temp_dir() if unavailable
// Better error messaging listing both checked directories

2. Fixed Cleanup Sequencing

Moved cleanup to AFTER all client operations complete:

  • emit(): Cleanup after storeArtifacts() completes
  • refresh(): Cleanup after storeArtifacts() completes
  • reemit(): Cleanup after storeArtifacts() completes

3. Comprehensive Error Logging

Capture complete exception information:

  • exception_class: The exception type
  • exception_message: Error message
  • exception_code: Error code
  • trace: Full stack trace
  • rawXml_length & rawXml_sample: XML payload details

Testing

  • New test: TransportCertificateCleanupTest validates certificate availability during DANFSE generation
  • Updated existing tests to match corrected behavior
  • All tests pass

Changes

  • Support/TransportCertificateManager.php: Fallback directory logic
  • Http/Controllers/InvoiceController.php: Cleanup sequencing + enhanced logging
  • tests/Unit/Http/Controllers/TransportCertificateCleanupTest.php: New test
  • tests/Unit/Http/Controllers/InvoiceControllerTest.php: Updated test assertions

…ror logging

Problem
-------
In production, DANFSE (PDF) generation failed with HTTP 400 errors from the gateway,
and temporary certificate files were deleted before being used.

Solutions
---------

1. Transport Certificate Management:
   - Add fallback from /dev/shm to sys_get_temp_dir() when /dev/shm unavailable
   - Handles production environments with restricted memory-backed storage

2. Certificate Cleanup Sequencing:
   - Move cleanup to AFTER storeArtifacts() completes in emit(), refresh(), reemit()
   - Ensures getDanfse() calls complete before certificate cleanup occurs
   - Prevents 'Unable to set local cert chain file' errors

3. Enhanced Error Logging:
   - Capture full exception details: class, message, code, stack trace
   - Log XML payload information: length and first 200 characters
   - Enables diagnosis of HTTP 400 gateway rejections

Changes
-------
- Support/TransportCertificateManager.php: Fallback directory strategy
- Http/Controllers/InvoiceController.php: Cleanup sequencing + detailed logging
- tests/Unit/Http/Controllers/TransportCertificateCleanupTest.php: New test
- tests/Unit/Http/Controllers/InvoiceControllerTest.php: Updated assertions

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
@vitormattos vitormattos merged commit dbaef4d into main Jul 2, 2026
9 checks passed
@vitormattos vitormattos deleted the fix/danfse-error-logging branch July 2, 2026 19:37
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.

1 participant