Skip to content

fix: stop double-JSON-encoding scalar error fields in Box API errors#1559

Open
buptliuhs wants to merge 1 commit into
box:mainfrom
buptliuhs:fix/clean-error-field-encoding
Open

fix: stop double-JSON-encoding scalar error fields in Box API errors#1559
buptliuhs wants to merge 1 commit into
box:mainfrom
buptliuhs:fix/clean-error-field-encoding

Conversation

@buptliuhs

Copy link
Copy Markdown

Summary

When building a BoxApiError for a 4xx/5xx response, the scalar error fields
(code, request_id, help_url, message, error, error_description)
are passed through sdToJson, which JSON-encodes its input. For string
values that adds a layer of quotes: responseInfo.code comes back as
"\"storage_limit_exceeded\"" instead of storage_limit_exceeded, and the
composed error.message inherits the stray quotes.

Reproduction

Box returns this (clean) response body for a storage-limit 403:

{
  "type": "error",
  "status": 403,
  "code": "storage_limit_exceeded",
  "help_url": "http://developers.box.com/docs/#errors",
  "message": "Account storage limit reached",
  "request_id": "<request-id>"
}

The resulting BoxApiError (relevant fields only):

Before

{
  "message": "403 \"storage_limit_exceeded\" \"Account storage limit reached\"; Request ID: \"<request-id>\"",
  "responseInfo": {
    "statusCode": 403,
    "body": {
      "type": "error",
      "status": 403,
      "code": "storage_limit_exceeded",
      "help_url": "http://developers.box.com/docs/#errors",
      "message": "Account storage limit reached",
      "request_id": "<request-id>"
    },
    "code": "\"storage_limit_exceeded\"",
    "requestId": "\"<request-id>\"",
    "helpUrl": "\"http://developers.box.com/docs/#errors\""
  }
}

After

{
  "message": "403 storage_limit_exceeded Account storage limit reached; Request ID: <request-id>",
  "responseInfo": {
    "statusCode": 403,
    "body": {
      "type": "error",
      "status": 403,
      "code": "storage_limit_exceeded",
      "help_url": "http://developers.box.com/docs/#errors",
      "message": "Account storage limit reached",
      "request_id": "<request-id>"
    },
    "code": "storage_limit_exceeded",
    "requestId": "<request-id>",
    "helpUrl": "http://developers.box.com/docs/#errors"
  }
}

Note how responseInfo.code carries embedded quotes ("\"storage_limit_exceeded\"")
while responseInfo.body.code — the raw parsed payload — is already correct.
The fix makes the two agree.

Root cause

src/networking/boxNetworkClient.ts extracts these fields with
sdToJson(fetchResponse.data['code']). sdToJson serializes to a JSON
string, so a string value gains surrounding quotes.

Fix

Read each scalar field with an sdIsString guard, which yields the raw
string and is safe on absent keys (unlike getSdValueByKey, which throws on
a missing key). context_info (a map) is untouched.

Tests

The repository's tests are the generated integration suites under src/test/,
so I've left those untouched rather than add an out-of-pattern unit test under
src/networking/.

Verified locally with a throwaway test that mocks nodeFetch and drives a 403
through BoxNetworkClient.fetch: it fails against the current implementation
(responseInfo.code === "\"storage_limit_exceeded\"") and passes after this
change (responseInfo.code === storage_limit_exceeded), with error.message
losing its stray quotes. Happy to contribute that as a proper test if you can
point me at where error-path coverage belongs in the generated layout.

Breaking change

No type signatures change, so consumer code continues to compile. But the
runtime values of responseInfo.code, responseInfo.requestId,
responseInfo.helpUrl, and error.message change — they lose the erroneous
quotes. Anyone who worked around the previous quoting will need to adjust;
anyone doing the natural code === 'storage_limit_exceeded' comparison starts
working correctly. Additionally, a present-but-non-string field now yields
undefined rather than a JSON-encoded string (Box's error schema always
sends strings for these). Suggest an appropriate version bump / changelog
note.

Note for maintainers

Same codegen question as the companion export PR (#1558) — if
boxNetworkClient.ts is generated, point me at the generator source and I'll
move it there.

The 4xx error path ran code, request_id, help_url, message, error and
error_description through `sdToJson`, which JSON-encodes its input and
therefore wrapped strings in quotes. As a result `responseInfo.code` came
back as `"\"storage_limit_exceeded\""` instead of `storage_limit_exceeded`,
and the composed `error.message` inherited the stray quotes
(`403 "storage_limit_exceeded" "Account storage limit reached"`).

Read these scalar fields with an `sdIsString` guard instead, which yields the
raw string and is safe on absent keys (unlike getSdValueByKey, which would
throw). `context_info` is unchanged.

Behavior change: `responseInfo.code`, `responseInfo.requestId`,
`responseInfo.helpUrl` and `error.message` now contain the raw, unquoted
values. Consumers that worked around the previous quoting must adjust;
consumers doing the natural comparison (`code === 'storage_limit_exceeded'`)
now behave correctly. No type signatures change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@buptliuhs
buptliuhs requested a review from a team July 22, 2026 09:22
@CLAassistant

CLAassistant commented Jul 22, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@buptliuhs buptliuhs changed the title fix: stop double-JSON-encoding scalar error fields in BoxApiError fix: stop double-JSON-encoding scalar error fields in Box API errors Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants