Upstream/server/errors.js
jvanvik 3c23c51902 Phase 4: close loot/refund leaks, landing-site garrisons, speed-scaled shipping, update/offsite tooling, broad e2e
Built:
- Economy: raid loot capped to the room the raiders have at home (rest stays with the victim, "lootLeft" in the report); trade escrow reserves its room so refunds always fit; trade accept refuses with no_room_for_goods when the goods would not fit. Loot destroyed per 30-day run 31-61k -> ~1k, refunds clipped ~27k -> 0 (docs/BALANCE.md, 3 seeds).
- End game: members of the holding alliance garrison landing sites (migration 005_site_garrison.sql), garrison counts in site defence, 72 h capture shield, withdraw route, POP view lists garrisoned units. Shipping capacity scales with world speed (3x world: winner 76-98 -> ~30-36 days after sites open). Bot runs show real contests (84-194 attacks on held sites at 1x).
- Ops: deploy/update.sh (backup, stop, ff-merge, npm ci --ignore-scripts, health check, rollback), deploy/upstream-offsite (rsync only), admin end-world snapshots results, EPIPE fix, docs/workflows/ filled in.
- e2e 62 -> 163 checks: units, raids/reports, alliance+messages, trade, end game, nb, PWA, reconnect, slow polling fallback (opt-in). Two app bugs found: message push ignored, "New" button stayed English.

Tested: npm test 274/274, openapi check, npm run e2e 163/163 (137 s), bot runs 50x30d seeds 1-3 and 140-day/60-day speed-3 end-game runs INVARIANTS OK, adversarial review pass (findings fixed or documented).
NOT tested: update.sh on a real box (systemd, real npm, runuser; stubs only, shellcheck not installed), garrison card/loot text on a phone, Node 22.13, humans. Known leaks left open and documented (BALANCE S6): loot/shipments can still overflow when stock arrives in flight, bounced backbone shipments lose overflow (reported to the sender). Garrison numbers (heldDefenceMult 2, shield 72 h) are first guesses from 2 seeds. Not claimed: balanced.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-12 00:58:12 +02:00

79 lines
4.8 KiB
JavaScript

'use strict';
/*
* Stable API error codes. Clients switch on `code`, never on `message`. Every code the API can return is listed
* here with its HTTP status; docs/API.md and openapi.json are generated from this table.
*/
const CODES = {
bad_request: [400, 'The request could not be parsed.'],
validation_failed: [422, 'A field is missing or has the wrong shape.'],
unauthorized: [401, 'Missing, invalid or revoked token.'],
invalid_credentials: [401, 'Wrong username or password.'],
forbidden: [403, 'You may not do that.'],
not_found: [404, 'No such thing.'],
method_not_allowed: [405, 'Wrong HTTP method for this path.'],
conflict: [409, 'The request conflicts with the current state.'],
request_in_progress: [409, 'A request with this Idempotency-Key is still running.'],
idempotency_key_reuse: [422, 'This Idempotency-Key was used for a different request.'],
payload_too_large: [413, 'Request body too large.'],
rate_limited: [429, 'Too many requests, slow down.'],
internal_error: [500, 'Something broke on our side.'],
signup_closed: [403, 'Signup needs a valid invite code.'],
username_taken: [409, 'That username is taken.'],
world_full: [409, 'There is no room left on the map.'],
// game rules
unknown_building: [404, 'No such building or slot.'],
not_buildable: [409, 'That building cannot be built here.'],
max_level: [409, 'Already at the maximum level.'],
requirements_not_met: [409, 'A required building level is missing.'],
already_queued: [409, 'That building is already being built.'],
queue_full: [409, 'All build slots are busy.'],
not_enough_resources: [409, 'Not enough resources.'],
// social: alliances, diplomacy, messages
peering_office_required: [409, 'A Peering office (level 1 or higher) is needed to found or join an alliance.'],
already_in_alliance: [409, 'That operator is already in an alliance.'],
not_in_alliance: [409, 'You are not in that alliance.'],
alliance_name_taken: [409, 'That alliance name is taken.'],
alliance_tag_taken: [409, 'That alliance tag is taken.'],
alliance_full: [409, 'The alliance has no room for more members.'],
invite_expired: [409, 'The invite has expired.'],
leader_must_transfer: [409, 'The leader must hand over leadership before leaving.'],
notes_full: [409, 'The alliance has reached its pinned-note limit.'],
diplomacy_invalid: [409, 'That diplomatic move is not allowed from the current state.'],
diplomacy_pending: [409, 'A diplomatic change between these alliances is already scheduled.'],
proposal_exists: [409, 'The other alliance already proposed this; accept their proposal instead.'],
proposal_expired: [409, 'The proposal has expired.'],
// military and expansion
unknown_unit: [404, 'No such unit type.'],
not_researchable: [409, 'That unit stat cannot be researched.'],
not_enough_units: [409, 'Not enough units at home.'],
units_offline: [409, 'Those units are offline (the battery is empty).'],
invalid_target: [409, 'That target is not valid for this kind of movement.'],
target_protected: [409, 'The target is under beginner protection.'],
protection_active: [409, 'Attacking ends your beginner protection; confirm to go ahead.'],
out_of_range: [409, 'The target is too far from your POPs.'],
footprint_too_low: [409, 'Your footprint is too low for another POP.'],
cannot_takeover: [409, 'That POP cannot be taken over.'],
recall_expired: [409, 'The recall window (first 90 seconds) has passed.'],
capacity_exceeded: [409, 'More than one transport link can carry.'],
no_transport_link: [409, 'All transport links are busy (or there is no Internet exchange).'],
// trade and end game
trade_ratio: [409, 'The offered and the wanted amount may differ by at most 1:2.'],
offer_closed: [409, 'That offer is not open any more.'],
own_offer: [409, 'You cannot accept your own offer.'],
trade_out_of_range: [409, 'That POP is too far from the offer (its max distance or your Internet exchange reach).'],
no_room_for_goods: [409, 'Your Rack hall will not have room for the goods when they arrive; make room (spend, or raise the hall) and accept again.'],
world_ended: [409, 'This world has ended: it can still be read, but nothing can be changed.'],
backbone_closed: [409, 'The backbone landing sites have not appeared yet.'],
};
class GameError extends Error {
constructor(code, details, message) {
const def = CODES[code];
super(message || (def ? def[1] : code));
this.code = code;
this.status = def ? def[0] : 400;
this.details = details || {};
}
}
module.exports = { CODES, GameError };