{"record":{"id":"437c05539650bed6","repo":"different-ai/openwork","slug":"request-failed","errorCode":"request_failed","errorMessage":"Request failed with ${raw.status}.","messagePattern":"Request failed with (.+?)\\.","errorType":"http","errorClass":"DenApiError","httpStatus":null,"severity":"error","filePath":"apps/app/src/app/lib/den.ts","lineNumber":2886,"sourceCode":"  try {\n    json = text ? (JSON.parse(text) as T) : null;\n  } catch {\n    json = null;\n  }\n  return { ok: response.ok, status: response.status, json };\n}\n\nasync function requestJson<T>(\n  input: string | DenBaseUrls,\n  path: string,\n  options: DenRequestOptions = {},\n): Promise<T> {\n  const raw = await requestJsonRaw<T>(input, path, options);\n  if (!raw.ok) {\n    const payload = raw.json;\n    const code = isRecord(payload) && typeof payload.error === \"string\" ? payload.error : \"request_failed\";\n    const message = getErrorMessage(payload, `Request failed with ${raw.status}.`);\n    throw new DenApiError(raw.status, code, message, isRecord(payload) ? payload.details : undefined);\n  }\n  return raw.json as T;\n}\n\nasync function ensureActiveOrganization(\n  baseUrls: DenBaseUrls,\n  token: string | null,\n  input: { organizationId?: string | null; organizationSlug?: string | null },\n) {\n  const organizationId = input.organizationId?.trim() ?? \"\";\n  const organizationSlug = input.organizationSlug?.trim() ?? \"\";\n  if (!token || (!organizationId && !organizationSlug)) {\n    return;\n  }\n\n  await requestJson<unknown>(baseUrls, \"/v1/me/active-organization\", {\n    method: \"POST\",\n    token,","sourceCodeStart":2868,"sourceCodeEnd":2904,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/apps/app/src/app/lib/den.ts#L2868-L2904","documentation":"requestJson is the generic Den API response handler: when the HTTP response is not ok, it throws DenApiError with the status, an error code taken from the JSON body's `error` field (defaulting to \"request_failed\"), and a message \"Request failed with <status>.\" unless the body supplies a message. This is the catch-all error for any non-2xx Den API call.","triggerScenarios":"Any Den API request via requestJson that returns a non-ok status (401 unauthorized, 403 forbidden, 404 not found, 500 server error, etc.) without a more specific handler.","commonSituations":"Expired/missing auth token (401), wrong baseUrl or apiBaseUrl pointing at a wrong server, missing organization header, or the Den server being down (5xx).","solutions":["Read err.status and err.code on the DenApiError to identify the actual failure","For 401, re-authenticate (refresh the handoff/session token) before retrying","Verify baseUrl/apiBaseUrl configuration points at the correct Den deployment","Retry with backoff for transient 5xx statuses"],"exampleFix":"// before\nconst me = await client.getMe();\n// after\ntry {\n  const me = await client.getMe();\n} catch (err) {\n  if (err instanceof DenApiError && err.status === 401) await reauthenticate();\n  else throw err;\n}","handlingStrategy":"try-catch","validationCode":"if (!baseUrl || !/^https?:\\/\\//.test(baseUrl)) throw new Error(\"A valid Den baseUrl is required\");\nif (!authToken) throw new Error(\"Sign in before calling the Den API\");","typeGuard":"const isDenApiError = (e: unknown): e is DenApiError =>\n  e instanceof DenApiError && typeof e.status === \"number\" && typeof e.code === \"string\";","tryCatchPattern":"try {\n  data = await callDenApi();\n} catch (err) {\n  if (isDenApiError(err)) {\n    if (err.status === 401) await reauthenticate();\n    else if (err.status >= 500) scheduleRetry();\n    else showError(err.message, err.code);\n  } else throw err;\n}","preventionTips":["Branch on err.status and err.code of DenApiError, not the message string","Re-authenticate on 401 before retrying","Retry 5xx with exponential backoff; never retry 4xx blindly","Validate baseUrl/apiBaseUrl configuration at startup"],"tags":["http","api","den-cloud","request-failed"],"backgroundTag":"http-request-failed","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}