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

50 lines
1.3 KiB
TypeScript

import {
DISPATCH_FAILURES,
REPOSITION_FAILURES,
type DispatchFailure,
type RepositionFailure,
} 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",
] as const;
export type ErrorCode = DispatchFailure | RepositionFailure | (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, ...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>;