michel-kraemer / gradle-download-task

📥 Adds a download task to Gradle that displays progress information
Apache License 2.0
686 stars 84 forks source link

Can download task add new property to avoid directly throw exception? #380

Closed simonjhy closed 8 months ago

simonjhy commented 9 months ago

Is your feature request related to a problem? Please describe. we don't want download task direclty throw exception when it can not dowload src file. We want to an anlternative closure if the download task failed and set a ignore this property, then no exception and dowload task can directly returen the existed download file lastt time.

A clear and concise description of what the problem is. Example: I'm always frustrated when [...] we don't want download task direclty throw exception when it can not dowload src file.

Describe the solution you'd like It will be better that task can add a new property to enable or disable throw exception action and give us a hook point that we can add our custimzed code to handle these scenario.

A clear and concise description of what you want to happen. It will be better that task can add a new property to enable or disable throw exception action, that means task will not give an error console result.

Describe alternatives you've considered It will be better that task can add a new property to enable or disable throw exception action.

A clear and concise description of any alternative solutions or features you've considered. It will be better that task can add a new property to enable or disable throw exception action.

simonjhy commented 9 months ago

@michel-kraemer Can you help to take a look?

michel-kraemer commented 9 months ago

@simonjhy How about this?

task downloadFile {
    doLast {
        try {
            download.run {
                src '<enter-your-url-here>'
                dest layout.buildDirectory
            }
        } catch (e) {
            println("Ignore error. Do something else instead.")
        }
    }
}
simonjhy commented 9 months ago

@michel-kraemer i I tried and we can not use this way. Below is we used in our code. We add the download task in our gradle plugin, we don't drectly call download.run. https://github.com/liferay/liferay-portal/blob/7c720a2bc9c2a31d7f3ecd5144844851331557c4/modules/sdk/gradle-plugins-workspace/src/main/java/com/liferay/gradle/plugins/workspace/configurator/RootProjectConfigurator.java#L1009

michel-kraemer commented 9 months ago

I think what you have to do in this case is to write a custom task like this:

class MyDownload extends DefaultTask {
    private def action

    MyDownload() {
        action = new de.undercouch.gradle.tasks.download.DownloadAction(project, this)

        action.src('https://example.com/404')
        action.dest(project.layout.buildDirectory)
    }

    @TaskAction
    void download() {
        action.execute(false).exceptionally(t -> {
            println("Ignore error. Do something else instead.")
        }).thenRun(() -> {
            // copied from de.undercouch.gradle.tasks.download.Download.download()
            if (action.isUpToDate()) {
                getState().setDidWork(false);
            }
        });
    }
}

The important thing here is to pass false to action.execute. This will make the action not throw an exception immediately but return a CompletableFuture that completes exceptionally if the download failed and normally if it succeeded (see the method's javadoc).

I hope this helps. Let me know if it works or if you need more assistance.

simonjhy commented 9 months ago

@michel-kraemer Thank you for your help. I tested your function. When I set false to the value of execute, that will not throw any exception. I hope that I can add some logic to determine what exception I can throw in exceptionally method. For example, when netwok is borke and the download file is existed, then I don't throw exception, but I want to throw excetion when the destinantion file is not existed and the network broke.

michel-kraemer commented 9 months ago

This should be possible. Have a look at the javadoc of CompletableFuture. The whenComplete method might be the one you're looking for.

michel-kraemer commented 8 months ago

Seeing the emojis you sent, it appears this solution has worked for you. I'm closing this issue. If you still have questions, feel free to comment here again.