override console.log/error and catch unhandled rejections

master
Brad Cornes 2021-02-12 13:29:01 +00:00
parent 6868884346
commit f6971ca001
3 changed files with 22 additions and 1 deletions

View File

@ -17,6 +17,7 @@ import normalizePath from 'normalize-path'
import { withUserEnvironment } from './environment' import { withUserEnvironment } from './environment'
import execa from 'execa' import execa from 'execa'
import { klona } from 'klona/full' import { klona } from 'klona/full'
import { formatError } from '../lsp/util/formatError'
function arraysEqual(arr1, arr2) { function arraysEqual(arr1, arr2) {
return ( return (
@ -238,7 +239,7 @@ export default async function getClassNames(
result = await run() result = await run()
console.log('Initialised successfully.') console.log('Initialised successfully.')
} catch (error) { } catch (error) {
console.error('Failed to initialise:', error) console.error(formatError('Failed to initialise:', error))
return null return null
} }

View File

@ -36,12 +36,20 @@ import {
import { createEmitter } from '../lib/emitter' import { createEmitter } from '../lib/emitter'
import { registerDocumentColorProvider } from './providers/documentColorProvider' import { registerDocumentColorProvider } from './providers/documentColorProvider'
import { TextDocument } from 'vscode-languageserver-textdocument' import { TextDocument } from 'vscode-languageserver-textdocument'
import { formatError } from './util/formatError'
let connection = createConnection(ProposedFeatures.all) let connection = createConnection(ProposedFeatures.all)
const state: State = { enabled: false, emitter: createEmitter(connection) } const state: State = { enabled: false, emitter: createEmitter(connection) }
let documents = new TextDocuments(TextDocument) let documents = new TextDocuments(TextDocument)
let workspaceFolder: string | null 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 = { const defaultSettings: Settings = {
tabSize: 2, tabSize: 2,
emmetCompletions: false, emmetCompletions: false,

View File

@ -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 = <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
}