@unraid/libvirt
Version:
Libvirt bindings for Node.js® - forked from vmngr/libvirt
51 lines • 1.8 kB
JavaScript
import { LibvirtError } from './types.js';
/**
* Handles errors from the native libvirt module by converting them to LibvirtError instances.
* @param error - The error to handle
* @throws {LibvirtError} A standardized libvirt error
*/
export function handleError(error) {
if (error instanceof Error) {
// If it's already an Error instance, convert it to LibvirtError
const libvirtError = new LibvirtError(error.message, error.code || -1, error.domain || -1, error.level || -1, error.str1, error.str2, error.str3);
// Preserve the original stack trace
libvirtError.stack = error.stack;
throw libvirtError;
}
// Handle string errors directly
if (typeof error === 'string') {
const libvirtError = new LibvirtError(error, -1, -1, -1);
Error.captureStackTrace(libvirtError);
throw libvirtError;
}
// Try to get a string representation of the error
let errorMessage = 'Unknown error';
if (error && typeof error.toString === 'function') {
try {
const result = error.toString();
errorMessage = result || errorMessage;
}
catch {
// Keep default message if toString throws
}
}
const libvirtError = new LibvirtError(errorMessage, -1, -1, -1);
Error.captureStackTrace(libvirtError);
throw libvirtError;
}
/**
* Wraps a method call with standardized error handling.
* @param method - Method to call
* @param args - Arguments to pass to the method
* @returns Result of the method call
* @throws {LibvirtError} If an error occurs
*/
export async function wrapMethod(method, ...args) {
try {
return await method(...args);
}
catch (error) {
handleError(error);
}
}
//# sourceMappingURL=error.js.map