starfleet-ui
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#6 · main · 2 min 01 s
0Étoiles
0Forks
0Issues
| 1 | /** |
| 2 | * How the UI reacts to Starfleet API errors. |
| 3 | * |
| 4 | * The API's own `message` is already player-readable ("The ship is docked: orbit first."), |
| 5 | * so we show it as-is. This module only adds: |
| 6 | * - a retry policy for the transient errors every endpoint may answer, and |
| 7 | * - the action that usually fixes a refusal, so the UI can offer it. |
| 8 | * Codes are from https://starfleet.glo.quebec/docs (API v1, the "captain" revision). |
| 9 | */ |
| 10 | |
| 11 | /** Longest we are willing to wait before the single automatic retry. */ |
| 12 | export const MAX_RETRY_WAIT_MS = 5_000; |
| 13 | |
| 14 | /** |
| 15 | * Milliseconds to wait before retrying once, or null if the error is not worth retrying. |
| 16 | * - `409 contended`: a concurrent change; the docs say to retry (we wait a beat). |
| 17 | * - `429 rate_limited` / `503 busy`: wait `Retry-After` seconds, if that is short enough. |
| 18 | */ |
| 19 | export function retryDelayMs(status: number, code: string, retryAfter: string | null): number | null { |
| 20 | if (status === 409 && code === 'contended') return 250; |
| 21 | if (status !== 429 && status !== 503) return null; |
| 22 | const seconds = retryAfter === null || retryAfter.trim() === '' ? NaN : Number(retryAfter); |
| 23 | const ms = Number.isFinite(seconds) && seconds >= 0 ? seconds * 1000 : 1000; |
| 24 | return ms <= MAX_RETRY_WAIT_MS ? ms : null; |
| 25 | } |
| 26 | |
| 27 | /** Error code → name of the ship action that resolves it. */ |
| 28 | const FIXES: Record<string, string> = { |
| 29 | ship_docked: 'orbit', |
| 30 | ship_in_orbit: 'dock', |
| 31 | no_market: 'dock', |
| 32 | not_enough_fuel: 'refuel', |
| 33 | tank_full: 'navigate', |
| 34 | trade_unsettled: 'settle', |
| 35 | ship_disabled: 'distress', |
| 36 | hold_full: 'sell' |
| 37 | }; |
| 38 | |
| 39 | export function fixFor(code: string): string | null { |
| 40 | return FIXES[code] ?? null; |
| 41 | } |
| 42 | |
| 43 | /** `$.waypoint` → `waypoint`; anything deeper or malformed → null. */ |
| 44 | export function fieldFromPath(path: string | undefined): string | null { |
| 45 | const m = path?.match(/^\$\.([A-Za-z_][A-Za-z0-9_]*)$/); |
| 46 | return m ? m[1] : null; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Actions the API refuses to rehearse with `?dry_run=1` (422 `no_dry_run`: |
| 51 | * "Extractions, trades and purchases cannot be rehearsed"). Anything else that |
| 52 | * answers `no_dry_run` at runtime is remembered by the caller. |
| 53 | */ |
| 54 | export const NO_DRY_RUN = new Set(['extract', 'buy', 'sell', 'buy_ship']); |
| 55 | |
| 56 | /** |
| 57 | * Message for an error response that carries no API error envelope: a proxy, firewall or |
| 58 | * CDN answered instead of the game server (an HTML 403 page, a bare 502…). |
| 59 | */ |
| 60 | export function unexplainedMessage(status: number, statusText: string): string { |
| 61 | const what = statusText ? `${status} ${statusText}` : String(status); |
| 62 | return `The game server answered ${what} without an explanation. Try again in a moment.`; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * HTTP status for a page whose load failed on an API error. The API's 4xx answers are |
| 67 | * meaningful to the player (401 key refused, 404 not found…) and pass through; anything |
| 68 | * else is the upstream failing, which is a 502 from our side, not a crash of ours. |
| 69 | */ |
| 70 | export function pageStatusFor(apiStatus: number): number { |
| 71 | return apiStatus >= 400 && apiStatus < 500 ? apiStatus : 502; |
| 72 | } |