deadhead-space-freight-tycoon/packages/schema/src/errors.ts

65 lines
1.6 KiB
TypeScript

import {
DISPATCH_FAILURES,
REPOSITION_FAILURES,
UPGRADE_FAILURES,
type DispatchFailure,
type RepositionFailure,
type UpgradeFailure,
} from "@deadhead/sim";
import { z } from "zod";
/** Error codes the server adds on top of the sim's validation reasons. */
export const SERVER_ERROR_CODES = [
"validation-error",
"unauthorized",
"not-found",
"internal-error",
"insufficient-funds",
"not-in-tutorial",
"tutorial-run-in-flight",
"unknown-contract",
"contract-already-accepted",
"location-undiscovered",
"bay-already-leased",
"no-bay-at-location",
"ship-not-idle-here",
"ship-crewed",
"pilot-already-assigned",
"pilot-not-assigned-to-ship",
"no-skill-points",
"skill-maxed",
"unknown-skill",
"avatar-in-tutorial",
"tutorial-active",
"deal-not-available",
"unknown-goal",
"goal-not-ready",
"goal-already-claimed",
"restore-failed",
"rate-limited",
] as const;
export type ErrorCode =
DispatchFailure | RepositionFailure | UpgradeFailure | (typeof SERVER_ERROR_CODES)[number];
/** Every machine-readable error code. Sim validation reasons are used unchanged. */
export const ERROR_CODES = [
...new Set<ErrorCode>([
...DISPATCH_FAILURES,
...REPOSITION_FAILURES,
...UPGRADE_FAILURES,
...SERVER_ERROR_CODES,
]),
] as [ErrorCode, ...ErrorCode[]];
export const errorCodeSchema = z.enum(ERROR_CODES);
/** Body of every non-2xx response. */
export const errorResponseSchema = z.object({
error: z.object({
code: errorCodeSchema,
message: z.string().min(1),
}),
});
export type ErrorResponse = z.infer<typeof errorResponseSchema>;