Skip to content

Commit

Permalink
Use Sequelize's error types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Sep 12, 2024
1 parent 1f97c48 commit d5efce2
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit d5efce2

Please sign in to comment.