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

Commit

Permalink
Parse error responses as text first
Browse files Browse the repository at this point in the history
  • Loading branch information
Morley Zhi committed Apr 15, 2020
1 parent d6b5809 commit 9fe355d
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/transfers/TransferProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ export abstract class TransferProvider {
);

if (!response.ok) {
const badResponse = await response.text();
try {
const { error } = await response.json();
const { error, message } = JSON.parse(badResponse);
throw new Error(
`Error fetching info from ${this.transferServer}: error ${error}`,
`Error fetching info from ${this.transferServer}: error ${error ||
message}`,
);
} catch (e) {
throw new Error(
`Error fetching info from ${this.transferServer}: error
code ${response.status}, status text: "${response.statusText}"`,
code ${response.status}, response text: "${badResponse}"`,
);
}
}
Expand Down Expand Up @@ -206,17 +208,18 @@ export abstract class TransferProvider {
);

if (!response.ok) {
const badResponse = await response.text();
try {
const { error } = await response.json();
const { error, message } = JSON.parse(badResponse);
throw new Error(
`Error fetching transactions from ${
this.transferServer
}: error ${error}`,
}: error ${error || message}`,
);
} catch (e) {
throw new Error(
`Error fetching transactions from ${this.transferServer}: error
code ${response.status}, status text: "${response.statusText}"`,
code ${response.status}, response text: "${badResponse}"`,
);
}
}
Expand Down Expand Up @@ -282,17 +285,19 @@ export abstract class TransferProvider {
);

if (!response.ok) {
const badResponse = await response.text();

try {
const { error } = await response.json();
const { error, message } = JSON.parse(badResponse);
throw new Error(
`Error fetching transaction ${qs} from ${
this.transferServer
}: error ${error}`,
}: error ${error || message}`,
);
} catch (e) {
throw new Error(
`Error fetching transaction ${qs} from ${this.transferServer}: error
code ${response.status}, status text: "${response.statusText}"`,
code ${response.status}, response text: "${badResponse}"`,
);
}
}
Expand Down Expand Up @@ -637,15 +642,18 @@ export abstract class TransferProvider {
);

if (!response.ok) {
const badResponse = await response.text();

try {
const { error } = await response.json();
const { error, message } = JSON.parse(badResponse);
throw new Error(
`Error fetching fees from ${this.transferServer}: error ${error}`,
`Error fetching fees from ${this.transferServer}: error ${error ||
message}`,
);
} catch (e) {
throw new Error(
`Error fetching fees from ${this.transferServer}: error
code ${response.status}, status text: "${response.statusText}"`,
code ${response.status}, response text: "${badResponse}"`,
);
}
}
Expand Down

0 comments on commit 9fe355d

Please sign in to comment.