Provide more context. Handle more edge cases.

This commit is contained in:
Eric Peterson
2020-10-15 12:37:50 +02:00
parent a6c0d9803e
commit 1af3dbdb5e
+12 -2
View File
@@ -84,10 +84,20 @@ export class NewRelicClient implements NewRelicApi {
async getApplications(): Promise<NewRelicApplications> {
const url = await this.getApiUrl('apm', 'applications.json');
const response = await fetch(url);
const responseJson = await response.json();
let responseJson;
try {
responseJson = await response.json();
} catch (e) {
responseJson = { applications: [] };
}
if (response.status !== 200) {
throw new Error(responseJson?.error?.title || response.statusText);
throw new Error(
`Error communicating with New Relic: ${
responseJson?.error?.title || response.statusText
}`,
);
}
return responseJson;