eae5d8c807
These conflicts will be resolved in the following commits. We do it this way so that PR review is possible.
69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
import { CancellationToken, ResponseError, LSPErrorCodes } from 'vscode-languageserver';
|
|
|
|
export function formatError(message: string, err: any): string {
|
|
if (err instanceof Error) {
|
|
let error = <Error>err;
|
|
return `${message}: ${error.message}\n${error.stack}`;
|
|
} else if (typeof err === 'string') {
|
|
return `${message}: ${err}`;
|
|
} else if (err) {
|
|
return `${message}: ${err.toString()}`;
|
|
}
|
|
return message;
|
|
}
|
|
|
|
export function runSafeAsync<T>(func: () => Thenable<T>, errorVal: T, errorMessage: string, token: CancellationToken): Thenable<T | ResponseError<any>> {
|
|
return new Promise<T | ResponseError<any>>((resolve) => {
|
|
setImmediate(() => {
|
|
if (token.isCancellationRequested) {
|
|
resolve(cancelValue());
|
|
}
|
|
return func().then(result => {
|
|
if (token.isCancellationRequested) {
|
|
resolve(cancelValue());
|
|
return;
|
|
} else {
|
|
resolve(result);
|
|
}
|
|
}, e => {
|
|
console.error(formatError(errorMessage, e));
|
|
resolve(errorVal);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
export function runSafe<T, E>(func: () => T, errorVal: T, errorMessage: string, token: CancellationToken): Thenable<T | ResponseError<E>> {
|
|
return new Promise<T | ResponseError<E>>((resolve) => {
|
|
setImmediate(() => {
|
|
if (token.isCancellationRequested) {
|
|
resolve(cancelValue());
|
|
} else {
|
|
try {
|
|
let result = func();
|
|
if (token.isCancellationRequested) {
|
|
resolve(cancelValue());
|
|
return;
|
|
} else {
|
|
resolve(result);
|
|
}
|
|
|
|
} catch (e) {
|
|
console.error(formatError(errorMessage, e));
|
|
resolve(errorVal);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function cancelValue<E>() {
|
|
console.log('cancelled');
|
|
return new ResponseError<E>(LSPErrorCodes.RequestCancelled, 'Request cancelled');
|
|
}
|