From acf31985d8a6d2e75981d95af24a12e005dfe7ce Mon Sep 17 00:00:00 2001 From: Travis Swiers Date: Wed, 1 Jan 2025 10:16:39 -0700 Subject: [PATCH] feat: response json --- src/rest/errors.ts | 17 ++++++++++++----- src/rest/rest-base.ts | 6 +++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/rest/errors.ts b/src/rest/errors.ts index 5d9695e..c6363b7 100644 --- a/src/rest/errors.ts +++ b/src/rest/errors.ts @@ -14,7 +14,7 @@ class CoinbaseError extends Error { export function handleException( response: Response, - responseText: string, + responseJson: object, reason: string ) { let message: string | undefined; @@ -23,10 +23,17 @@ export function handleException( (400 <= response.status && response.status <= 499) || (500 <= response.status && response.status <= 599) ) { - if ( - response.status == 403 && - responseText.includes('"error_details":"Missing required scopes"') - ) { + let missingRequiredScopes = false; + const responseText = JSON.stringify(responseJson, (key, value) => { + if ( + key === 'error_details' && + typeof value === 'string' && + value.includes('Missing required scopes') + ) { + missingRequiredScopes = true; + } + }); + if (response.status == 403 && missingRequiredScopes) { message = `${response.status} Coinbase Error: Missing Required Scopes. Please verify your API keys include the necessary permissions.`; } else message = `${response.status} Coinbase Error: ${reason} ${responseText}`; diff --git a/src/rest/rest-base.ts b/src/rest/rest-base.ts index 9084df5..15ce312 100644 --- a/src/rest/rest-base.ts +++ b/src/rest/rest-base.ts @@ -61,10 +61,10 @@ export class RESTBase { url: string ) { const response: Response = await fetch(url, requestOptions); - const responseText = await response.text(); - handleException(response, responseText, response.statusText); + const responseJson = await response.json(); + handleException(response, responseJson, response.statusText); - return responseText; + return responseJson; } setHeaders(httpMethod: string, urlPath: string, isPublic?: boolean) {