Skip to content

Commit

Permalink
feat: 🥅 better error handling with edgedb's built-in errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JitPackJoyride committed Sep 27, 2023
1 parent 1cb5073 commit e310646
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
36 changes: 13 additions & 23 deletions src/edgedb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { Client } from "edgedb";
import {
ConstraintViolationError,
MissingRequiredError,
type Client,
} from "edgedb";
import type {
Adapter,
GlobalDatabaseSessionAttributes,
Expand Down Expand Up @@ -82,13 +86,9 @@ export const edgedbAdapter = (
await keyInsertQuery.run(tx);
});
} catch (error) {
// Catch duplicate key errors
type ErrorCasting = { message?: string };
if (
(error as ErrorCasting).message &&
(error as ErrorCasting).message?.includes(
`${modelNames.key}: key_id`
)
error instanceof ConstraintViolationError &&
error.message.includes(`${modelNames.key}: key_id`)
) {
throw new LuciaError("AUTH_DUPLICATE_KEY_ID");
}
Expand Down Expand Up @@ -169,15 +169,10 @@ export const edgedbAdapter = (
});
await query.run(client);
} catch (error) {
// Catch invalid user id errors
type ErrorCasting = { message?: string };

// Catch invalid user id errors
if (
(error as ErrorCasting).message &&
(error as ErrorCasting).message?.includes(
`missing value for required link 'user'`
)
error instanceof MissingRequiredError &&
error.message.includes(`missing value for required link 'user'`)
) {
throw new LuciaError("AUTH_INVALID_USER_ID");
}
Expand Down Expand Up @@ -256,23 +251,18 @@ export const edgedbAdapter = (
});
await query.run(client);
} catch (error) {
type ErrorCasting = { message?: string };
// Catch duplicate key id error
if (
(error as ErrorCasting).message &&
(error as ErrorCasting).message?.includes(
`${modelNames.key}: key_id`
)
error instanceof ConstraintViolationError &&
error.message.includes(`${modelNames.key}: key_id`)
) {
throw new LuciaError("AUTH_DUPLICATE_KEY_ID");
}

// Catch invalid user id errors
if (
(error as ErrorCasting).message &&
(error as ErrorCasting).message?.includes(
`missing value for required link 'user'`
)
error instanceof MissingRequiredError &&
error.message.includes(`missing value for required link 'user'`)
) {
throw new LuciaError("AUTH_INVALID_USER_ID");
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { edgedbAdapter } from "./edgedb.ts";
export { edgedbAdapter } from "./edgedb";

0 comments on commit e310646

Please sign in to comment.