diff --git a/lib/Client.js b/lib/Client.js index 37e0d17..82aab44 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -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