fix: enhance error messages when Fetch API fails (#893)

This commit is contained in:
Gauthier
2024-07-27 01:43:16 +02:00
committed by GitHub
parent 3fc14c9e22
commit fccfca6ed0
16 changed files with 308 additions and 59 deletions

View File

@@ -100,7 +100,7 @@ class LunaSeaAgent
});
try {
await fetch(settings.options.webhookUrl, {
const response = await fetch(settings.options.webhookUrl, {
method: 'POST',
headers: settings.options.profileName
? {
@@ -114,15 +114,25 @@ class LunaSeaAgent
},
body: JSON.stringify(this.buildPayload(type, payload)),
});
if (!response.ok) {
throw new Error(response.statusText, { cause: response });
}
return true;
} catch (e) {
let errorData;
try {
errorData = await e.cause?.text();
errorData = JSON.parse(errorData);
} catch {
/* empty */
}
logger.error('Error sending LunaSea notification', {
label: 'Notifications',
type: Notification[type],
subject: payload.subject,
errorMessage: e.message,
response: e.response?.data,
response: errorData,
});
return false;