From f1b0cc18b6974fc417a37e628f6a5bb0b3d8e701 Mon Sep 17 00:00:00 2001 From: Konstantin Krivopustov Date: Mon, 28 Sep 2015 09:25:37 +0000 Subject: [PATCH] EclipseLink ORM (unfetched attribute exception handler). #PL-5412 --- .../exception/OptimisticExceptionHandler.java | 5 +- ...etchedAttributeAccessExceptionHandler.java | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 modules/gui/src/com/haulmont/cuba/gui/exception/UnfetchedAttributeAccessExceptionHandler.java diff --git a/modules/gui/src/com/haulmont/cuba/gui/exception/OptimisticExceptionHandler.java b/modules/gui/src/com/haulmont/cuba/gui/exception/OptimisticExceptionHandler.java index 6a0e82f73e..c42b29816a 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/exception/OptimisticExceptionHandler.java +++ b/modules/gui/src/com/haulmont/cuba/gui/exception/OptimisticExceptionHandler.java @@ -28,13 +28,12 @@ public class OptimisticExceptionHandler extends AbstractGenericExceptionHandler protected Messages messages; public OptimisticExceptionHandler() { - // todo EL - super("org.springframework.orm.jpa.JpaOptimisticLockingFailureException"/*, "org.apache.openjpa.persistence.OptimisticLockException"*/); + super("javax.persistence.OptimisticLockException"); } @Override protected void doHandle(String className, String message, @Nullable Throwable throwable, WindowManager windowManager) { - Pattern pattern = Pattern.compile("\\[([^-]*)-"); + Pattern pattern = Pattern.compile("Class> (.+)"); Matcher matcher = pattern.matcher(message); String entityClassName = ""; if (matcher.find()) { diff --git a/modules/gui/src/com/haulmont/cuba/gui/exception/UnfetchedAttributeAccessExceptionHandler.java b/modules/gui/src/com/haulmont/cuba/gui/exception/UnfetchedAttributeAccessExceptionHandler.java new file mode 100644 index 0000000000..f78ee065e2 --- /dev/null +++ b/modules/gui/src/com/haulmont/cuba/gui/exception/UnfetchedAttributeAccessExceptionHandler.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2008-2015 Haulmont. All rights reserved. + * Use is subject to license terms, see http://www.cuba-platform.com/license for details. + */ + +package com.haulmont.cuba.gui.exception; + +import com.haulmont.cuba.gui.WindowManager; +import com.haulmont.cuba.gui.components.Frame; +import org.apache.commons.lang.exception.ExceptionUtils; + +import javax.annotation.ManagedBean; +import javax.annotation.Nullable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author Konstantin Krivopustov + * @version $Id$ + */ +@ManagedBean("cuba_UnfetchedAttributeAccessExceptionHandler") +public class UnfetchedAttributeAccessExceptionHandler extends AbstractGenericExceptionHandler { + + private static final Pattern PATTERN = Pattern.compile("at (.+)\\._persistence_get_(.+)\\("); + + public UnfetchedAttributeAccessExceptionHandler() { + super("org.eclipse.persistence.exceptions.ValidationException"); + } + + @Override + protected boolean canHandle(String className, String message, @Nullable Throwable throwable) { + return message.contains("An attempt was made to traverse a relationship using indirection that had a null Session"); + } + + @Override + protected void doHandle(String className, String message, @Nullable Throwable throwable, WindowManager windowManager) { + String msg = "It usually occurs when the attribute is not included into a view"; + String defaultMsg = "\n\nSee the log to find out what attribute caused the exception"; + + if (throwable != null) { + Matcher matcher = PATTERN.matcher(ExceptionUtils.getStackTrace(throwable)); + if (matcher.find()) { + msg += "\n\nEntity: " + matcher.group(1) + "\nAttribute: " + matcher.group(2); + } else { + msg += defaultMsg; + } + } else { + msg += defaultMsg; + } + + windowManager.showNotification("Unfetched attribute access error", msg, Frame.NotificationType.ERROR); + } +}