-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(equipment): implement error handling for equipment addition, upd…
…ate, and deletion
- Loading branch information
1 parent
f6e0f77
commit d7ca267
Showing
7 changed files
with
126 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { FetchError } from 'ofetch'; | ||
|
||
export default function useApiErrorToast() { | ||
const { addToast } = useToaster(); | ||
|
||
function showErrorToast(error: unknown, title: string) { | ||
if (error instanceof FetchError) { | ||
addToast({ | ||
title, | ||
message: error.data.message | ||
}); | ||
} else { | ||
addToast({ | ||
title: 'Error', | ||
message: 'An unexpected error occurred' | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
showErrorToast | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { eq } from 'drizzle-orm' | ||
import * as v from 'valibot' | ||
|
||
const paramsSchema = v.object({ | ||
itemId: stringToIntegerValidator | ||
}) | ||
|
||
function validateParams(params: unknown) { | ||
return v.parse(paramsSchema, params) | ||
} | ||
|
||
export default defineEventHandler(async (event) => { | ||
await validateAdmin(event) | ||
const { itemId } = await getValidatedRouterParams(event, validateParams) | ||
|
||
const deleted = await event.context.db | ||
.delete(tables.equipment) | ||
.where( | ||
eq(tables.equipment.id, itemId) | ||
) | ||
.returning({ | ||
id: tables.equipment.id | ||
}) | ||
|
||
if (deleted.length === 0) { | ||
throw createError({ | ||
statusCode: 404, | ||
message: `Item with ID ${itemId} not found` | ||
}) | ||
} | ||
|
||
setResponseStatus(event, 204) | ||
}) |
File renamed without changes.
File renamed without changes.