-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support try-catch for both `invoke` and `run` If an exception is thrown in the `invoke` or `run` function, it will now be caught and returned as a `StepError` exception. This will allow the error to be propagated to the caller and handled appropriately. Transforming the errors into a `StepError` exception is following the Inngest SDK spec: https://github.com/inngest/inngest/blob/main/docs/SDK_SPEC.md#522-memoizing-a-step Inngest JS SDK behavior: https://github.com/inngest/inngest-js/blob/4f91d9c302592ecc2228914469dd057ae148005b/packages/inngest/src/components/execution/v1.ts#L437-L443 Inngest documentation: https://www.inngest.com/docs/features/inngest-functions/error-retries/inngest-errors#step-errors * Implement two example functions demonstrating try-catching * Add tests for try-catching in `run` and `invoke` functions * Remove the @BeforeAll method from `StepErrorsIntegrationTest` * Extract Step status codes set into a const
- Loading branch information
Showing
14 changed files
with
272 additions
and
9 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...ot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/InvokeFailureFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.inngest.springbootdemo.testfunctions; | ||
|
||
import com.inngest.*; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.LinkedHashMap; | ||
|
||
public class InvokeFailureFunction extends InngestFunction { | ||
|
||
@NotNull | ||
@Override | ||
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { | ||
return builder | ||
.id("invoke-failure-fn") | ||
.name("Invoke Function") | ||
.triggerEvent("test/invoke.failure"); | ||
} | ||
|
||
@Override | ||
public String execute(FunctionContext ctx, Step step) { | ||
try { | ||
step.invoke( | ||
"failing-function", | ||
"spring_test_demo", | ||
"non-retriable-fn", | ||
new LinkedHashMap<String, | ||
String>(), | ||
null, | ||
Object.class); | ||
} catch (StepError e) { | ||
return e.getMessage(); | ||
} | ||
|
||
return "An error should have been thrown and this message should not be returned"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/TryCatchRunFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.inngest.springbootdemo.testfunctions; | ||
|
||
import com.inngest.*; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
class CustomException extends RuntimeException { | ||
public CustomException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
public class TryCatchRunFunction extends InngestFunction { | ||
|
||
@NotNull | ||
@Override | ||
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { | ||
return builder | ||
.id("try-catch-run-fn") | ||
.name("Try catch run") | ||
.triggerEvent("test/try.catch.run") | ||
.retries(0); | ||
} | ||
|
||
@Override | ||
public String execute(FunctionContext ctx, Step step) { | ||
try { | ||
step.run("fail-step", () -> { | ||
throw new CustomException("Something fatally went wrong"); | ||
}, String.class); | ||
} catch (StepError e) { | ||
return e.getMessage(); | ||
} | ||
|
||
return "An error should have been thrown and this message should not be returned"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...-spring-boot-demo/src/test/java/com/inngest/springbootdemo/StepErrorsIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.inngest.springbootdemo; | ||
|
||
import com.inngest.Inngest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
@IntegrationTest | ||
@Execution(ExecutionMode.CONCURRENT) | ||
class StepErrorsIntegrationTest { | ||
@Autowired | ||
private DevServerComponent devServer; | ||
|
||
static int sleepTime = 5000; | ||
|
||
@Autowired | ||
private Inngest client; | ||
|
||
@Test | ||
void testShouldCatchStepErrorWhenInvokeThrows() throws Exception { | ||
String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/invoke.failure").first(); | ||
|
||
Thread.sleep(sleepTime); | ||
|
||
RunEntry<Object> run = devServer.runsByEvent(eventId).first(); | ||
String output = (String) run.getOutput(); | ||
|
||
assertEquals("Completed", run.getStatus() ); | ||
assertNotNull(run.getEnded_at()); | ||
|
||
assertEquals("Something fatally went wrong", output); | ||
} | ||
|
||
@Test | ||
void testShouldCatchStepErrorWhenRunThrows() throws Exception { | ||
String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/try.catch.run").first(); | ||
|
||
Thread.sleep(sleepTime); | ||
|
||
RunEntry<Object> run = devServer.runsByEvent(eventId).first(); | ||
String output = (String) run.getOutput(); | ||
|
||
assertEquals("Completed", run.getStatus()); | ||
assertNotNull(run.getEnded_at()); | ||
|
||
assertEquals("Something fatally went wrong", output); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
inngest-test-server/src/main/kotlin/com/inngest/testserver/ImageFromPrompt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.inngest.testserver | ||
|
||
import com.inngest.* | ||
|
||
class ImageFromPrompt : InngestFunction() { | ||
override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder = | ||
builder | ||
.id("ImageFromPrompt") | ||
.name("Image from Prompt") | ||
.triggerEvent("media/prompt.created") | ||
|
||
override fun execute( | ||
ctx: FunctionContext, | ||
step: Step, | ||
): String { | ||
val imageURL = | ||
try { | ||
step.run("generate-image-dall-e") { | ||
// Call the DALL-E model to generate an image | ||
throw Exception("Failed to generate image") | ||
|
||
"example.com/image-dall-e.jpg" | ||
} | ||
} catch (e: StepError) { | ||
// Fall back to a different image generation model | ||
step.run("generate-image-midjourney") { | ||
// Call the MidJourney model to generate an image | ||
"example.com/image-midjourney.jpg" | ||
} | ||
} | ||
|
||
try { | ||
step.invoke<Map<String, Any>>( | ||
"push-to-slack-channel", | ||
"ktor-dev", | ||
"PushToSlackChannel", | ||
mapOf("image" to imageURL), | ||
null, | ||
) | ||
} catch (e: StepError) { | ||
// Pushing to Slack is not critical, so we can ignore the error, log it | ||
// or handle it in some other way. | ||
} | ||
|
||
return imageURL | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
inngest-test-server/src/main/kotlin/com/inngest/testserver/PushToSlackChannel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.inngest.testserver | ||
|
||
import com.inngest.* | ||
|
||
class PushToSlackChannel : InngestFunction() { | ||
override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder = | ||
builder | ||
.id("PushToSlackChannel") | ||
.name("Push to Slack Channel") | ||
.triggerEvent("media/image.generated") | ||
|
||
override fun execute( | ||
ctx: FunctionContext, | ||
step: Step, | ||
): String = | ||
step.run("push-to-slack-channel") { | ||
// Call Slack API to push the image to a channel | ||
throw NonRetriableError("Failed to push image to Slack channel ${ctx.event.data["image"]}") | ||
|
||
"Image pushed to Slack channel" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.