From d5efce24d7f78b9f2c70f8a377df6719af8907da Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Thu, 12 Sep 2024 14:47:04 -0400 Subject: [PATCH] Use Sequelize's error types. --- src/database.ts | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/database.ts b/src/database.ts index 4c6a59c..78f7ee9 100644 --- a/src/database.ts +++ b/src/database.ts @@ -1,4 +1,4 @@ -import { Model, Op, QueryTypes, Sequelize, WhereOptions } from "sequelize"; +import { BaseError, Model, Op, QueryTypes, Sequelize, UniqueConstraintError, WhereOptions } from "sequelize"; import dotenv from "dotenv"; import { @@ -39,8 +39,6 @@ import { Question } from "./models/question"; import { logger } from "./logger"; import { Stage } from "./models/stage"; -type SequelizeError = { parent: { code: string } }; - export type LoginResponse = { type: "none" | "student" | "educator" | "admin", result: LoginResult; @@ -90,23 +88,19 @@ setUpAssociations(); // For now, this just distinguishes between duplicate account creation and other errors // We can flesh this out layer -function signupResultFromError(error: SequelizeError): SignUpResult { - const code = error.parent.code; - switch (code) { - case "ER_DUP_ENTRY": - return SignUpResult.EmailExists; - default: - return SignUpResult.Error; +function signupResultFromError(error: BaseError): SignUpResult { + if (error instanceof UniqueConstraintError) { + return SignUpResult.EmailExists; + } else { + return SignUpResult.Error; } } -function createClassResultFromError(error: SequelizeError): CreateClassResult { - const code = error.parent.code; - switch (code) { - case "ER_DUP_ENTRY": - return CreateClassResult.AlreadyExists; - default: - return CreateClassResult.Error; +function createClassResultFromError(error: BaseError): CreateClassResult { + if (error instanceof UniqueConstraintError) { + return CreateClassResult.AlreadyExists; + } else { + return CreateClassResult.Error; } }