Skip to content

Commit

Permalink
Gracefully handle unified-engine errors (#63)
Browse files Browse the repository at this point in the history
Closes #24
  • Loading branch information
remcohaszing committed Sep 5, 2023
1 parent c3023e7 commit 9ab5368
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export function createUnifiedLanguageServer({
processor = defaultProcessor
}

return new Promise((resolve, reject) => {
return new Promise((resolve) => {
engine(
{
alwaysStringify,
Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
3 changes: 3 additions & 0 deletions test/misconfigured.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {createUnifiedLanguageServer} from 'unified-language-server'

createUnifiedLanguageServer({configurationSection: '', processorName: 'remark'})

0 comments on commit 9ab5368

Please sign in to comment.