JuliusBrussee/caveman · error · Error
device authorization failed
Error message
device authorization failed: ${JSON.stringify(rawCode)} What it means
Thrown by runCavemanDeviceFlow when the device-code endpoint responds 2xx but its JSON body fails DeviceCode validation: missing/empty device_code, user_code, verification_uri, or a non-positive/non-finite expires_in (a JSON parse failure yields null and also triggers this). The library refuses to proceed with malformed authorization data.
Solutions
- Log/capture the JSON.stringify(rawCode) payload from the message to see exactly which field is missing or malformed.
- Verify server and client versions are compatible (the server must return device_code, user_code, verification_uri, expires_in).
- Check that no proxy or middleware is rewriting the response body (HTML challenge pages, WAF interstitials).
- Retry; if transient (e.g. an edge node served a bad body), a fresh request often succeeds.
Example fix
// server returning wrong shape
{ "device_code": "abc" } // missing user_code, verification_uri, expires_in
// after (correct DeviceCode shape)
{ "device_code": "abc", "user_code": "ABCD-EFGH", "verification_uri": "https://example.com/activate", "expires_in": 900 } Defensive patterns
Strategy: validation
Validate before calling
function isValidDeviceCode(v: unknown): v is { device_code: string; user_code: string; verification_uri: string; expires_in: number } {
const d = v as any;
return !!d && typeof d.device_code === 'string' && d.device_code !== '' &&
typeof d.user_code === 'string' && typeof d.verification_uri === 'string' &&
typeof d.expires_in === 'number' && Number.isFinite(d.expires_in) && d.expires_in > 0;
} Type guard
const isDeviceCode = (v: unknown): v is DeviceCode => typeof v === 'object' && v !== null && typeof (v as DeviceCode).device_code === 'string' && (v as DeviceCode).device_code !== '' && typeof (v as DeviceCode).user_code === 'string' && typeof (v as DeviceCode).verification_uri === 'string' && typeof (v as DeviceCode).expires_in === 'number' && (v as DeviceCode).expires_in > 0;
Try / catch
try {
await runCavemanDeviceFlow(opts);
} catch (e) {
if (e instanceof Error && e.message.startsWith('device authorization failed: {')) {
console.error('Malformed device-code response:', e.message);
} else throw e;
} Prevention
- Keep server and client package versions aligned
- Test the flow against staging to catch schema drift
- Check for proxies/WAFs that rewrite JSON bodies
- Log raw responses on failure for support tickets
When it happens
Trigger: codeResponse.ok is true but `await codeResponse.json()` returns null (unparseable body) or a Partial<DeviceCode> failing any of: device_code/user_code/verification_uri must be non-empty strings, expires_in must be a finite number > 0.
Common situations: Server behind a gateway returning an HTML error page with 200, an API version mismatch returning a different schema, a mock/stub server returning an empty body, or an incompatible server release.
Understand the failure class
Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.
Related errors
- assembly slot content is not JSON-serializable
- cave_output_schema_mismatch
- duplicate object key
- eval evidence line must be a JSON object
- generic decode: expected a JSON array or
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/c7eeee2ab502182b.
Report an issue: GitHub.
Appendix: source
Thrown at packages/device-auth/src/index.ts:107
signal?: AbortSignal;
sleep?: (ms: number) => Promise<void>;
onCode?: (code: DeviceCode) => void | Promise<void>;
}): Promise<DeviceGrant> {
const fetcher = options.fetch ?? globalThis.fetch;
const wait = options.sleep ?? defaultSleep;
const baseURL = options.baseURL.replace(/\/$/, "");
const codeResponse = await fetcher(`${baseURL}/api/v1/auth/device/code`, {
method: "POST",
headers: { "content-type": "application/json", "x-cave-client": options.client },
body: "{}",
signal: requestSignal(options.signal, 5000),
});
if (!codeResponse.ok) throw new Error(`device authorization failed: HTTP ${codeResponse.status}`);
const rawCode = await codeResponse.json().catch(() => null) as Partial<DeviceCode> | null;
if (rawCode === null || typeof rawCode.device_code !== "string" || rawCode.device_code === "" ||
typeof rawCode.user_code !== "string" || typeof rawCode.verification_uri !== "string" ||
typeof rawCode.expires_in !== "number" || !Number.isFinite(rawCode.expires_in) || rawCode.expires_in <= 0) {
throw new Error(`device authorization failed: ${JSON.stringify(rawCode)}`);
}
const code = rawCode as DeviceCode;
await options.onCode?.(structuredClone(code));
let intervalMs = Math.max(0, Number(code.interval ?? 5)) * 1000;
const deadline = Date.now() + code.expires_in * 1000;
while (Date.now() < deadline) {
let payload: Record<string, unknown>;
let status = 0;
let retryAfterMs = 0;
try {
const response = await fetcher(`${baseURL}/api/v1/auth/device/token`, {
method: "POST",
headers: { "content-type": "application/json", "x-cave-client": options.client },
body: JSON.stringify({ device_code: code.device_code }),
signal: requestSignal(options.signal, 5000),
});
status = response.status;
const retryAfter = response.headers.get("retry-after");View on GitHub (pinned to 3ee70a1026)