2025-09-14 18:12:29 +00:00
|
|
|
import { ConvexError } from "convex/values"
|
|
|
|
|
|
2025-11-08 17:56:28 +00:00
|
|
|
export enum ErrorCode {
|
2025-09-21 22:24:24 +00:00
|
|
|
Conflict = "Conflict",
|
2025-09-14 18:12:29 +00:00
|
|
|
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",
|
2025-09-14 18:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-08 17:56:28 +00:00
|
|
|
export type ApplicationErrorData = { code: ErrorCode; message?: string }
|
2025-09-14 18:12:29 +00:00
|
|
|
|
2025-11-08 17:56:28 +00:00
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
)
|
2025-09-14 18:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-08 17:56:28 +00:00
|
|
|
export function createErrorData(
|
|
|
|
|
code: ErrorCode,
|
|
|
|
|
message?: string,
|
|
|
|
|
): ApplicationErrorData {
|
2025-09-25 23:12:13 +00:00
|
|
|
return {
|
|
|
|
|
code,
|
|
|
|
|
message:
|
2025-11-08 17:56:28 +00:00
|
|
|
code === ErrorCode.Internal
|
|
|
|
|
? "Internal application error"
|
|
|
|
|
: message,
|
2025-09-25 23:12:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-08 17:56:28 +00:00
|
|
|
|
|
|
|
|
export function error(data: ApplicationErrorData): never {
|
|
|
|
|
throw new ConvexError(data)
|
|
|
|
|
}
|