/** * Search for {@link Cause} by its exception class name. Subclasses are not taken into account. * * @param throwableClassName exception class name * @return Cause instance or null if it is not found */ @Nullable public Cause getCause(String throwableClassName) { for (Cause cause : causes) { if (cause.getClassName().equals(throwableClassName)) { return cause; } } return null; }
@Override public String toString() { StringBuilder sb = new StringBuilder("com.haulmont.cuba.core.global.RemoteException:"); for (Cause cause : causes) { sb.append("\n---\n").append(cause.getClassName()).append(": ").append(cause.getMessage()); } return sb.toString(); } }
/** * @return First exception in the causes list if it is checked or it is supported by client, null otherwise */ @Nullable @SuppressWarnings({"ThrowableResultOfMethodCallIgnored"}) public Exception getFirstCauseException() { if (!causes.isEmpty()) { Throwable t = causes.get(0).getThrowable(); if (t != null) { if (!(t instanceof RuntimeException) && !(t instanceof Error)) { return (Exception) t; } if (!(t instanceof Error) && t.getClass().getAnnotation(SupportedByClient.class) != null) return (Exception) t; } } return null; }
List<RemoteException.Cause> causes = ((RemoteException) rootCause).getCauses(); RemoteException.Cause cause = causes.get(causes.size() - 1); msg = cause.getThrowable() != null ? cause.getThrowable().toString() : cause.getClassName() + ": " + cause.getMessage(); } else { msg = rootCause.toString();
if (!re.getCauses().isEmpty()) { RemoteException.Cause cause = re.getCauses().get(re.getCauses().size() - 1); if (cause.getThrowable() != null) { rootCause = cause.getThrowable(); } else { String className = cause.getClassName(); if (className != null && className.indexOf('.') > 0) { className = className.substring(className.lastIndexOf('.') + 1); msg.append(className).append(": ").append(cause.getMessage());
public WebTimer() { component = new Label(); timerImpl = new CubaTimer(); timerImpl.setExceptionHandler(e -> { int reIdx = ExceptionUtils.indexOfType(e, RemoteException.class); if (reIdx > -1) { RemoteException re = (RemoteException) ExceptionUtils.getThrowableList(e).get(reIdx); for (RemoteException.Cause cause : re.getCauses()) { if (cause.getThrowable() instanceof NoUserSessionException) { log.warn("NoUserSessionException in timer {}, timer will be stopped", timerImpl.getLoggingTimerId()); stop(); break; } } } else if (ExceptionUtils.indexOfThrowable(e, NoUserSessionException.class) > -1) { log.warn("NoUserSessionException in timer {}, timer will be stopped", timerImpl.getLoggingTimerId()); stop(); } throw new RuntimeException("Exception in timer", e); }); }
@Override public boolean handle(ErrorEvent event, App app) { Throwable exception = event.getThrowable(); List<Throwable> list = ExceptionUtils.getThrowableList(exception); for (Throwable throwable : list) { if (classNames.contains(throwable.getClass().getName()) && canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) { doHandle(app, throwable.getClass().getName(), throwable.getMessage(), throwable); return true; } if (throwable instanceof RemoteException) { RemoteException remoteException = (RemoteException) throwable; for (RemoteException.Cause cause : remoteException.getCauses()) { if (classNames.contains(cause.getClassName()) && canHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable())) { doHandle(app, cause.getClassName(), cause.getMessage(), cause.getThrowable()); return true; } } } } return false; }
@Override public boolean handle(Throwable exception, UiContext context) { List<Throwable> list = ExceptionUtils.getThrowableList(exception); for (Throwable throwable : list) { if (classNames.contains(throwable.getClass().getName()) && canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) { doHandle(throwable.getClass().getName(), throwable.getMessage(), throwable, context); return true; } if (throwable instanceof RemoteException) { RemoteException remoteException = (RemoteException) throwable; for (RemoteException.Cause cause : remoteException.getCauses()) { if (classNames.contains(cause.getClassName()) && canHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable())) { doHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable(), context); return true; } } } } return false; }
@Override public boolean handle(Throwable exception, WindowManager windowManager) { List<Throwable> list = ExceptionUtils.getThrowableList(exception); for (Throwable throwable : list) { if (classNames.contains(throwable.getClass().getName()) && canHandle(throwable.getClass().getName(), throwable.getMessage(), throwable)) { doHandle(throwable.getClass().getName(), throwable.getMessage(), throwable, windowManager); return true; } if (throwable instanceof RemoteException) { RemoteException remoteException = (RemoteException) throwable; for (RemoteException.Cause cause : remoteException.getCauses()) { if (classNames.contains(cause.getClassName()) && canHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable())) { doHandle(cause.getClassName(), cause.getMessage(), cause.getThrowable(), windowManager); return true; } } } } return false; }
@ExceptionHandler(Exception.class) @ResponseBody public ResponseEntity<ErrorInfo> handleException(Exception e) { log.error("Exception in REST controller", e); @SuppressWarnings("unchecked") List<Throwable> list = ExceptionUtils.getThrowableList(e); for (Throwable throwable : list) { if (throwable instanceof RemoteException) { RemoteException remoteException = (RemoteException) throwable; for (RemoteException.Cause cause : remoteException.getCauses()) { if (Objects.equals("javax.persistence.OptimisticLockException", cause.getClassName())) { ErrorInfo errorInfo = new ErrorInfo("Optimistic lock", cause.getMessage()); return new ResponseEntity<>(errorInfo, HttpStatus.BAD_REQUEST); } } } } ErrorInfo errorInfo = new ErrorInfo("Server error", ""); return new ResponseEntity<>(errorInfo, HttpStatus.INTERNAL_SERVER_ERROR); }
@Override public boolean handle(Throwable exception, WindowManager windowManager) { List<Throwable> list = ExceptionUtils.getThrowableList(exception); for (Throwable throwable : list) { if (className.contains(throwable.getClass().getName()) && isDateOutOfRangeMessage(throwable.getMessage())) { doHandle(windowManager); return true; } if (throwable instanceof RemoteException) { RemoteException remoteException = (RemoteException) throwable; for (RemoteException.Cause cause : remoteException.getCauses()) { if (className.contains(cause.getClassName()) && isDateOutOfRangeMessage(throwable.getMessage())) { doHandle(windowManager); return true; } } } } return false; }
@SuppressWarnings("unchecked") public RemoteException(Throwable throwable) { List<Throwable> list = ExceptionUtils.getThrowableList(throwable); for (int i = 0; i < list.size(); i++) { Throwable t = list.get(i); boolean suitable = true; List<Throwable> causesOfT = list.subList(i, list.size()); for (Throwable aCauseOfT : causesOfT) { if (!isSuitable(aCauseOfT)) { suitable = false; break; } } if (suitable) causes.add(new Cause(t)); else causes.add(new Cause(t.getClass().getName(), t.getMessage())); } }
protected Throwable removeRemoteException(Throwable throwable) { if (throwable instanceof RemoteException) { RemoteException re = (RemoteException) throwable; for (int i = re.getCauses().size() - 1; i >= 0; i--) { if (re.getCauses().get(i).getThrowable() != null) { return re.getCauses().get(i).getThrowable(); } } } return throwable; }
@Override @Nullable public String getMessage() { if (!causes.isEmpty()) return causes.get(causes.size() - 1).getMessage(); else return null; }