UNPKG

@felixgeelhaar/govee-api-client

Version:

Enterprise-grade TypeScript client library for the Govee Developer REST API

52 lines 2.34 kB
import { GoveeApiClientError } from './GoveeApiClientError'; export class GoveeApiError extends GoveeApiClientError { constructor(message, statusCode, apiErrorCode, apiMessage, cause) { super(message, cause); this.code = 'GOVEE_API_ERROR'; this.statusCode = statusCode; this.apiErrorCode = apiErrorCode; this.apiMessage = apiMessage; } static fromResponse(statusCode, responseBody) { // Handle string responses if (typeof responseBody === 'string' && responseBody.trim().length > 0) { return new GoveeApiError(`Govee API error (HTTP ${statusCode}): ${responseBody}`, statusCode); } // Handle null, undefined, or non-object responses if (!responseBody || typeof responseBody !== 'object') { return new GoveeApiError(`Govee API error (HTTP ${statusCode})`, statusCode); } // Handle object responses with potential undefined/null properties const apiErrorCode = responseBody.code; const apiMessage = responseBody.message; // Construct meaningful error message, handling undefined/null apiMessage const message = apiMessage && typeof apiMessage === 'string' && apiMessage.trim().length > 0 ? `Govee API error (HTTP ${statusCode}): ${apiMessage}` : `Govee API error (HTTP ${statusCode})`; return new GoveeApiError(message, statusCode, apiErrorCode, apiMessage); } isDeviceOffline() { return (this.statusCode === 400 && ((this.apiMessage?.toLowerCase().includes('offline') ?? false) || (this.apiMessage?.toLowerCase().includes('not available') ?? false))); } isUnsupportedCommand() { return (this.statusCode === 400 && ((this.apiMessage?.toLowerCase().includes('not support') ?? false) || (this.apiMessage?.toLowerCase().includes('unsupported') ?? false))); } toObject() { const obj = { ...super.toObject(), statusCode: this.statusCode, }; if (this.apiErrorCode !== undefined) { obj.apiErrorCode = this.apiErrorCode; } if (this.apiMessage !== undefined) { obj.apiMessage = this.apiMessage; } return obj; } } //# sourceMappingURL=GoveeApiError.js.map