Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mail-connector): check htmlBody and/or body is null and throw an error #3796

Merged
merged 8 commits into from
Dec 18, 2024

Conversation

itsnuyen
Copy link
Contributor

Description

Because FEEL will take a string and a data and try to sum it, it will produce a null value.
This null value will produce a nullpointer as an error but the process is still running and after the defined retries it will break.
If you have multiple process running, this behavior will block everything else

So check if the content of the body is null and throw a null pointer

Related issues

closes ##3795

Checklist

  • PR has a milestone or the no milestone label.

…error

if not throw an error, connector will retry it over and over again.
@itsnuyen itsnuyen self-assigned this Dec 13, 2024
@itsnuyen itsnuyen marked this pull request as ready for review December 13, 2024 10:06
@itsnuyen itsnuyen requested a review from a team as a code owner December 13, 2024 10:06
Comment on lines 304 to 322
if (smtpSendEmail.body() == null) {
throw new MessagingException("Text Body is null");
}
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(smtpSendEmail.body(), StandardCharsets.UTF_8.name());
multipart.addBodyPart(textPart);
}
case HTML -> {
if (smtpSendEmail.htmlBody() == null) {
throw new MessagingException("HTML Body is null");
}
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(smtpSendEmail.htmlBody(), JakartaUtils.HTML_CHARSET);
multipart.addBodyPart(htmlPart);
}
case MULTIPART -> {
if (smtpSendEmail.htmlBody() == null || smtpSendEmail.body() == null) {
throw new MessagingException("Text or HTML Body is null");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @itsnuyen, instead of throwing a MessagingException, could you implement a custom validation method in the model that verifies that these values are not null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @sbuettner,
I have one questions. Do you mean adapting inside the SmtpSendEmail record the validation like this?

switch (contentType) {
  case PLAIN:
    if (body == null) {
      throw new IllegalArgumentException("Email body is null");
    }
  case HTML:
    if (htmlBody == null) {
      throw new IllegalArgumentException("Email html body is null");
    }
  case MULTIPART:
    if (body == null || htmlBody == null) {
      throw new IllegalArgumentException("Email body or html body cannot be null");
    }
}

This could also running but as a error message it could be confusing for the end user:
image
As it state that an exception is raised and it is a JSON_PROCESSING_ERROR

Do you have an Idea how I could provide the end user proper feedback?
Or do I even misunderstand your request?

Copy link
Contributor

@sbuettner sbuettner Dec 17, 2024

Choose a reason for hiding this comment

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

Hey @itsnuyen I was thinking about something like this as part of the SmtpSendEmail record:

@AssertTrue(message = "Please provide a proper message body")
        public boolean isValidMessageBody() {
                return body != null || htmlBody != null;
        }

@sbuettner sbuettner added this to the 8.7.0-alpha3 milestone Dec 17, 2024
@sbuettner
Copy link
Contributor

Great work @itsnuyen, could you add a simple test that covers this new validation?

sbuettner
sbuettner previously approved these changes Dec 18, 2024
Copy link
Contributor

@sbuettner sbuettner left a comment

Choose a reason for hiding this comment

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

LGTM,
looks like the test is missing the right license information:

Warning: Missing header in: /home/runner/work/connectors/connectors/connectors/email/src/test/java/io/camunda/connector/email/outbound/protocols/actions/SmtpSendEmailTest.java

You can use the license plugin to make sure its added to all files.

@mathias-vandaele
Copy link
Contributor

LGTM, Great work 😄

@mathias-vandaele mathias-vandaele added this pull request to the merge queue Dec 18, 2024
Merged via the queue into main with commit 657810f Dec 18, 2024
11 checks passed
@mathias-vandaele mathias-vandaele deleted the pham-minh-nguyen-fix-smtp-mailconnector branch December 18, 2024 14:55
@sbuettner
Copy link
Contributor

Backport failed for release/8.6, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin release/8.6
git worktree add -d .worktree/backport-3796-to-release/8.6 origin/release/8.6
cd .worktree/backport-3796-to-release/8.6
git switch --create backport-3796-to-release/8.6
git cherry-pick -x 657810f4bb25bf2da74bd19c0e7906a95aa934a3

itsnuyen added a commit that referenced this pull request Dec 19, 2024
…error (#3796)

* fix(mail-connector): check htmlBody and/or body is null and throw an error
if not throw an error, connector will retry it over and over again.

* chore(mail-connector): build the assertation inside the `SmtpSendMail` record

* test(mail-connector): adding unit test to validate `isEmailMessageValid`

* chore(mail-connector): run mvn spotless to format the `SmtpSendEmailTest` class

* chore(mail-connector): use mvn:licence to add the licence to the file

(cherry picked from commit 657810f)
mathias-vandaele pushed a commit that referenced this pull request Dec 20, 2024
…error (#3796)

* fix(mail-connector): check htmlBody and/or body is null and throw an error
if not throw an error, connector will retry it over and over again.

* chore(mail-connector): build the assertation inside the `SmtpSendMail` record

* test(mail-connector): adding unit test to validate `isEmailMessageValid`

* chore(mail-connector): run mvn spotless to format the `SmtpSendEmailTest` class

* chore(mail-connector): use mvn:licence to add the licence to the file

(cherry picked from commit 657810f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants