Checkmate icon indicating copy to clipboard operation
Checkmate copied to clipboard

Fix email template loading and error handling for infrastructure alerts

Open Retr0-XD opened this issue 1 month ago • 1 comments

Enhancements to the EmailService improve the loading of email templates and error handling. The changes ensure that when a template fails to load or compile, the error is logged and re-thrown, preventing the sending of empty emails. This addresses the issue of empty infrastructure alert emails.

Fixes #3054

Summary by CodeRabbit

  • Improvements
    • Enhanced email template reliability with improved error handling and reporting.
    • Optimized email template initialization for faster performance.
    • Added template caching and lazy-loading for consistent email delivery.

Retr0-XD avatar Nov 09 '25 09:11 Retr0-XD

[!NOTE]

.coderabbit.yml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key(s) in object: 'release_notes'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Walkthrough

The EmailService class was refactored to use synchronous initialization with template preloading instead of asynchronous initialization. A new loadTemplate() method loads and compiles templates, while buildEmail() now supports lazy-loading and caching of templates with enhanced error handling for templating failures.

Changes

Cohort / File(s) Summary
Template initialization and caching
server/src/service/v1/infrastructure/emailService.js
Changed init method from async to synchronous with preloaded templates; added loadTemplate() method for loading and compiling templates; introduced templateLookup object for caching compiled templates; enhanced buildEmail() with lazy-loading fallback and template validation; improved error handling with descriptive logging and re-throws

Sequence Diagram

sequenceDiagram
    participant Client
    participant EmailService
    participant TemplateSystem
    
    Note over Client,TemplateSystem: Initialization Phase
    Client->>EmailService: new EmailService()
    EmailService->>EmailService: init() [synchronous]
    EmailService->>TemplateSystem: loadTemplate() for each
    TemplateSystem-->>EmailService: compiled functions cached
    Note over EmailService: templateLookup populated
    
    Note over Client,TemplateSystem: Email Building Phase
    Client->>EmailService: buildEmail(templateName, ...)
    EmailService->>EmailService: Check templateLookup
    alt Template Found
        EmailService->>TemplateSystem: Use cached template
    else Template Not Found
        EmailService->>TemplateSystem: loadTemplate() with fallback logic
        TemplateSystem-->>EmailService: compiled function or null
        alt Load Success
            EmailService->>EmailService: Cache in templateLookup
        else Load Fails
            EmailService->>EmailService: Log & throw error
            EmailService-->>Client: Error
        end
    end
    EmailService->>TemplateSystem: Generate MJML → HTML
    EmailService-->>Client: HTML or empty string

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Areas requiring attention:
    • Logic in buildEmail() for template fallback resolution and lazy-loading behavior
    • Error handling paths and their impact on caller expectations
    • Verify all templates that should be preloaded are covered in init()
    • Confirm synchronous initialization doesn't block the application startup

Poem

🐰 Templates now load without a wait,
No empty emails, their proper fate!
Preloaded and cached, they're ready to go,
Infrastructure alerts will always show! 📧✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description check ⚠️ Warning The description provides context and includes the issue reference, but it lacks details on verification steps and doesn't follow the provided template with required checklist items. Add a completed checklist from the template showing local deployment, self-review, and testing verification to confirm the fix resolves the empty email issue.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing email template loading and error handling for infrastructure alerts, which directly addresses the linked issue.
Linked Issues check ✅ Passed The code changes directly address all key requirements from issue #3054: fixing template loading errors, preventing empty emails, and improving error handling through synchronous initialization and lazy-loading with caching.
Out of Scope Changes check ✅ Passed All changes are focused on the EmailService template loading mechanism and error handling, directly scoped to fixing the infrastructure alert email issue with no unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Nov 09 '25 09:11 coderabbitai[bot]

Closing as inactive

ajhollid avatar Dec 05 '25 17:12 ajhollid