Skip to content

Commit

Permalink
fix(sendgrid): Sendgrid Email notification returns errors but task ma…
Browse files Browse the repository at this point in the history
…rked as succeeded

fix api

throw exception if status code is not 2xx

#178
  • Loading branch information
mgabelle committed Nov 13, 2024
1 parent bb6d207 commit b50b0f1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ public SendGridMailSend.Output run(RunContext runContext) throws Exception {

Logger logger = runContext.logger();

logger.debug("Sending an email to {}", to);
logger.debug("Sending an email to {}", runContext.render(to));

Mail mail = new Mail();

Personalization personalization = new Personalization();

Email fromEmail = new Email(runContext.render(this.from));
personalization.setFrom(fromEmail);
mail.setFrom(fromEmail);

Personalization personalization = new Personalization();

runContext.render(this.to).stream().map(Email::new).forEach(personalization::addTo);

Expand Down Expand Up @@ -196,6 +196,10 @@ public SendGridMailSend.Output run(RunContext runContext) throws Exception {
Map<String, String> headers = api.getHeaders();
int statusCode = api.getStatusCode();

if (statusCode/100 != 2) {
throw new RuntimeException("SendGrid API failed with status code: " + statusCode + " and body: " + body);
}

return Output.builder().body(body).headers(headers).statusCode(statusCode).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@Disabled("Need a SendGrid API key")
@KestraTest
public class SendGridMailSendTest {

Expand Down Expand Up @@ -86,7 +87,6 @@ private RunContext getRunContext() {
}

@Test
@Disabled("Need a SendGrid API key")
@DisplayName("Send email with html and plain text contents")
void sendEmail() throws Exception {
RunContext runContext = getRunContext();
Expand Down Expand Up @@ -115,7 +115,7 @@ void sendEmail() throws Exception {

SendGridMailSend.Output output = mailSend.run(runContext);

assertThat(output.getStatusCode(), is(200));
assertThat(output.getStatusCode(), is(202));

String body = IOUtils.toString(output.getBody().getBytes(), String.valueOf(StandardCharsets.UTF_8));

Expand Down

0 comments on commit b50b0f1

Please sign in to comment.