Skip to content

Commit

Permalink
fix: add error handling to schema decoding
Browse files Browse the repository at this point in the history
Introduce try-catch blocks for schema decoding to handle errors gracefully.
  • Loading branch information
gentlementlegen committed Oct 25, 2024
1 parent c9b92bc commit 7b15aa1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/sdk/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,24 @@ export async function createActionsPlugin<TConfig = unknown, TEnv = unknown, TSu

let config: TConfig;
if (pluginOptions.settingsSchema) {
config = Value.Decode(pluginOptions.settingsSchema, Value.Default(pluginOptions.settingsSchema, JSON.parse(inputs.settings)));
try {
config = Value.Decode(pluginOptions.settingsSchema, Value.Default(pluginOptions.settingsSchema, JSON.parse(inputs.settings)));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.settingsSchema, JSON.parse(inputs.settings)), { depth: null });
throw e;
}
} else {
config = JSON.parse(inputs.settings) as TConfig;
}

let env: TEnv;
if (pluginOptions.envSchema) {
env = Value.Decode(pluginOptions.envSchema, Value.Default(pluginOptions.envSchema, process.env));
try {
env = Value.Decode(pluginOptions.envSchema, Value.Default(pluginOptions.envSchema, process.env));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.envSchema, process.env), { depth: null });
throw e;
}
} else {
env = process.env as TEnv;
}
Expand Down
15 changes: 13 additions & 2 deletions src/sdk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Context } from "./context";
import { customOctokit } from "./octokit";
import { verifySignature } from "./signature";
import { sanitizeMetadata } from "./util";
import { env as honoEnv } from "hono/adapter";

interface Options {
kernelPublicKey?: string;
Expand Down Expand Up @@ -52,14 +53,24 @@ export async function createPlugin<TConfig = unknown, TEnv = unknown, TSupported

let config: TConfig;
if (pluginOptions.settingsSchema) {
config = Value.Decode(pluginOptions.settingsSchema, Value.Default(pluginOptions.settingsSchema, payload.settings));
try {
config = Value.Decode(pluginOptions.settingsSchema, Value.Default(pluginOptions.settingsSchema, payload.settings));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.settingsSchema, payload.settings), { depth: null });
throw e;
}
} else {
config = payload.settings as TConfig;
}

let env: TEnv;
if (pluginOptions.envSchema) {
env = Value.Decode(pluginOptions.envSchema, Value.Default(pluginOptions.envSchema, ctx.env));
try {
env = Value.Decode(pluginOptions.envSchema, Value.Default(pluginOptions.envSchema, honoEnv(ctx)));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.envSchema, ctx.env), { depth: null });
throw e;
}
} else {
env = ctx.env as TEnv;
}
Expand Down

0 comments on commit 7b15aa1

Please sign in to comment.