Files
drive/packages/convex/shared/error.ts
kenneth acfe1523df refactor: wrap all errors in ConvexError
- Update error() helper to throw ConvexError instead of plain objects
- Add isApplicationConvexError() type guard for client-side error checking
- Fix Vite config to include convex/values in optimizeDeps for proper instanceof checks
- Update error handling to check ConvexError wrapper and extract data property

This ensures all application errors are properly typed and can be identified
using instanceof ConvexError on the client side.

Co-authored-by: Ona <no-reply@ona.com>
2025-11-08 17:56:28 +00:00

45 lines
963 B
TypeScript

import { ConvexError } from "convex/values"
export enum ErrorCode {
Conflict = "Conflict",
DirectoryExists = "DirectoryExists",
FileExists = "FileExists",
Internal = "Internal",
Unauthenticated = "Unauthenticated",
NotFound = "NotFound",
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)
}