Files
drive/packages/convex/shared/error.ts

45 lines
963 B
TypeScript
Raw Permalink Normal View History

import { ConvexError } from "convex/values"
export enum ErrorCode {
Conflict = "Conflict",
DirectoryExists = "DirectoryExists",
FileExists = "FileExists",
Internal = "Internal",
2025-09-15 21:44:41 +00:00
Unauthenticated = "Unauthenticated",
2025-10-12 14:31:02 +00:00
NotFound = "NotFound",
2025-11-02 18:12:33 +00:00
StorageQuotaExceeded = "StorageQuotaExceeded",
}
export type ApplicationErrorData = { code: ErrorCode; message?: string }
export function isApplicationError(
error: unknown,
): error is ApplicationErrorData {
return (
error !== null &&
typeof error === "object" &&
"code" in error &&
"message" in error &&
Object.values(ErrorCode).includes(
(error as { code: string }).code as ErrorCode,
)
)
}
export function createErrorData(
code: ErrorCode,
message?: string,
): ApplicationErrorData {
return {
code,
message:
code === ErrorCode.Internal
? "Internal application error"
: message,
}
}
export function error(data: ApplicationErrorData): never {
throw new ConvexError(data)
}