Skip to content

Commit 61ee240

Browse files
fix: isSyntaxError() function (#5466)
Problem: Due to a recent change the isSyntaxError() function was not returning when expected. We were still seeing it appear in `auth_refreshCredentials` even though it should have been filtered out. Solution: Fix the function so it reports correctly identifies syntax error when it happens. Signed-off-by: Nikolas Komonen <[email protected]>
1 parent 2f5ff3a commit 61ee240

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

packages/core/src/shared/errors.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ export function isNetworkError(err?: unknown): err is Error & { code: string } {
788788
if (
789789
isVSCodeProxyError(err) ||
790790
isSocketTimeoutError(err) ||
791-
isNonJsonHttpResponse(err) ||
791+
isHttpSyntaxError(err) ||
792792
isEnoentError(err) ||
793793
isEaccesError(err) ||
794794
isEbadfError(err) ||
@@ -849,11 +849,13 @@ function isSocketTimeoutError(err: Error): boolean {
849849
/**
850850
* Expected JSON response from HTTP request, but got an error HTML error page instead.
851851
*
852-
* Example error message:
853-
* "Unexpected token '<', "<html><bod"... is not valid JSON Deserialization error: to see the raw response, inspect the hidden field {error}.$response on this object."
852+
* IMPORTANT:
853+
*
854+
* This function is influenced by {@link getReasonFromSyntaxError()} since it modifies the error
855+
* message with the real underlying reason, instead of the default "Unexpected token" message.
854856
*/
855-
function isNonJsonHttpResponse(err: Error): boolean {
856-
return isError(err, 'SyntaxError', 'Unexpected token')
857+
function isHttpSyntaxError(err: Error): boolean {
858+
return isError(err, 'SyntaxError', 'SDK Client unexpected error response')
857859
}
858860

859861
/**
@@ -890,6 +892,9 @@ function isError(err: Error, id: string, messageIncludes: string = '') {
890892
* While the contents of the response may contain sensitive information, there may be a reason
891893
* for failure embedded. This function attempts to extract that reason.
892894
*
895+
* Example error message before extracting:
896+
* "Unexpected token '<', "<html><bod"... is not valid JSON Deserialization error: to see the raw response, inspect the hidden field {error}.$response on this object."
897+
*
893898
* If the reason cannot be found or the error is not a SyntaxError, return undefined.
894899
*/
895900
export function getReasonFromSyntaxError(err: Error): string | undefined {

packages/core/src/test/shared/errors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ describe('util', function () {
492492
false,
493493
'Incorrectly indicated as network error'
494494
)
495-
let err = new Error("Unexpected token '<'")
495+
let err = new Error('SDK Client unexpected error response: Blah Blah Blah')
496496
err.name = 'SyntaxError'
497497
assert.deepStrictEqual(isNetworkError(err), true, 'Did not indicate SyntaxError as network error')
498498

0 commit comments

Comments
 (0)