Question

I need to copy some files using scp from a remote host.

If the file is not there I need to display a message.

The problem I have, is that I cannot catch a FileNotFound Exception. All I have is a BuildException, but this is raised even if the connexion cannot be done for instance.

Here is my code

def ant = new AntBuilder()
ant.scp(
    trust:true,
    file:"theFileToLook",
    todir:"destinationFolder",
    keyfile: "myrivateKeyFile",
    verbose:true
)

When I put

catch(Exception e){
    log.error("error", e)
}

I have the following:

java.io.IOException: scp: theFileToLook: No such file or directory
    at org.apache.tools.ant.taskdefs.optional.ssh.ScpFromMessage.startRemoteCpProtocol(ScpFromMessage.java:189)
    at org.apache.tools.ant.taskdefs.optional.ssh.ScpFromMessage.execute(ScpFromMessage.java:143)
    at org.apache.tools.ant.taskdefs.optional.ssh.Scp.download(Scp.java:276)
    at org.apache.tools.ant.taskdefs.optional.ssh.Scp.execute(Scp.java:221)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at MyService$$EODjgsH6.executeScp(MyService.groovy:98)
    at MyService$$EODjgsH6.getFileFromRemoteHost(MyService.groovy:63)
    at MyController.get(RetrieveController.groovy:36)
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

When I put println e.class it displays BuildException. The problem is that if I'm trying to catch the IOException it does not pass inside the catch.

Any ideas ?

Was it helpful?

Solution

So it looks like Ant uses BuildException to wrap any exceptions that are thrown.

To show a message, you'd need to implement something like:

def ant = new AntBuilder()
try {
    ant.scp(
        trust:true,
        file:"theFileToLook",
        todir:"destinationFolder",
        keyfile: "myrivateKeyFile",
        verbose:true
    )
}
catch( BuildException ex ) {
    if( ex.exception instanceof IOException ) {
        println "Whoops!  $ex.exception.message"
    }
    else {
        throw ex
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top