Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/rest/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CoinbaseError extends Error {

export function handleException(
response: Response,
responseText: string,
responseJson: object,
reason: string
) {
let message: string | undefined;
Expand All @@ -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}`;
Expand Down
6 changes: 3 additions & 3 deletions src/rest/rest-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down