Skip to content

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinvdpol committed Sep 26, 2024
1 parent 10ccde0 commit ef3f904
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,25 +205,32 @@ class Client extends OAuth2Client {
headers,
}));

const error = 'ErrorMessages' in body && filled(body.ErrorMessages[0]) ? body.ErrorMessages[0] : null;
let error;

// Client errors
if (status === 401 || status === 403 || status === 404) {
return new Error(this.homey.__(`error.${status}`));
error = new Error(this.homey.__(`error.${status}`));
}

// Internal server error
if (status >= 500 && status < 600) {
return new Error(this.homey.__('error.50x'));
error = new Error(this.homey.__('error.50x'));
}

// Custom error message
if (error) {
return new Error(error);
if (filled(body.ErrorMessages) && filled(body.ErrorMessages[0])) {
error = new Error(body.ErrorMessages[0]);
}

// Unknown error
return new Error(this.homey.__('error.unknown'));
if (blank(error)) {
error = new Error(this.homey.__('error.unknown'));
}

error.status = status;
error.statusText = statusText;

return error;
}

// Handle result
Expand Down

0 comments on commit ef3f904

Please sign in to comment.