{"record":{"id":"65af5dd0d62f1079","repo":"hcengineering/platform","slug":"errorbody-error","errorCode":null,"errorMessage":"${errorBody?.error}","messagePattern":"\\$\\{errorBody\\?\\.error\\}","errorType":"exception","errorClass":"PlatformError","httpStatus":null,"severity":"error","filePath":"packages/kvs-client/src/client.ts","lineNumber":148,"sourceCode":"      ...this.requestInit,\n      method: options.method,\n      headers,\n      body: options.body\n    })\n\n    if (options.returnNullOn404 === true && response.status === 404) {\n      return null\n    }\n\n    if (options.acceptNotFound === true && response.status === 404) {\n      return null\n    }\n\n    if (!response.ok) {\n      try {\n        const errorBody = await response.json()\n        if (errorBody?.error != null) {\n          throw new PlatformError(errorBody?.error)\n        }\n      } catch (e) {\n        // Ignore JSON parsing errors\n      }\n      throw new Error(options.errorMessage ?? 'Request failed')\n    }\n\n    // Parse JSON response when needed\n    const contentType = response.headers.get('content-type')\n    if (\n      response.status !== 204 &&\n      (options.method === 'GET' || (contentType != null && contentType.includes('application/json')))\n    ) {\n      return await response.json()\n    }\n\n    return null\n  }","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/packages/kvs-client/src/client.ts#L130-L166","documentation":"sendRequest wraps all HTTP calls made by setValue/getValue/deleteKey/listKeys. When the server responds with a non-OK status and its JSON body contains an `error` field, the client re-throws that server-provided message wrapped in a PlatformError. This propagates the actual backend reason (auth failure, not found, quota, etc.) to the caller.","triggerScenarios":"Any of setValue, getValue, deleteKey, or listKeys receiving an HTTP error response whose body parses as JSON with a non-null `error` property — e.g. 401 with `{\"error\":\"unauthorized\"}` or 404 with `{\"error\":\"key not found\"}`.","commonSituations":"Expired or invalid auth token, wrong namespace or key name, service-side rate limiting, or the server rejecting a malformed value.","solutions":["Read the PlatformError message — it contains the server's own error description and usually states the exact cause.","Check that the token passed to the KvsClient constructor is valid and not expired.","Verify the namespace/key identifier used in the failing call exists on the server.","Wrap calls in try/catch and handle PlatformError distinctly from generic failures to react to server-reported conditions."],"exampleFix":"// before\nawait client.getValue('user:42')\n// after\ntry {\n  await client.getValue('user:42')\n} catch (e) {\n  if (e instanceof PlatformError) console.error('Server said:', e.message)\n  else throw e\n}","handlingStrategy":"try-catch","validationCode":"if (!token) throw new Error('Refusing KVS call: token missing') // avoid predictable 401s before calling the API","typeGuard":"function isPlatformError(e: unknown): e is PlatformError {\n  return e instanceof PlatformError\n}","tryCatchPattern":"try {\n  await client.getValue(key)\n} catch (e) {\n  if (isPlatformError(e)) {\n    // e.message is the server-provided reason; branch on it\n  } else {\n    throw e\n  }\n}","preventionTips":["Refresh tokens before they expire and propagate auth failures to a re-auth flow.","Log PlatformError messages so server-side causes are visible in your logs.","Validate key/namespace inputs client-side to avoid predictable 404s."],"tags":["http","server-error","platform-error","kvs"],"backgroundTag":"server-error-response","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}