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>
This commit is contained in:
2025-11-08 17:56:28 +00:00
parent 9b8367ade4
commit acfe1523df
3 changed files with 45 additions and 25 deletions

View File

@@ -1,36 +1,44 @@
import { ConvexError } from "convex/values"
export enum Code {
export enum ErrorCode {
Conflict = "Conflict",
DirectoryExists = "DirectoryExists",
DirectoryNotFound = "DirectoryNotFound",
FileExists = "FileExists",
FileNotFound = "FileNotFound",
Internal = "Internal",
Unauthenticated = "Unauthenticated",
NotFound = "NotFound",
StorageQuotaExceeded = "StorageQuotaExceeded",
}
export type ApplicationErrorData = { code: Code; message?: string }
export type ApplicationError = ConvexError<ApplicationErrorData>
export type ApplicationErrorData = { code: ErrorCode; message?: string }
export function isApplicationError(error: unknown): error is ApplicationError {
return error instanceof ConvexError && "code" in error.data
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 create(code: Code, message?: string): ApplicationError {
return new ConvexError({
code,
message:
code === Code.Internal ? "Internal application error" : message,
})
}
export function createJson(code: Code, message?: string): ApplicationErrorData {
export function createErrorData(
code: ErrorCode,
message?: string,
): ApplicationErrorData {
return {
code,
message:
code === Code.Internal ? "Internal application error" : message,
code === ErrorCode.Internal
? "Internal application error"
: message,
}
}
export function error(data: ApplicationErrorData): never {
throw new ConvexError(data)
}