Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

fix(logging): do not use JSON.stringify to Error object #666

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class PluginLoader {
this.logger.info(`Loading ${moduleName} from ${initialDir} (resolved to ${resolvedPath})`)
const result = this.requirePlugin(resolvedPath, moduleName)
if (result.error) {
this.logger.error(`Failed to load module: ${JSON.stringify(result.error)}`)
this.logger.error('Failed to load module:', result.error)
return undefined
}
return result.module
Expand Down
28 changes: 28 additions & 0 deletions src/test/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path'
import * as sinon from 'sinon'
import * as ts from 'typescript'
import { NoopLogger } from '../logging'
import { InMemoryFileSystem } from '../memfs'
import { PluginLoader, PluginModule, PluginModuleFactory } from '../plugins'
import { PluginSettings } from '../request-type'
Expand Down Expand Up @@ -73,5 +74,32 @@ describe('plugins', () => {
sinon.assert.calledOnce(applyProxy)
sinon.assert.calledWithExactly(applyProxy, pluginFactoryFunc, sinon.match(pluginOption))
})

it('should log error messages', async () => {
const memfs = new InMemoryFileSystem('/')
const peerPackagesPath = path.resolve(__filename, '../../../../')
const peerPackagesUri = path2uri(peerPackagesPath)
memfs.add(
peerPackagesUri + '/node_modules/some-plugin/package.json',
'{ "name": "some-plugin", "version": "0.1.1", "main": "plugin.js"}'
)
memfs.add(peerPackagesUri + '/node_modules/some-plugin/plugin.js', '')
const pluginSettings: PluginSettings = {
globalPlugins: ['some-plugin'],
allowLocalPluginLoads: false,
pluginProbeLocations: [],
}
const internalError = new Error('invalid')
const fakeRequire = (_path: string) => { throw internalError }
const logger = new NoopLogger() as NoopLogger & { error: sinon.SinonStub }
sinon.stub(logger, 'error')
const loader = new PluginLoader('/', memfs, pluginSettings, logger, memfs, fakeRequire)
const compilerOptions: ts.CompilerOptions = {}
const applyProxy = sinon.spy()
loader.loadPlugins(compilerOptions, applyProxy)
sinon.assert.notCalled(applyProxy)
sinon.assert.calledWith(logger.error, sinon.match('Failed'), internalError)
sinon.assert.calledWith(logger.error, sinon.match('some-plugin'))
})
})
})