Upstream/server/errors.js
jvanvik b56b13880e Stage 0-2 server spine: db, scheduler, REST, auth, economy, map
Built: SQLite (node:sqlite) via server/db.js with checksummed migrations, injectable clock, persistent event
scheduler with catch-up, tiny REST router (error envelope, idempotency keys, rate limits, bearer tokens with scrypt
passwords), lazy-resource economy in public/js/rules.js + data.js, build/cancel/rename commands, seeded map +
spawn ring, generated docs/openapi.json. NETWALL net.js/static.js reused.
Tested: manual smoke run only (signup, town view, build, map). No unit tests or e2e yet.
NOT done: military, expansion, social, client, push, deploy docs, bots/admin.

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

43 lines
2.1 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.'],
};
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 };