refactor: created custom error for a token already exists
This commit is contained in:
@@ -3,6 +3,7 @@ import { env } from "hono/adapter";
|
||||
import mapKeys from "lodash.mapkeys";
|
||||
|
||||
import { Case, changeStringCase } from "~/libs/changeStringCase";
|
||||
import { TokenAlreadyExistsError } from "~/libs/errors/TokenAlreadyExists";
|
||||
import type { AdminSdkCredentials } from "~/libs/fcm/getGoogleAuthToken";
|
||||
import { verifyFcmToken } from "~/libs/fcm/verifyFcmToken";
|
||||
import { readEnvVariable } from "~/libs/readEnvVariable";
|
||||
@@ -69,7 +70,7 @@ app.openapi(route, async (c) => {
|
||||
|
||||
await saveToken(env(c, "workerd"), deviceId, token, username);
|
||||
} catch (error) {
|
||||
if (error.message === "Token already exists in the database") {
|
||||
if (error instanceof TokenAlreadyExistsError) {
|
||||
return c.json(ErrorResponse, 412);
|
||||
}
|
||||
|
||||
|
||||
5
src/libs/errors/TokenAlreadyExists.ts
Normal file
5
src/libs/errors/TokenAlreadyExists.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export class TokenAlreadyExistsError extends Error {
|
||||
constructor() {
|
||||
super("Token already exists in the database");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { eq, or, sql } from "drizzle-orm";
|
||||
|
||||
import { TokenAlreadyExistsError } from "~/libs/errors/TokenAlreadyExists";
|
||||
import type { Env } from "~/types/env";
|
||||
|
||||
import { getDb } from "./db";
|
||||
@@ -34,7 +35,7 @@ export function saveToken(
|
||||
!existingToken.username &&
|
||||
!username
|
||||
) {
|
||||
throw new Error("Token already exists in the database");
|
||||
throw new TokenAlreadyExistsError();
|
||||
}
|
||||
|
||||
return updateToken(env, deviceId, token, username);
|
||||
|
||||
Reference in New Issue
Block a user