refactor: standardize error handling with toError() helper

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dorian
2026-03-08 21:44:17 +00:00
co-authored by Claude Opus 4.6
parent ea716c1f0e
commit 4d1e592365
4 changed files with 9 additions and 6 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
import { db, schema } from '../db/index.js' import { db, schema } from '../db/index.js'
import { runMockFight } from './mock.js' import { runMockFight } from './mock.js'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { pick } from '../lib/utils.js' import { pick, toError } from '../lib/utils.js'
import { fightEvents } from './events.js' import { fightEvents } from './events.js'
export interface FightResult { export interface FightResult {
@@ -137,7 +137,7 @@ export async function startFightLoop(options: FightLoopOptions = {}): Promise<vo
} }
} catch (err) { } catch (err) {
if (onError) { if (onError) {
onError(err instanceof Error ? err : new Error(String(err))) onError(toError(err))
} else { } else {
console.error('[fight-loop] error:', err) console.error('[fight-loop] error:', err)
} }
+2 -1
View File
@@ -1,4 +1,5 @@
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { toError } from '../lib/utils.js'
import { db, schema, sqlite } from '../db/index.js' import { db, schema, sqlite } from '../db/index.js'
import { eq, sql } from 'drizzle-orm' import { eq, sql } from 'drizzle-orm'
import { randomArena, type Arena } from './arenas.js' import { randomArena, type Arena } from './arenas.js'
@@ -207,7 +208,7 @@ async function callWebhook(
} catch (err: unknown) { } catch (err: unknown) {
const elapsed = Date.now() - start const elapsed = Date.now() - start
const isAbort = err instanceof Error && err.name === 'AbortError' const isAbort = err instanceof Error && err.name === 'AbortError'
console.log(`[webhook] ${url} ${isAbort ? "TIMEOUT" : "ERROR"} in ${elapsed}ms: ${err instanceof Error ? err.message : err}`) console.log(`[webhook] ${url} ${isAbort ? "TIMEOUT" : "ERROR"} in ${elapsed}ms: ${toError(err).message}`)
return { return {
answer: null, answer: null,
timeMs: elapsed, timeMs: elapsed,
+2 -1
View File
@@ -1,4 +1,5 @@
import { Hono } from 'hono' import { Hono } from 'hono'
import { toError } from '../lib/utils.js'
import { db, schema } from '../db/index.js' import { db, schema } from '../db/index.js'
import { eq, desc } from 'drizzle-orm' import { eq, desc } from 'drizzle-orm'
import { calculateOdds } from '../engine/odds.js' import { calculateOdds } from '../engine/odds.js'
@@ -104,7 +105,7 @@ betsRouter.post('/place', rateLimit(60_000, 10), async (c) => {
potentialPayout: bet.potentialPayout, potentialPayout: bet.potentialPayout,
}) })
} catch (err: unknown) { } catch (err: unknown) {
return c.json({ error: err instanceof Error ? err.message : String(err) }, 400) return c.json({ error: toError(err).message }, 400)
} }
}) })
+3 -2
View File
@@ -1,6 +1,7 @@
import { Hono } from 'hono' import { Hono } from 'hono'
import { db, schema } from '../db/index.js' import { db, schema } from '../db/index.js'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { toError } from '../lib/utils.js'
import { import {
createTournament, createTournament,
joinTournament, joinTournament,
@@ -73,7 +74,7 @@ tournamentsRouter.post('/:id/join', async (c) => {
const entryId = joinTournament(tournamentId, bot.id, body.paymentId) const entryId = joinTournament(tournamentId, bot.id, body.paymentId)
return c.json({ entryId, botId: bot.id }) return c.json({ entryId, botId: bot.id })
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : String(err) const message = toError(err).message
return c.json({ error: message }, 400) return c.json({ error: message }, 400)
} }
}) })
@@ -92,7 +93,7 @@ tournamentsRouter.post('/:id/start', async (c) => {
const bracket = getTournamentBracket(tournamentId) const bracket = getTournamentBracket(tournamentId)
return c.json({ status: 'active', bracket }) return c.json({ status: 'active', bracket })
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : String(err) const message = toError(err).message
return c.json({ error: message }, 400) return c.json({ error: message }, 400)
} }
}) })