Skip to content

Commit

Permalink
Add Prisma and Zod error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jthoward64 committed Jul 22, 2024
1 parent 77454ff commit 36c635a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
4 changes: 4 additions & 0 deletions packages/common/lib/error/errorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ export const InvariantViolation = Symbol("InvariantViolation");
export type InvariantViolation = typeof InvariantViolation;
export const HttpError = Symbol("HttpError");
export type HttpError = typeof HttpError;
export const PrismaError = Symbol("PrimaError");
export type PrismaError = typeof PrismaError;
export const ZodError = Symbol("ZodError");
export type ZodError = typeof ZodError;
11 changes: 3 additions & 8 deletions packages/server/src/lib/error/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
PrismaClientUnknownRequestError,
PrismaClientValidationError,
} from "@prisma/client/runtime/library";
import { ConcreteError } from "@ukdanceblue/common/error";
import { ConcreteError, ErrorCode } from "@ukdanceblue/common/error";
import type { Option } from "ts-results-es";
import { None, Some } from "ts-results-es";

Expand All @@ -16,8 +16,6 @@ type RawPrismaError =
| PrismaClientInitializationError
| PrismaClientValidationError;

const PrismaErrorTag = Symbol("PrismaError");
type PrismaErrorTag = typeof PrismaErrorTag;
export abstract class PrismaError extends ConcreteError {
readonly error: RawPrismaError;

Expand All @@ -38,11 +36,8 @@ export abstract class PrismaError extends ConcreteError {
return false;
}

static get Tag(): PrismaErrorTag {
return PrismaErrorTag;
}
get tag(): PrismaErrorTag {
return PrismaErrorTag;
get tag(): ErrorCode.PrismaError {
return ErrorCode.PrismaError;
}
}

Expand Down
12 changes: 3 additions & 9 deletions packages/server/src/lib/error/zod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ConcreteError } from "@ukdanceblue/common/error";
import { ConcreteError, ErrorCode } from "@ukdanceblue/common/error";
import type { ZodError as RawZodError } from "zod";

const ZodErrorTag = Symbol("ZodError");
type ZodErrorTag = typeof ZodErrorTag;
export class ZodError extends ConcreteError {
readonly error: RawZodError;
constructor(error: RawZodError) {
Expand All @@ -16,10 +13,7 @@ export class ZodError extends ConcreteError {
return false;
}

static get Tag() {
return ZodErrorTag;
}
get tag(): ZodErrorTag {
return ZodErrorTag;
get tag(): ErrorCode.ZodError {
return ErrorCode.ZodError;
}
}
6 changes: 2 additions & 4 deletions packages/server/src/lib/formatError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,20 @@ export function formatError(
...originalFormattedError,
extensions: {
...originalFormattedError.extensions,
code: ErrorCode.Unknown,
code: ErrorCode.Unknown.description,
stacktrace:
shouldIncludeSensitiveInfo &&
Array.isArray(originalFormattedError.extensions?.stacktrace)
? originalFormattedError.extensions.stacktrace.map(String)
: [],
clientActions: [],
internalDetails: {},
},
};

if (
error instanceof jwt.NotBeforeError ||
error instanceof jwt.TokenExpiredError
) {
formattedError.extensions.code = ErrorCode.Unauthenticated;
formattedError.extensions.code = ErrorCode.Unauthenticated.description;
} else if (typeof error === "string") {
formattedError.message = error;
} else if (typeof error === "object" && error != null) {
Expand Down

0 comments on commit 36c635a

Please sign in to comment.