Closed simonjhy closed 8 months ago
@michel-kraemer Can you help to take a look?
@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.")
}
}
}
@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
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.
@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.
This should be possible. Have a look at the javadoc of CompletableFuture. The whenComplete
method might be the one you're looking for.
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.
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.