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,7 +1,9 @@
import {
Code as ErrorCode,
type ApplicationErrorData,
ErrorCode,
isApplicationError,
} from "@fileone/convex/error"
import { ConvexError } from "convex/values"
import { toast } from "sonner"
const ERROR_MESSAGE = {
@@ -9,13 +11,19 @@ const ERROR_MESSAGE = {
[ErrorCode.FileExists]: "File already exists",
[ErrorCode.Internal]: "Internal application error",
[ErrorCode.Conflict]: "Conflict",
[ErrorCode.DirectoryNotFound]: "Directory not found",
[ErrorCode.FileNotFound]: "File not found",
[ErrorCode.Unauthenticated]: "Unauthenticated",
[ErrorCode.NotFound]: "Not found",
[ErrorCode.StorageQuotaExceeded]: "Storage is full",
} as const
export function isApplicationConvexError(
error: unknown,
): error is ConvexError<ApplicationErrorData> {
return error instanceof ConvexError && isApplicationError(error.data)
}
export function formatError(error: unknown): string {
if (isApplicationError(error)) {
if (isApplicationConvexError(error)) {
return ERROR_MESSAGE[error.data.code]
}
if (error instanceof Error) {
@@ -25,8 +33,12 @@ export function formatError(error: unknown): string {
}
export function defaultOnError(error: unknown) {
console.log(error)
toast.error(formatError(error))
if (isApplicationConvexError(error)) {
toast.error(formatError(error))
} else {
console.error("Catastrophic error:", error)
toast.error("An unexpected error occurred")
}
}
export function withDefaultOnError(fn: (error: unknown) => void) {