teamcfadvance / cfstatic

CfStatic is a framework for managing the inclusion and packaging of CSS and JavaScript in CFML applications.
https://teamcfadvance.github.io/cfstatic
MIT License
102 stars 35 forks source link

Bad JS gives hard CF error #48

Closed cutterbl closed 12 years ago

cutterbl commented 12 years ago

When a file has bad JS, and your app initializes and tries to minify files, a hard CF error is thrown. Currently the compressJs() method of the YuiCompressor.cfc has the following:

try {
    compressor = $loadJavaClass('com.yahoo.platform.yui.compressor.JavaScriptCompressor').init(input, reporter);
    compressor.compress(output, javaCast('int', arguments.linebreak), javaCast('boolean', arguments.munge), javaCast('boolean',arguments.verbose), javaCast('boolean', arguments.preserveAllSemiColons), javaCast('boolean',arguments.disableOptimizations));

} catch('org.mozilla.javascript.EvaluatorException' e){
    $throw(type="org.cfstatic.util.YuiCompressor.badJs", message="There was an error compressing your javascript: '#e.message#'. Please see the error detail for the problematic javascript source.", detail=arguments.source);
}

This doesn't catch the EvaluatorException, because that exception is nested in the call stack. The error thrown is of type 'Object' stating

An exception occurred while instantiating a Java object. The class must not be an interface or an abstract class. Error: ''.

That message has a Cause of error type 'java.lang.reflect.InvocationTargetException', with the message of [empty string]. This error has a Cause of type 'org.mozilla.javascript.EvaluatorException'.

DominicWatson commented 12 years ago

Ah dang, the error throws more usefully in Railo and OpenBD (you get the Java error type rather than 'Object'). I'll make a fix.

DominicWatson commented 12 years ago

Are you able to supply the code to get into the call stack and examine the error (I'm not running ACF anywhere)? I guess it needs to be something like:

var badJsMessage = "";

try {
    compressor = $loadJavaClass( 'com.yahoo.platform.yui.compressor.JavaScriptCompressor' ).init( input, reporter );
    compressor.compress( output, javaCast('int', arguments.linebreak), javaCast('boolean', arguments.munge), javaCast('boolean',arguments.verbose), javaCast('boolean', arguments.preserveAllSemiColons), javaCast('boolean',arguments.disableOptimizations) );

} catch( 'org.mozilla.javascript.EvaluatorException' e ){
    badJsMessage = e.message;
} catch( 'Object' e ){
    if ( e.cause.type EQ 'org.mozilla.javascript.EvaluatorException' ) {
        badJsMessage = e.cause.message;
    } else {
        $throw( argumentCollection = e );
    }
}

if ( Len( badJsMessage ) ) {
    $throw( type="org.cfstatic.util.YuiCompressor.badJs", message="There was an error compressing your javascript: '#badJsMessage#'. Please see the error detail for the problematic javascript source.", detail=arguments.source );
}