Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for error handling #10

Open
javier-godoy opened this issue Apr 23, 2022 · 0 comments
Open

Add support for error handling #10

javier-godoy opened this issue Apr 23, 2022 · 0 comments

Comments

@javier-godoy
Copy link

If createInputStream fails, it's not possible to intercept the exception and handle it.
Moreover, if setDisableOnClick is set, the button will remain disabled (because no DownloadStartsEvent will occur).

Executors.newSingleThreadExecutor().execute(() -> {
try {
InputStream inputStream = inputStreamCallback.createInputStream();
optionalUI.ifPresent(ui -> ui.access(() -> {
StreamResource href = new StreamResource(fileNameCallback.get(), () -> inputStream);
href.setCacheTime(0);
anchor.setHref(href);
anchor.getElement().callJsFunction("click");
}));
} catch (Exception e) {
throw new RuntimeException(e);
}
});

As a workaround, one need to write error handling logic inside of the InputStreamFactory:

btnDownload = new LazyDownloadButton("Download",
	() -> "foo.pdf",
	() -> {
		try {
			return new ByteArrayInputStream(generateFile());
		} catch (RuntimeException e) {
			btnDownload.getUI().ifPresent(ui->ui.access(()->{
				// restore normal state, so that the user can download the content again
				btnDownload.setText("Download");
				btnDownload.setEnabled(true);
				Notification.show("Download failed").addThemeVariants(NotificationVariant.LUMO_ERROR);
			}));
			throw e;
		}
	});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant