Skip to content

Commit d00d432

Browse files
committed
fix: handle unknown errors in cards route
1 parent a9a1a85 commit d00d432

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

apps/backend/src/routes/cards.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,17 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
8080
try {
8181
const card = await cardService.createCard(app, userId, parsed.data)
8282
return reply.status(201).send(card)
83-
} catch (error: unknown) {
84-
if (error?.code === 'OWNERSHIP') {return reply.status(403).send({ error: 'One or more links do not belong to your account' })}
85-
return handleDbError(error, request, reply)
86-
}
83+
} catch (error: unknown) {
84+
const err = error as { code?: string };
85+
86+
if (err.code === 'OWNERSHIP') {
87+
return reply.status(403).send({
88+
error: 'One or more links do not belong to your account',
89+
});
90+
}
91+
92+
return handleDbError(error, request, reply);
93+
}
8794
});
8895

8996
// ─── Update Card ───
@@ -98,10 +105,17 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
98105
const updated = await cardService.updateCard(app, userId, id, parsed.data)
99106
if (!updated) {return reply.status(404).send({ error: 'Card not found' })}
100107
return updated
101-
} catch (error: unknown) {
102-
if (error?.code === 'OWNERSHIP') {return reply.status(403).send({ error: 'One or more links do not belong to your account' })}
103-
return handleDbError(error, request, reply)
104-
}
108+
} catch (error: unknown) {
109+
const err = error as { code?: string };
110+
111+
if (err.code === 'OWNERSHIP') {
112+
return reply.status(403).send({
113+
error: 'One or more links do not belong to your account',
114+
});
115+
}
116+
117+
return handleDbError(error, request, reply);
118+
}
105119
});
106120

107121
// ─── Delete Card ───
@@ -114,17 +128,20 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
114128
await cardService.deleteCard(app, userId, id)
115129
return reply.status(204).send()
116130
} catch (error: unknown) {
117-
if (error?.code === 'NOT_FOUND') {
131+
const err = error as { code?: string };
132+
133+
if (err.code === 'NOT_FOUND') {
118134
return reply.status(404).send({ error: 'Card not found' });
119135
}
120136

121-
if (error?.code === 'LAST_CARD') {
137+
if (err.code === 'LAST_CARD') {
122138
return reply.status(400).send({
123139
error: 'Cannot delete the last remaining card. A user must have at least one card.',
124140
});
125141
}
126-
return handleDbError(error, request, reply)
127-
}
142+
143+
return handleDbError(error, request, reply);
144+
}
128145
});
129146

130147
// ─── Set Default Card ───

0 commit comments

Comments
 (0)