@zenfs/core
Version:
A filesystem, anywhere
61 lines (60 loc) • 1.92 kB
JavaScript
// SPDX-License-Identifier: LGPL-3.0-or-later
import { Errno, Exception, setUVMessage, UV } from 'kerium';
/**
* @deprecated Use {@link Exception} instead
* @category Internals
*/
export const ErrnoError = Exception;
export function withPath(e, path) {
e.path = path;
return e;
}
/**
* @internal @hidden
*/
export function wrap(fs, prop, path, dest) {
const extra = typeof path === 'string' ? { path, dest, syscall: prop.endsWith('Sync') ? prop.slice(0, -4) : prop } : path;
const fn = fs[prop];
if (typeof fn !== 'function')
throw new TypeError(`${prop} is not a function`);
return function (...args) {
try {
return fn.call(fs, ...args);
}
catch (e) {
throw setUVMessage(Object.assign(e, extra));
}
};
}
/**
* @internal
* Wraps an `fs` so that thrown errors aren't empty
*/
export function withExceptionContext(fs, context) {
return new Proxy(fs, {
get(target, prop) {
const value = Reflect.get(target, prop);
if (typeof value != 'function')
return value;
return function __withContext(...args) {
try {
const result = value.apply(target, args);
if (!(result instanceof Promise))
return result;
return result.catch((e) => {
if ('code' in e)
throw setUVMessage(Object.assign(e, context));
if (e in Errno) {
const ex = UV(e, context);
Error.captureStackTrace(ex, __withContext);
}
throw e;
});
}
catch (e) {
throw setUVMessage(Object.assign(e, context));
}
};
},
});
}