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 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;

9
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;

5
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')`
}
};

5
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`
}
};

5
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`
}
};

11
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`
}
};

2
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;
}

Loading…
Cancel
Save