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(sendgrid): Sendgrid Email notification returns errors but task marked as succeeded #179

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
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 @@ -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