Browse Source

use error helpers in more places

gh-1187
Rich-Harris 8 years ago
parent
commit
dc53d05614
  1. 11
      src/utils/getExportMode.js
  2. 9
      src/utils/transformBundle.js
  3. 5
      test/function/export-type-mismatch-b/_config.js
  4. 5
      test/function/export-type-mismatch-c/_config.js
  5. 5
      test/function/export-type-mismatch/_config.js
  6. 11
      test/function/throws-only-first-transform-bundle/_config.js
  7. 2
      test/test.js

11
src/utils/getExportMode.js

@ -1,7 +1,11 @@
import { keys } from './object.js'; import { keys } from './object.js';
import error from './error.js';
function badExports ( option, keys ) { 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} ) { 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 ) ) { 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; return exportMode;

9
src/utils/transformBundle.js

@ -1,4 +1,5 @@
import { decode } from 'sourcemap-codec'; import { decode } from 'sourcemap-codec';
import error from './error.js';
export default function transformBundle ( code, plugins, sourceMapChain, options ) { export default function transformBundle ( code, plugins, sourceMapChain, options ) {
return plugins.reduce( ( code, plugin ) => { return plugins.reduce( ( code, plugin ) => {
@ -9,9 +10,11 @@ export default function transformBundle ( code, plugins, sourceMapChain, options
try { try {
result = plugin.transformBundle( code, { format : options.format } ); result = plugin.transformBundle( code, { format : options.format } );
} catch ( err ) { } catch ( err ) {
err.plugin = plugin.name; error({
err.message = `Error transforming bundle${plugin.name ? ` with '${plugin.name}' plugin` : ''}: ${err.message}`; code: 'BAD_BUNDLE_TRANSFORMER',
throw err; message: `Error transforming bundle${plugin.name ? ` with '${plugin.name}' plugin` : ''}: ${err.message}`,
plugin: plugin.name
});
} }
if ( result == null ) return code; if ( result == null ) return code;

5
test/function/export-type-mismatch-b/_config.js

@ -5,7 +5,8 @@ module.exports = {
bundleOptions: { bundleOptions: {
exports: 'blah' exports: 'blah'
}, },
generateError: function ( err ) { generateError: {
assert.ok( /options\.exports must be 'default', 'named', 'none', 'auto', or left unspecified/.test( err.message ) ); code: 'INVALID_EXPORT_OPTION',
message: `options.exports must be 'default', 'named', 'none', 'auto', or left unspecified (defaults to 'auto')`
} }
}; };

5
test/function/export-type-mismatch-c/_config.js

@ -5,7 +5,8 @@ module.exports = {
bundleOptions: { bundleOptions: {
exports: 'none' exports: 'none'
}, },
generateError: function ( err ) { generateError: {
assert.ok( /'none' was specified for options\.exports/.test( err.message ) ); code: 'INVALID_EXPORT_OPTION',
message: `'none' was specified for options.exports, but entry module has following exports: default`
} }
}; };

5
test/function/export-type-mismatch/_config.js

@ -5,7 +5,8 @@ module.exports = {
bundleOptions: { bundleOptions: {
exports: 'default' exports: 'default'
}, },
generateError: function ( err ) { generateError: {
assert.ok( /'default' was specified for options\.exports/.test( err.message ) ); code: 'INVALID_EXPORT_OPTION',
message: `'default' was specified for options.exports, but entry module has following exports: foo`
} }
}; };

11
test/function/throws-only-first-transform-bundle/_config.js

@ -7,19 +7,20 @@ module.exports = {
{ {
name: 'plugin1', name: 'plugin1',
transformBundle: function () { transformBundle: function () {
throw Error('Something happend 1'); throw Error( 'Something happened 1' );
} }
}, },
{ {
name: 'plugin2', name: 'plugin2',
transformBundle: function () { transformBundle: function () {
throw Error('Something happend 2'); throw Error( 'Something happened 2' );
} }
} }
] ]
}, },
generateError: function ( err ) { generateError: {
assert.equal( err.plugin, 'plugin1' ); code: 'BAD_BUNDLE_TRANSFORMER',
assert.equal( err.message, 'Error transforming bundle with \'plugin1\' plugin: Something happend 1' ); plugin: 'plugin1',
message: `Error transforming bundle with 'plugin1' plugin: Something happened 1`
} }
}; };

2
test/test.js

@ -276,7 +276,7 @@ describe( 'rollup', function () {
} }
} catch ( err ) { } catch ( err ) {
if ( config.generateError ) { if ( config.generateError ) {
config.generateError( err ); compareError( err, config.generateError );
} else { } else {
unintendedError = err; unintendedError = err;
} }

Loading…
Cancel
Save