@pkerschbaum/code-oss-file-service
Version:
VS Code ([microsoft/vscode](https://github.com/microsoft/vscode)) includes a rich "`FileService`" and "`DiskFileSystemProvider`" abstraction built on top of Node.js core modules (`fs`, `path`) and Electron's `shell` module. This package allows to use that
194 lines • 6.54 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.createErrorWithActions = exports.isErrorWithActions = exports.ExpectedError = exports.NotSupportedError = exports.NotImplementedError = exports.getErrorMessage = exports.disposed = exports.readonly = exports.illegalState = exports.illegalArgument = exports.canceled = exports.CancellationError = exports.isPromiseCanceledError = exports.transformErrorForSerialization = exports.onUnexpectedExternalError = exports.onUnexpectedError = exports.setUnexpectedErrorHandler = exports.errorHandler = exports.ErrorHandler = void 0;
// Avoid circular dependency on EventEmitter by implementing a subset of the interface.
class ErrorHandler {
constructor() {
this.listeners = [];
this.unexpectedErrorHandler = function (e) {
setTimeout(() => {
if (e.stack) {
throw new Error(e.message + '\n\n' + e.stack);
}
throw e;
}, 0);
};
}
addListener(listener) {
this.listeners.push(listener);
return () => {
this._removeListener(listener);
};
}
emit(e) {
this.listeners.forEach((listener) => {
listener(e);
});
}
_removeListener(listener) {
this.listeners.splice(this.listeners.indexOf(listener), 1);
}
setUnexpectedErrorHandler(newUnexpectedErrorHandler) {
this.unexpectedErrorHandler = newUnexpectedErrorHandler;
}
getUnexpectedErrorHandler() {
return this.unexpectedErrorHandler;
}
onUnexpectedError(e) {
this.unexpectedErrorHandler(e);
this.emit(e);
}
// For external errors, we don't want the listeners to be called
onUnexpectedExternalError(e) {
this.unexpectedErrorHandler(e);
}
}
exports.ErrorHandler = ErrorHandler;
exports.errorHandler = new ErrorHandler();
function setUnexpectedErrorHandler(newUnexpectedErrorHandler) {
exports.errorHandler.setUnexpectedErrorHandler(newUnexpectedErrorHandler);
}
exports.setUnexpectedErrorHandler = setUnexpectedErrorHandler;
function onUnexpectedError(e) {
// ignore errors from cancelled promises
if (!isPromiseCanceledError(e)) {
exports.errorHandler.onUnexpectedError(e);
}
return undefined;
}
exports.onUnexpectedError = onUnexpectedError;
function onUnexpectedExternalError(e) {
// ignore errors from cancelled promises
if (!isPromiseCanceledError(e)) {
exports.errorHandler.onUnexpectedExternalError(e);
}
return undefined;
}
exports.onUnexpectedExternalError = onUnexpectedExternalError;
function transformErrorForSerialization(error) {
if (error instanceof Error) {
let { name, message } = error;
const stack = error.stacktrace || error.stack;
return {
$isError: true,
name,
message,
stack
};
}
// return as is
return error;
}
exports.transformErrorForSerialization = transformErrorForSerialization;
const canceledName = 'Canceled';
/**
* Checks if the given error is a promise in canceled state
*/
function isPromiseCanceledError(error) {
return error instanceof Error && error.name === canceledName && error.message === canceledName;
}
exports.isPromiseCanceledError = isPromiseCanceledError;
// !!!IMPORTANT!!!
// Do NOT change this class because it is also used as an API-type.
class CancellationError extends Error {
constructor() {
super(canceledName);
this.name = this.message;
}
}
exports.CancellationError = CancellationError;
/**
* Returns an error that signals cancellation.
*/
function canceled() {
const error = new Error(canceledName);
error.name = error.message;
return error;
}
exports.canceled = canceled;
function illegalArgument(name) {
if (name) {
return new Error(`Illegal argument: ${name}`);
}
else {
return new Error('Illegal argument');
}
}
exports.illegalArgument = illegalArgument;
function illegalState(name) {
if (name) {
return new Error(`Illegal state: ${name}`);
}
else {
return new Error('Illegal state');
}
}
exports.illegalState = illegalState;
function readonly(name) {
return name
? new Error(`readonly property '${name} cannot be changed'`)
: new Error('readonly property cannot be changed');
}
exports.readonly = readonly;
function disposed(what) {
const result = new Error(`${what} has been disposed`);
result.name = 'DISPOSED';
return result;
}
exports.disposed = disposed;
function getErrorMessage(err) {
if (!err) {
return 'Error';
}
if (err.message) {
return err.message;
}
if (err.stack) {
return err.stack.split('\n')[0];
}
return String(err);
}
exports.getErrorMessage = getErrorMessage;
class NotImplementedError extends Error {
constructor(message) {
super('NotImplemented');
if (message) {
this.message = message;
}
}
}
exports.NotImplementedError = NotImplementedError;
class NotSupportedError extends Error {
constructor(message) {
super('NotSupported');
if (message) {
this.message = message;
}
}
}
exports.NotSupportedError = NotSupportedError;
class ExpectedError extends Error {
constructor() {
super(...arguments);
this.isExpected = true;
}
}
exports.ExpectedError = ExpectedError;
function isErrorWithActions(obj) {
const candidate = obj;
return candidate instanceof Error && Array.isArray(candidate.actions);
}
exports.isErrorWithActions = isErrorWithActions;
function createErrorWithActions(message, options = Object.create(null)) {
const result = new Error(message);
if (options.actions) {
result.actions = options.actions;
}
return result;
}
exports.createErrorWithActions = createErrorWithActions;
//# sourceMappingURL=errors.js.map
;