@@ -20,17 +20,13 @@ export default function convertEncoding(options) {
2020 throw new PluginError ( pluginName , ErrorBindings . missingEncoding ) ;
2121 }
2222 const { from = UTF8 , to = UTF8 } = options ;
23- if ( ! iconv . encodingExists ( from ) ) {
24- throw new PluginError (
25- pluginName ,
26- `${ ErrorBindings . unsupportedEncoding } : ${ from } ` ,
27- ) ;
28- }
29- if ( ! iconv . encodingExists ( to ) ) {
30- throw new PluginError (
31- pluginName ,
32- `${ ErrorBindings . unsupportedEncoding } : ${ to } ` ,
33- ) ;
23+ for ( const option of [ from , to ] ) {
24+ if ( ! iconv . encodingExists ( option ) ) {
25+ throw new PluginError (
26+ pluginName ,
27+ `${ ErrorBindings . unsupportedEncoding } : ${ option } ` ,
28+ ) ;
29+ }
3430 }
3531 if ( from === to ) {
3632 console . warn ( `${ pluginName } : ${ ErrorBindings . sameEncoding } ` ) ;
@@ -49,17 +45,25 @@ export default function convertEncoding(options) {
4945 return file ;
5046 }
5147 if ( file . isBuffer ( ) ) {
52- const decodedContent = iconv . decode (
53- file . contents ,
54- from ,
55- iconvOptions . decode ,
56- ) ;
57- file . contents = iconv . encode ( decodedContent , to , iconvOptions . encode ) ;
48+ try {
49+ const decodedContent = iconv . decode (
50+ file . contents ,
51+ from ,
52+ iconvOptions . decode ,
53+ ) ;
54+ file . contents = iconv . encode ( decodedContent , to , iconvOptions . encode ) ;
55+ } catch ( err ) {
56+ throw new PluginError ( pluginName , err ) ;
57+ }
5858 }
5959 if ( file . isStream ( ) ) {
60- file . contents = file . contents
61- . pipe ( iconv . decodeStream ( from , iconvOptions . decode ) )
62- . pipe ( iconv . encodeStream ( to , iconvOptions . encode ) ) ;
60+ try {
61+ file . contents = file . contents
62+ . pipe ( iconv . decodeStream ( from , iconvOptions . decode ) )
63+ . pipe ( iconv . encodeStream ( to , iconvOptions . encode ) ) ;
64+ } catch ( err ) {
65+ throw new PluginError ( pluginName , err ) ;
66+ }
6367 }
6468 return file ;
6569 } ,
0 commit comments