Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,18 @@ public SimpleEmailTemplate addEmailTemplate(String templateTypeId, EmailTemplate
throw handleError(Constants.ErrorMessage.ERROR_EMAIL_TEMPLATE_ALREADY_EXISTS);
}
} catch (I18nEmailMgtException e) {
throw handleI18nEmailMgtException(e, Constants.ErrorMessage.ERROR_ADDING_EMAIL_TEMPLATE);
// Check if the error is due to the template already existing (Conflict)
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);
}
Comment on lines +267 to 275

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.

throw handleI18nEmailMgtException(e, Constants.ErrorMessage.ERROR_ADDING_EMAIL_TEMPLATE);
}
}

/**
Expand Down