Skip to content

Commit

Permalink
Fix error messages on discord interaction responses (#16)
Browse files Browse the repository at this point in the history
* Run `deno task all`

Updates

Co-Authored-By: Ethan Davidson <[email protected]>

* Add error messages to discord interaction response

Co-Authored-By: Ethan Davidson <[email protected]>

* typo

Co-Authored-By: Ethan Davidson <[email protected]>

---------

Co-authored-by: Ethan Davidson <[email protected]>
  • Loading branch information
karnikaavelumani and EthanThatOneKid authored Sep 2, 2023
1 parent ee20196 commit 98a2e69
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 39 deletions.
10 changes: 6 additions & 4 deletions api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export function makeAPIRouter(
return new router.Router()
.post(
new URLPattern({ pathname: "/" }),
discord_app.makeDiscordAppHandler(
leaderboardClient,
discordPublicKey,
discordChannelID,
discord_app.withErrorResponse(
discord_app.makeDiscordAppHandler(
leaderboardClient,
discordPublicKey,
discordChannelID,
),
),
)
.post(
Expand Down
29 changes: 29 additions & 0 deletions api/discord_app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,32 @@ function makeSubmitSubcommandHandler(
return makeSubmitInteractionResponse(submitResponse);
};
}

/**
* withErrorResponse wraps around the Discord app handler to catch any errors
* and return a response using the error message.
*/
export function withErrorResponse(
oldHandle: router.RouterHandler["handle"],
): router.RouterHandler["handle"] {
return async function handle(
request: router.RouterRequest,
): Promise<Response> {
return await oldHandle(request)
.catch((error) => {
if (!(error instanceof Error)) {
throw error;
}

return Response.json(
{
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `Error: ${error.message}`,
flags: MessageFlags.Ephemeral,
},
} satisfies APIInteractionResponse,
);
});
};
}
66 changes: 34 additions & 32 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export {
assertEquals,
assertRejects,
} from "https://deno.land/std@0.200.0/assert/mod.ts";
} from "https://deno.land/std@0.201.0/assert/mod.ts";

export { load } from "https://deno.land/std@0.200.0/dotenv/mod.ts";
export * from "https://deno.land/std@0.200.0/datetime/constants.ts";
export { load } from "https://deno.land/std@0.201.0/dotenv/mod.ts";
export * from "https://deno.land/std@0.201.0/datetime/constants.ts";
export { ulid } from "https://deno.land/x/[email protected]/mod.ts";
export type {
APIApplicationCommandInteractionDataOption,
Expand Down

0 comments on commit 98a2e69

Please sign in to comment.