From 9ab5368b6a668abde7024cc6c1480beb52ec6e61 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Tue, 5 Sep 2023 13:18:44 +0200 Subject: [PATCH] Gracefully handle unified-engine errors (#63) Closes #24 --- lib/index.js | 14 +++++++------- test/index.js | 41 +++++++++++++++++++++++++++++++++++++++++ test/misconfigured.js | 3 +++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 test/misconfigured.js diff --git a/lib/index.js b/lib/index.js index ffaec60..c6bbc80 100644 --- a/lib/index.js +++ b/lib/index.js @@ -230,7 +230,7 @@ export function createUnifiedLanguageServer({ processor = defaultProcessor } - return new Promise((resolve, reject) => { + return new Promise((resolve) => { engine( { alwaysStringify, @@ -248,21 +248,21 @@ export function createUnifiedLanguageServer({ streamError: new PassThrough(), streamOut: new PassThrough() }, - (error, _, context) => { + (error) => { // An error never occured and can’t be reproduced. This is an internal // error in unified-engine. If a plugin throws, it’s reported as a // vfile message. - /* c8 ignore start */ if (error) { - reject(error) - } else { - resolve((context && context.files) || []) + for (const file of files) { + file.message(error).fatal = true + } } + + resolve(files) } ) }) } - /* c8 ignore stop */ /** * Process various LSP text documents using unified and send back the diff --git a/test/index.js b/test/index.js index 2fec7b2..d33ce89 100644 --- a/test/index.js +++ b/test/index.js @@ -346,6 +346,47 @@ test('global configuration `requireConfig`', async () => { ) }) +test('unified-engine errors', async () => { + startLanguageServer('misconfigured.js') + await connection.sendRequest(InitializeRequest.type, { + processId: null, + rootUri: null, + capabilities: {}, + workspaceFolders: null + }) + const uri = new URL('lsp.md', import.meta.url).href + + const openDiagnosticsPromise = createOnNotificationPromise( + PublishDiagnosticsNotification.type + ) + connection.sendNotification(DidOpenTextDocumentNotification.type, { + textDocument: { + uri, + languageId: 'markdown', + version: 1, + text: '# hi' + } + }) + const openDiagnostics = await openDiagnosticsPromise + + assert.deepEqual( + openDiagnostics.diagnostics.map(({message, ...rest}) => ({ + message: cleanStack(message, 3), + ...rest + })), + [ + { + message: + 'Missing `processor`\n' + + 'Error: Missing `processor`\n' + + ' at engine (index.js:1:1)', + range: {start: {line: 0, character: 0}, end: {line: 0, character: 0}}, + severity: 1 + } + ] + ) +}) + test('uninstalled processor so `window/showMessageRequest`', async () => { startLanguageServer('missing-package.js') diff --git a/test/misconfigured.js b/test/misconfigured.js new file mode 100644 index 0000000..756877d --- /dev/null +++ b/test/misconfigured.js @@ -0,0 +1,3 @@ +import {createUnifiedLanguageServer} from 'unified-language-server' + +createUnifiedLanguageServer({configurationSection: '', processorName: 'remark'})