'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 };