From f6971ca00147d014692d8337cc6aeedbb593b645 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 12 Feb 2021 13:29:01 +0000 Subject: [PATCH] override console.log/error and catch unhandled rejections --- .../src/class-names/index.js | 3 ++- packages/tailwindcss-intellisense/src/lsp/server.ts | 8 ++++++++ .../src/lsp/util/formatError.ts | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 packages/tailwindcss-intellisense/src/lsp/util/formatError.ts diff --git a/packages/tailwindcss-intellisense/src/class-names/index.js b/packages/tailwindcss-intellisense/src/class-names/index.js index 06177bd..2cb1b9b 100644 --- a/packages/tailwindcss-intellisense/src/class-names/index.js +++ b/packages/tailwindcss-intellisense/src/class-names/index.js @@ -17,6 +17,7 @@ import normalizePath from 'normalize-path' import { withUserEnvironment } from './environment' import execa from 'execa' import { klona } from 'klona/full' +import { formatError } from '../lsp/util/formatError' function arraysEqual(arr1, arr2) { return ( @@ -238,7 +239,7 @@ export default async function getClassNames( result = await run() console.log('Initialised successfully.') } catch (error) { - console.error('Failed to initialise:', error) + console.error(formatError('Failed to initialise:', error)) return null } diff --git a/packages/tailwindcss-intellisense/src/lsp/server.ts b/packages/tailwindcss-intellisense/src/lsp/server.ts index 9e5243c..53bd94f 100644 --- a/packages/tailwindcss-intellisense/src/lsp/server.ts +++ b/packages/tailwindcss-intellisense/src/lsp/server.ts @@ -36,12 +36,20 @@ import { import { createEmitter } from '../lib/emitter' import { registerDocumentColorProvider } from './providers/documentColorProvider' import { TextDocument } from 'vscode-languageserver-textdocument' +import { formatError } from './util/formatError' let connection = createConnection(ProposedFeatures.all) const state: State = { enabled: false, emitter: createEmitter(connection) } let documents = new TextDocuments(TextDocument) let workspaceFolder: string | null +console.log = connection.console.log.bind(connection.console) +console.error = connection.console.error.bind(connection.console) + +process.on('unhandledRejection', (e: any) => { + connection.console.error(formatError(`Unhandled exception`, e)) +}) + const defaultSettings: Settings = { tabSize: 2, emmetCompletions: false, diff --git a/packages/tailwindcss-intellisense/src/lsp/util/formatError.ts b/packages/tailwindcss-intellisense/src/lsp/util/formatError.ts new file mode 100644 index 0000000..017eb5b --- /dev/null +++ b/packages/tailwindcss-intellisense/src/lsp/util/formatError.ts @@ -0,0 +1,12 @@ +// https://github.com/vscode-langservers/vscode-json-languageserver/blob/master/src/utils/runner.ts +export function formatError(message: string, err: any): string { + if (err instanceof Error) { + let 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 +}