Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Don't assume anything is JSON anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Morley Zhi committed Jul 2, 2020
1 parent 9496b82 commit 3b15056
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stellar/wallet-sdk",
"version": "0.1.0-rc.3",
"version": "0.1.0-rc.4",
"description": "Libraries to help you write Stellar-enabled wallets in Javascript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
25 changes: 17 additions & 8 deletions src/KeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ export class KeyManager {
});

if (responseRes.status !== 200) {
const responseText = await responseRes.text();
try {
const responseJson = await responseRes.json();
const responseJson = JSON.parse(responseText);
throw new Error(
`[KeyManager#fetchAuthToken] Failed to return a signed transaction,
error: ${responseJson.error}`,
Expand All @@ -391,19 +392,27 @@ export class KeyManager {
throw new Error(
`[KeyManager#fetchAuthToken] Failed to return a signed transaction,
error code ${responseRes.status} and status text
"${responseRes.statusText}"`,
"${responseText}"`,
);
}
}

const { token, message, status } = await responseRes.json();
const responseResText = await responseRes.text();

// if we get a false status message, error out
if (status === false && message) {
throw new Error(message);
}
try {
const { token, message, status } = JSON.parse(responseResText);
// if we get a false status message, error out
if (status === false && message) {
throw new Error(message);
}

return token;
return token;
} catch (e) {
throw new Error(
`[KeyManager#fetchAuthToken] Failed to validate signed transaction
response, server responded with ${responseResText}`,
);
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/transfers/DepositProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,16 @@ export class DepositProvider extends TransferProvider {
}

if (!response.ok) {
const responseText = await response.text();
try {
const { error } = await response.json();
const { error } = JSON.parse(responseText);
throw new Error(
`Error starting deposit to ${this.transferServer}: error ${error}`,
);
} catch (e) {
throw new Error(
`Error starting deposit to ${this.transferServer}: error
code ${response.status}, status text: "${response.statusText}"`,
code ${response.status}, status text: "${responseText}"`,
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/transfers/WithdrawProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,16 @@ export class WithdrawProvider extends TransferProvider {
}

if (!response.ok) {
const responseText = await response.text();
try {
const { error } = await response.json();
const { error } = JSON.parse(responseText);
throw new Error(
`Error starting withdrawal to ${this.transferServer}: error ${error}`,
);
} catch (e) {
throw new Error(
`Error starting withdrawal to ${this.transferServer}: error
code ${response.status}, status text: "${response.statusText}"`,
code ${response.status}, status text: "${responseText}"`,
);
}
}
Expand Down

0 comments on commit 3b15056

Please sign in to comment.