org.bklab.flow.exception.consumer.FluentExceptionConsumer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-vaadin-flow Show documentation
Show all versions of fluent-vaadin-flow Show documentation
Broderick Labs for fluent vaadin flow. Inherits common Vaadin components.
package org.bklab.flow.exception.consumer;
import org.bklab.flow.dialog.ErrorDialog;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.function.Consumer;
public class FluentExceptionConsumer implements Consumer {
protected static final List renders = new ArrayList<>(FluentExceptionMessageRender.exceptionMessageRenders);
public static void addMessageRender(IFluentExceptionMessageRender... exceptionMessageRenders) {
Collections.addAll(renders, exceptionMessageRenders);
}
@Override
public void accept(Exception exception) {
String exceptionClassName = exception.getClass().getName();
IFluentExceptionMessageRender messageRender = renders.stream()
.filter(render -> Arrays.stream(render.className()).anyMatch(exceptionClassName::endsWith))
.findFirst().orElse(null);
if (messageRender != null) {
String message = messageRender.message(exception);
new ErrorDialog(message, exception).header(messageRender.header()).build().open();
LoggerFactory.getLogger(getExceptionClass(exception)).error(message, exception);
return;
}
new ErrorDialog(Objects.toString(exception.getMessage(), "未知错误"), exception).build().open();
LoggerFactory.getLogger(getExceptionClass(exception)).error("错误", exception);
}
private Class> getExceptionClass(Exception exception) {
StackTraceElement[] stackTrace = exception.getStackTrace();
if (stackTrace.length > 2) return stackTrace[2].getClass();
if (stackTrace.length > 0) return stackTrace[0].getClass();
return getClass();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy