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 (#179)

fix api

throw exception if status code is not 2xx

update test

#178
  • Loading branch information
mgabelle authored Nov 13, 2024
1 parent bb6d207 commit 2606b0e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 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 @@ -41,6 +41,6 @@ void init() throws IOException, URISyntaxException {
void testFlow() throws TimeoutException, QueueException {
Execution execution = runnerUtils.runOne(null, "io.kestra.tests", "sendgrid");
assertThat(execution.getTaskRunList(), hasSize(2));
assertThat(execution.getTaskRunList().get(1).getState().getCurrent(), is(State.Type.SUCCESS));
assertThat(execution.getTaskRunList().get(1).getState().getCurrent(), is(State.Type.FAILED));
}
}
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 2606b0e

Please sign in to comment.