diff --git a/src/utils/getExportMode.js b/src/utils/getExportMode.js index d525550..d61b128 100644 --- a/src/utils/getExportMode.js +++ b/src/utils/getExportMode.js @@ -1,7 +1,11 @@ import { keys } from './object.js'; +import error from './error.js'; function badExports ( option, keys ) { - throw new Error( `'${option}' was specified for options.exports, but entry module has following exports: ${keys.join(', ')}` ); + error({ + code: 'INVALID_EXPORT_OPTION', + message: `'${option}' was specified for options.exports, but entry module has following exports: ${keys.join(', ')}` + }); } export default function getExportMode ( bundle, {exports: exportMode, moduleName, format} ) { @@ -35,7 +39,10 @@ export default function getExportMode ( bundle, {exports: exportMode, moduleName } if ( !/(?:default|named|none)/.test( exportMode ) ) { - throw new Error( `options.exports must be 'default', 'named', 'none', 'auto', or left unspecified (defaults to 'auto')` ); + error({ + code: 'INVALID_EXPORT_OPTION', + message: `options.exports must be 'default', 'named', 'none', 'auto', or left unspecified (defaults to 'auto')` + }); } return exportMode; diff --git a/src/utils/transformBundle.js b/src/utils/transformBundle.js index 8569ad5..03eb966 100644 --- a/src/utils/transformBundle.js +++ b/src/utils/transformBundle.js @@ -1,4 +1,5 @@ import { decode } from 'sourcemap-codec'; +import error from './error.js'; export default function transformBundle ( code, plugins, sourceMapChain, options ) { return plugins.reduce( ( code, plugin ) => { @@ -9,9 +10,11 @@ export default function transformBundle ( code, plugins, sourceMapChain, options try { result = plugin.transformBundle( code, { format : options.format } ); } catch ( err ) { - err.plugin = plugin.name; - err.message = `Error transforming bundle${plugin.name ? ` with '${plugin.name}' plugin` : ''}: ${err.message}`; - throw err; + error({ + code: 'BAD_BUNDLE_TRANSFORMER', + message: `Error transforming bundle${plugin.name ? ` with '${plugin.name}' plugin` : ''}: ${err.message}`, + plugin: plugin.name + }); } if ( result == null ) return code; diff --git a/test/function/export-type-mismatch-b/_config.js b/test/function/export-type-mismatch-b/_config.js index 25a7220..e501330 100644 --- a/test/function/export-type-mismatch-b/_config.js +++ b/test/function/export-type-mismatch-b/_config.js @@ -5,7 +5,8 @@ module.exports = { bundleOptions: { exports: 'blah' }, - generateError: function ( err ) { - assert.ok( /options\.exports must be 'default', 'named', 'none', 'auto', or left unspecified/.test( err.message ) ); + generateError: { + code: 'INVALID_EXPORT_OPTION', + message: `options.exports must be 'default', 'named', 'none', 'auto', or left unspecified (defaults to 'auto')` } }; diff --git a/test/function/export-type-mismatch-c/_config.js b/test/function/export-type-mismatch-c/_config.js index 8df0d58..a1eca46 100644 --- a/test/function/export-type-mismatch-c/_config.js +++ b/test/function/export-type-mismatch-c/_config.js @@ -5,7 +5,8 @@ module.exports = { bundleOptions: { exports: 'none' }, - generateError: function ( err ) { - assert.ok( /'none' was specified for options\.exports/.test( err.message ) ); + generateError: { + code: 'INVALID_EXPORT_OPTION', + message: `'none' was specified for options.exports, but entry module has following exports: default` } }; diff --git a/test/function/export-type-mismatch/_config.js b/test/function/export-type-mismatch/_config.js index 5b321d6..1ff9fd5 100644 --- a/test/function/export-type-mismatch/_config.js +++ b/test/function/export-type-mismatch/_config.js @@ -5,7 +5,8 @@ module.exports = { bundleOptions: { exports: 'default' }, - generateError: function ( err ) { - assert.ok( /'default' was specified for options\.exports/.test( err.message ) ); + generateError: { + code: 'INVALID_EXPORT_OPTION', + message: `'default' was specified for options.exports, but entry module has following exports: foo` } }; diff --git a/test/function/throws-only-first-transform-bundle/_config.js b/test/function/throws-only-first-transform-bundle/_config.js index 9097f52..21804e7 100644 --- a/test/function/throws-only-first-transform-bundle/_config.js +++ b/test/function/throws-only-first-transform-bundle/_config.js @@ -7,19 +7,20 @@ module.exports = { { name: 'plugin1', transformBundle: function () { - throw Error('Something happend 1'); + throw Error( 'Something happened 1' ); } }, { name: 'plugin2', transformBundle: function () { - throw Error('Something happend 2'); + throw Error( 'Something happened 2' ); } } ] }, - generateError: function ( err ) { - assert.equal( err.plugin, 'plugin1' ); - assert.equal( err.message, 'Error transforming bundle with \'plugin1\' plugin: Something happend 1' ); + generateError: { + code: 'BAD_BUNDLE_TRANSFORMER', + plugin: 'plugin1', + message: `Error transforming bundle with 'plugin1' plugin: Something happened 1` } -}; \ No newline at end of file +}; diff --git a/test/test.js b/test/test.js index cf5348b..250b25a 100644 --- a/test/test.js +++ b/test/test.js @@ -276,7 +276,7 @@ describe( 'rollup', function () { } } catch ( err ) { if ( config.generateError ) { - config.generateError( err ); + compareError( err, config.generateError ); } else { unintendedError = err; }