Skip to content

Fix #19300: Avoid unnecessary error logging for existing email templates#1113

Open
SSSD-2001 wants to merge 1 commit into
wso2:masterfrom
SSSD-2001:fix-email-template-logging-19300
Open

Fix #19300: Avoid unnecessary error logging for existing email templates#1113
SSSD-2001 wants to merge 1 commit into
wso2:masterfrom
SSSD-2001:fix-email-template-logging-19300

Conversation

@SSSD-2001

Copy link
Copy Markdown

This PR addresses a logging issue where duplicate email template names were triggering unnecessary backend error logs.
Fixes wso2/product-is#19300

@CLAassistant

CLAassistant commented May 1, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented May 1, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Changes Made

Modified exception handling in the email template service to differentiate between conflict-style failures and other failures. When adding an email template fails due to the template name already existing, the system now logs this condition at DEBUG level instead of ERROR level. Other types of email template addition failures continue to be logged at ERROR level. This change prevents expected client-side conflicts (which correctly receive a 409 response) from generating unnecessary backend error logs.

Impact

Reduces noise in error logs by distinguishing between actual backend errors and expected client-side conflicts when duplicate email template names are provided.

Walkthrough

The change modifies exception handling in the addEmailTemplate method to differentiate logging severity based on error type. When an email template with the same name already exists, the exception is logged at DEBUG level rather than ERROR level. Other I18nEmailMgtException errors continue to be logged at ERROR level. Both cases rethrow the exception after logging, maintaining existing error handling behavior while reducing noise in application logs for expected client-side conflicts.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete. While it identifies the issue, it lacks the required template sections such as Goals, Approach, and critical sections like Automation tests and Security checks. Complete the pull request description using the provided template, including Goals, Approach, Release notes, Automation tests, and Security checks sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: differentiating error logging levels for existing email templates versus other failures.
Linked Issues check ✅ Passed The code changes directly address issue #19300 by implementing differentiated logging: DEBUG for existing templates and ERROR for other failures, resolving the unnecessary error logging problem.
Out of Scope Changes check ✅ Passed The changes are narrowly scoped to exception handling in the addEmailTemplate method and directly relate to the linked issue objective of preventing error-level logs for duplicate template checks.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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
Review rate limit: 0/1 reviews remaining, refill in 60 minutes.

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@components/org.wso2.carbon.identity.api.server.email.template/org.wso2.carbon.identity.rest.api.server.email.template.v1/src/main/java/org/wso2/carbon/identity/rest/api/server/email/template/v1/core/ServerEmailTemplatesService.java`:
- Around line 267-275: The code in ServerEmailTemplatesService is comparing
e.getErrorCode() to the REST-layer constant
Constants.ErrorMessage.ERROR_EMAIL_TEMPLATE_ALREADY_EXISTS.getCode(), which can
miss manager-thrown "already exists" errors; change the comparison to use the
manager-layer error-code constant used elsewhere (the manager's mapped error
code for template-conflict) — e.g., replace
Constants.ErrorMessage.ERROR_EMAIL_TEMPLATE_ALREADY_EXISTS.getCode() with the
manager constant (the ErrorMessages/ERROR_CODE_EMAIL_TEMPLATE_ALREADY_EXISTS
from the email template manager/constants class referenced in this module) so
the conflict branch triggers correctly and avoids logging a spurious ERROR.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1c2f2479-c5ac-47f7-bbcb-8b83d0a79e53

📥 Commits

Reviewing files that changed from the base of the PR and between 070e10b and 97fdb0a.

📒 Files selected for processing (1)
  • components/org.wso2.carbon.identity.api.server.email.template/org.wso2.carbon.identity.rest.api.server.email.template.v1/src/main/java/org/wso2/carbon/identity/rest/api/server/email/template/v1/core/ServerEmailTemplatesService.java

Comment on lines +267 to 275
if (Constants.ErrorMessage.ERROR_EMAIL_TEMPLATE_ALREADY_EXISTS.getCode().equals(e.getErrorCode())) {
// We only log this as a DEBUG message to keep Carbon logs clean
if (log.isDebugEnabled()) {
log.debug("Email template already exists: " + emailTemplateWithID.getId(), e);
}
} else {
// Log as an actual error only if it's a real server-side failure
log.error("Error occurred while adding email template", e);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Use mapped/manager error code for conflict detection

Line 267 compares e.getErrorCode() to a REST-layer code (Constants.ErrorMessage...getCode()), while manager exceptions in this class are matched using manager error-code constants. This can miss the “already exists” case and still hit Line 274 ERROR logging.

Suggested fix
-        if (Constants.ErrorMessage.ERROR_EMAIL_TEMPLATE_ALREADY_EXISTS.getCode().equals(e.getErrorCode())) {
+        Constants.ErrorMessage mappedError = Constants.getMappedErrorMessage(e.getErrorCode());
+        if (mappedError == Constants.ErrorMessage.ERROR_EMAIL_TEMPLATE_ALREADY_EXISTS) {
             if (log.isDebugEnabled()) {
                 log.debug("Email template already exists: " + emailTemplateWithID.getId(), e);
             }
         } else {
             log.error("Error occurred while adding email template", e);
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@components/org.wso2.carbon.identity.api.server.email.template/org.wso2.carbon.identity.rest.api.server.email.template.v1/src/main/java/org/wso2/carbon/identity/rest/api/server/email/template/v1/core/ServerEmailTemplatesService.java`
around lines 267 - 275, The code in ServerEmailTemplatesService is comparing
e.getErrorCode() to the REST-layer constant
Constants.ErrorMessage.ERROR_EMAIL_TEMPLATE_ALREADY_EXISTS.getCode(), which can
miss manager-thrown "already exists" errors; change the comparison to use the
manager-layer error-code constant used elsewhere (the manager's mapped error
code for template-conflict) — e.g., replace
Constants.ErrorMessage.ERROR_EMAIL_TEMPLATE_ALREADY_EXISTS.getCode() with the
manager constant (the ErrorMessages/ERROR_CODE_EMAIL_TEMPLATE_ALREADY_EXISTS
from the email template manager/constants class referenced in this module) so
the conflict branch triggers correctly and avoids logging a spurious ERROR.

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.

Log an error on carbon logs when adding an email template with existing name

2 participants