Skip to content

Commit

Permalink
fix request failure handling
Browse files Browse the repository at this point in the history
  • Loading branch information
WhatCats committed Jul 29, 2024
1 parent c6d763b commit f05eede
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/lib/apis/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface RequestOptions extends RequestInit {

export class RequestError extends Error {
name = "RequestError"

constructor(msg: string, cause?: Error) {
super(msg, { cause })
}
}

export class TimeoutError extends RequestError {
Expand All @@ -29,9 +33,12 @@ export async function request(url: string, options: RequestOptions = {}): Promis
const timeoutId = setTimeout(() => controller.abort(), (options.timeout || 10) * 1000)
if (options.urlParams) url += `?${new URLSearchParams(options.urlParams)}`

function requestError(): Response {
function requestError(error: Error): Response {
if (controller.signal.aborted) throw new TimeoutError("Server took too long to respond")
throw new RequestError("Network unavailable")
if (error instanceof TypeError) {
error = error.cause as Error
}
throw new RequestError("Request Failed", error)
}

return fetch(url, { ...options, signal: controller.signal, cache: "no-cache" })
Expand Down
12 changes: 11 additions & 1 deletion src/lib/discord/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,17 @@ export class CommandHandler {
if (error instanceof DiscordAPIError && [10062].includes(error.code as number)) return

if (!(error instanceof UserError) && !(error instanceof LocalizedError))
console.error("Unexpected error while handling a command!", error)
console.error(
"Unexpected error while handling a command!",
{
command: interaction.path,
type: interaction.type,
user: interaction.user.id,
channel: interaction.channelId,
guild: interaction.guildId,
},
error,
)

// Give the user that error message they were missing in their life
if (
Expand Down
3 changes: 2 additions & 1 deletion src/modules/council/list-members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ SlashCommand({
option
.setName(Options.Rank)
.setDescription("The rank to list the members of.")
.setChoices(Object.values(RANKS).map((v) => ({ name: v, value: v }))),
.setChoices(Object.values(RANKS).map((v) => ({ name: v, value: v })))
.setRequired(true),
),

config: { permissions: COUNCIL_PERMISSIONS },
Expand Down
8 changes: 5 additions & 3 deletions src/modules/rank-apps/CouncilVoteManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Component({
async handler(interaction) {
const { ticketManager, ticket } = await RankAppTicketManager.findTicket<RankAppExtras>(interaction)
if (!(ticketManager instanceof RankAppTicketManager))
throw new Error(`${interaction.customId} in non RankAppTicketManager channel!`)
throw new UserError(`This interaction is not available in this channel.`)

if (!interaction.userHasPosition(`${ticketManager.rank} Council`))
throw new LocalizedError("command_handler.missing_permissions")
Expand All @@ -144,10 +144,12 @@ Component({
if (isNaN(vote)) throw new Error(`Got invalid vote value of ${vote} from ${interaction.customId}!`)

await ticket.updateOne({ $set: { [`extras.votes.${interaction.user.id}`]: vote } })
ticket.extras!.votes[interaction.user.id] = vote

if (!ticket.extras) ticket.extras = { votes: {} }
ticket.extras.votes[interaction.user.id] = vote

await interaction.update(
ticketManager.vote.buildVoteMessage(ticket.user(), interaction.guild!, ticket.extras!.votes),
ticketManager.vote.buildVoteMessage(ticket.user(), interaction.guild!, ticket.extras.votes),
)
},
})
Expand Down
5 changes: 3 additions & 2 deletions src/modules/vouch-system/public-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { ApplicationCommandOptionChoiceData, SlashCommandStringOption, User } fr
import {
ContextMenu,
ContextMenuInteraction,
HTTPError,
LocalizedContextMenuCommandBuilder,
LocalizedError,
LocalizedSlashCommandBuilder,
RequestError,
SlashCommand,
SlashCommandInteraction,
TimeoutError,
Expand Down Expand Up @@ -156,8 +156,9 @@ async function finishVouchesInteraction(
async function fetchUserId(ign: string) {
const url = `https://api.scrims.network/v1/user?${new URLSearchParams({ username: ign })}`
const resp = await request(url).catch((error) => {
if (error instanceof HTTPError) throw new LocalizedError(`api.request_failed`, "Scrims Network API")
if (error instanceof TimeoutError) throw new LocalizedError("api.timeout", "Scrims Network API")
if (error instanceof RequestError)
throw new LocalizedError(`api.request_failed`, "Scrims Network API")
throw error
})

Expand Down

0 comments on commit f05eede

Please sign in to comment.