UNPKG

homebridge-aeg-robot

Version:

AEG RX9 / Electrolux Pure i9 robot vacuum plugin for Homebridge

82 lines 3.24 kB
// Homebridge plugin for AEG RX 9 / Electrolux Pure i9 robot vacuum // Copyright © 2022-2024 Alexander Thoukydides import { STATUS_CODES } from 'http'; import { assertIsDefined } from './utils.js'; import { checkers } from './ti/aegapi-types.js'; // Base for reporting all Electrolux Group API errors export class AEGAPIError extends Error { request; response; errCause; constructor(request, response, message, options) { // Standard error object initialisation super(message); this.request = request; this.response = response; Error.captureStackTrace(this, AEGAPIError); this.name = 'Electrolux Group API Error'; if (options?.cause) this.errCause = options.cause; } } // API could not be authorised export class AEGAPIAuthorisationError extends AEGAPIError { constructor(request, response, message, options) { super(request, response, message, options); Error.captureStackTrace(this, AEGAPIAuthorisationError); this.name = 'Electrolux Group API Authorisation Error'; } } // API returned a non-success status code export class AEGAPIStatusCodeError extends AEGAPIError { text; constructor(request, response, text, options) { super(request, response, AEGAPIStatusCodeError.getMessage(response, text), options); this.text = text; Error.captureStackTrace(this, AEGAPIStatusCodeError); this.name = 'Electrolux Group API Status Code Error'; } // Construct an error message from a response static getMessage(response, text) { const statusCode = response.statusCode; const statusCodeName = STATUS_CODES[statusCode]; const description = AEGAPIStatusCodeError.getBodyDescription(text) ?? 'No error message returned'; return `[${statusCode} ${statusCodeName}] ${description}`; } // Attempt to extract a useful description from the response body static getBodyDescription(text) { if (text === '') return null; let message = text; try { const json = JSON.parse(text); if (checkers.ErrorResponse.test(json)) { message = json.message; if (json.error) message += ` (${json.error})`; if (json.detail) message += `: ${json.detail}`; } } catch { /* empty */ } return message; } } // API returned a response that failed checker validation export class AEGAPIValidationError extends AEGAPIError { validation; constructor(request, response, validation, options) { super(request, response, AEGAPIValidationError.getMessage(validation), options); this.validation = validation; Error.captureStackTrace(this, AEGAPIValidationError); this.name = 'Electrolux Group API Validation Error'; } // Construct an error message from a checker validation error static getMessage(errors) { assertIsDefined(errors[0]); const description = `${errors[0].path} ${errors[0].message}`; return `Structure validation failed (${description})`; } } //# sourceMappingURL=aegapi-error.js.map