fix: stop double-JSON-encoding scalar error fields in Box API errors#1559
Open
buptliuhs wants to merge 1 commit into
Open
fix: stop double-JSON-encoding scalar error fields in Box API errors#1559buptliuhs wants to merge 1 commit into
buptliuhs wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When building a
BoxApiErrorfor 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 stringvalues that adds a layer of quotes:
responseInfo.codecomes back as"\"storage_limit_exceeded\""instead ofstorage_limit_exceeded, and thecomposed
error.messageinherits the stray quotes.Reproduction
Box returns this (clean) response body for a storage-limit 403:
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.codecarries 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.tsextracts these fields withsdToJson(fetchResponse.data['code']).sdToJsonserializes to a JSONstring, so a string value gains surrounding quotes.
Fix
Read each scalar field with an
sdIsStringguard, which yields the rawstring and is safe on absent keys (unlike
getSdValueByKey, which throws ona 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
nodeFetchand drives a 403through
BoxNetworkClient.fetch: it fails against the current implementation(
responseInfo.code==="\"storage_limit_exceeded\"") and passes after thischange (
responseInfo.code===storage_limit_exceeded), witherror.messagelosing 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, anderror.messagechange — they lose the erroneousquotes. Anyone who worked around the previous quoting will need to adjust;
anyone doing the natural
code === 'storage_limit_exceeded'comparison startsworking correctly. Additionally, a present-but-non-string field now yields
undefinedrather than a JSON-encoded string (Box's error schema alwayssends strings for these). Suggest an appropriate version bump / changelog
note.
Note for maintainers
Same codegen question as the companion export PR (#1558) — if
boxNetworkClient.tsis generated, point me at the generator source and I'llmove it there.