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) {