2025-09-14 18:12:29 +00:00
|
|
|
import { ConvexError } from "convex/values"
|
|
|
|
|
|
2025-09-26 22:20:30 +00:00
|
|
|
export enum Code {
|
2025-09-21 22:24:24 +00:00
|
|
|
Conflict = "Conflict",
|
2025-09-14 18:12:29 +00:00
|
|
|
DirectoryExists = "DirectoryExists",
|
2025-09-16 22:36:26 +00:00
|
|
|
DirectoryNotFound = "DirectoryNotFound",
|
2025-09-14 18:12:29 +00:00
|
|
|
FileExists = "FileExists",
|
2025-09-25 23:12:13 +00:00
|
|
|
FileNotFound = "FileNotFound",
|
2025-09-14 18:12:29 +00:00
|
|
|
Internal = "Internal",
|
2025-09-15 21:44:41 +00:00
|
|
|
Unauthenticated = "Unauthenticated",
|
2025-10-12 14:31:02 +00:00
|
|
|
NotFound = "NotFound",
|
2025-09-14 18:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-25 23:12:13 +00:00
|
|
|
export type ApplicationErrorData = { code: Code; message?: string }
|
|
|
|
|
export type ApplicationError = ConvexError<ApplicationErrorData>
|
2025-09-14 18:12:29 +00:00
|
|
|
|
|
|
|
|
export function isApplicationError(error: unknown): error is ApplicationError {
|
2025-09-15 22:58:23 +00:00
|
|
|
return error instanceof ConvexError && "code" in error.data
|
2025-09-14 18:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-21 22:24:24 +00:00
|
|
|
export function create(code: Code, message?: string): ApplicationError {
|
2025-09-14 18:12:29 +00:00
|
|
|
return new ConvexError({
|
|
|
|
|
code,
|
2025-09-21 15:12:05 +00:00
|
|
|
message:
|
|
|
|
|
code === Code.Internal ? "Internal application error" : message,
|
2025-09-14 18:12:29 +00:00
|
|
|
})
|
|
|
|
|
}
|
2025-09-25 23:12:13 +00:00
|
|
|
|
|
|
|
|
export function createJson(code: Code, message?: string): ApplicationErrorData {
|
|
|
|
|
return {
|
|
|
|
|
code,
|
|
|
|
|
message:
|
|
|
|
|
code === Code.Internal ? "Internal application error" : message,
|
|
|
|
|
}
|
|
|
|
|
}
|