Refactor error handling to improve clarity and maintainability

This commit is contained in:
booooza 2024-06-17 08:34:53 +02:00
parent 2e5e103434
commit 39b0c62c1d
No known key found for this signature in database
GPG key ID: 9C897D14EA4759EC

View file

@ -41,41 +41,35 @@ class Threema extends NotificationProvider {
await axios.post(url, new URLSearchParams(data), config); await axios.post(url, new URLSearchParams(data), config);
return "Threema notification sent successfully."; return "Threema notification sent successfully.";
} catch (error) { } catch (error) {
this.handleApiError(error); const errorMessage = this.handleApiError(error);
this.throwGeneralAxiosError(error); this.throwGeneralAxiosError(errorMessage);
} }
} }
/** /**
* Handle Threema API errors * Handle Threema API errors
* @param {any} error The error to handle * @param {any} error The error to handle
* @returns {void} * @returns {string} Additional error context
*/ */
handleApiError(error) { handleApiError(error) {
if (error.response) { if (!error.response) {
const status = error.response.status; return error.message;
switch (status) {
case 400:
error.message = "Invalid recipient identity or account not set up for basic mode (400).";
break;
case 401:
error.message = "Incorrect API identity or secret (401).";
break;
case 402:
error.message = "No credits remaining (402).";
break;
case 404:
error.message = "Recipient not found (404).";
break;
case 413:
error.message = "Message is too long (413).";
break;
case 500:
error.message = "Temporary internal server error (500).";
break;
default:
break;
} }
switch (error.response.status) {
case 400:
return "Invalid recipient identity or account not set up for basic mode (400).";
case 401:
return "Incorrect API identity or secret (401).";
case 402:
return "No credits remaining (402).";
case 404:
return "Recipient not found (404).";
case 413:
return "Message is too long (413).";
case 500:
return "Temporary internal server error (500).";
default:
return error.message;
} }
} }
} }