Skip to content

Commit 32b58fc

Browse files
javier-godoypaodb
authored andcommitted
feat: handle UploadException
Close #42
1 parent fa5b61d commit 32b58fc

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/main/java/com/flowingcode/vaadin/addons/errorwindow/ErrorManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package com.flowingcode.vaadin.addons.errorwindow;
2323

24+
import com.vaadin.flow.server.UploadException;
2425
import java.util.Map;
2526
import java.util.Optional;
2627
import java.util.concurrent.ConcurrentHashMap;
@@ -31,6 +32,14 @@ public final class ErrorManager {
3132

3233
static {
3334
factories.put(Object.class, new DefaultErrorWindowFactory());
35+
factories.put(UploadException.class, details -> {
36+
Throwable cause = details.getThrowable().getCause();
37+
if (cause != null) {
38+
showError(cause);
39+
} else {
40+
getErrorWindowFactory(Exception.class).showError(details);
41+
}
42+
});
3443
}
3544

3645
private ErrorManager() {

src/test/java/com/flowingcode/vaadin/addons/errorwindow/ErrorwindowDemo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.vaadin.flow.component.checkbox.Checkbox;
2828
import com.vaadin.flow.component.notification.Notification;
2929
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
30+
import com.vaadin.flow.component.upload.Upload;
3031
import com.vaadin.flow.router.PageTitle;
3132

3233
@PageTitle("Error Window Demo")
@@ -92,6 +93,11 @@ public ErrorwindowDemo() {
9293
"Currently production mode is: " + System.getProperty("productionMode"));
9394
});
9495

96+
Upload upload = new Upload(new NullMemoryBuffer());
97+
upload.addSucceededListener(ev -> {
98+
throw new UnsupportedOperationException("File upload is not allowed");
99+
});
100+
95101
setSizeFull();
96102
add(
97103
productionModeCb,
@@ -102,5 +108,8 @@ public ErrorwindowDemo() {
102108

103109
add(new Button("Navigation error",
104110
ev -> UI.getCurrent().navigate(ThrowInConstructorView.class)));
111+
112+
add(upload);
113+
105114
}
106115
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.flowingcode.vaadin.addons.errorwindow;
2+
3+
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
7+
public class NullMemoryBuffer extends MemoryBuffer {
8+
9+
@Override
10+
public OutputStream receiveUpload(String fileName, String MIMEType) {
11+
super.receiveUpload(fileName, MIMEType);
12+
return new OutputStream() {
13+
@Override
14+
public void write(int b) throws IOException {
15+
// do nothing
16+
}
17+
18+
@Override
19+
public void write(byte[] b, int off, int len) throws IOException {
20+
// do nothing
21+
}
22+
};
23+
}
24+
}

0 commit comments

Comments
 (0)