homebridge-homeconnect
Version:
A Homebridge plugin that connects Home Connect appliances to Apple HomeKit
47 lines • 1.97 kB
JavaScript
// Homebridge plugin for Home Connect home appliances
// Copyright © 2023-2025 Alexander Thoukydides
import { APIError } from './api-errors.js';
// Log an error
let lastLoggedError;
export function logError(log, when, err) {
try {
// Suppress duplicate reports
if (lastLoggedError === err)
return err;
lastLoggedError = err;
// Log the top-level error message
const message = err instanceof Error ? `${err.name}: ${err.message}` : String(err);
log.error(`[${when}] ${message}`);
const prefix = ' '.repeat(when.length + 3);
// Log the request details for API errors
if (err instanceof APIError) {
log.error(`${err.request.method} ${err.request.path}`);
}
// Log any history of causes or sub-errors
logSubErrors(log, prefix, err);
// Log any stack backtrace (for the top-level error only)
if (err instanceof Error && err.stack)
log.debug(err.stack);
}
catch { /* empty */ }
return err;
}
// Log any sub-errors (causes or aggregates) of an error, formatted as a tree
function logSubErrors(log, prefix, err) {
// Collect all aggregate or causal errors
const subErrs = [];
if (err instanceof APIError && err.errCause)
subErrs.push(err.errCause);
if (err instanceof Error && err.cause)
subErrs.push(err.cause);
if (err instanceof AggregateError && Array.isArray(err.errors))
subErrs.push(...err.errors);
// Log the collected errors formatted as a tree
subErrs.forEach((subErr, index) => {
const subPrefix = index < subErrs.length - 1 ? ['├─', '│ '] : ['└─', ' '];
const message = subErr instanceof Error ? `${subErr.name}: ${subErr.message}` : String(subErr);
log.error(`${prefix}${subPrefix[0]} ${message}`);
logSubErrors(log, `${prefix}${subPrefix[1]} `, subErr);
});
}
//# sourceMappingURL=log-error.js.map