-
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.
Implement two example functions demonstrating try-catching
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
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" | ||
} | ||
} |