From d395c666fc2157b565d869149834bdff740838b1 Mon Sep 17 00:00:00 2001 From: rob bygrave Date: Fri, 26 Oct 2018 01:32:22 +1300 Subject: [PATCH] #1516 - More user friendly exceptions - BeanNotEnhancedException, BeanNotRegisteredException --- src/main/java/io/ebean/Ebean.java | 11 +++++++++ .../config/BeanNotEnhancedException.java | 13 +++++++++++ .../config/BeanNotRegisteredException.java | 13 +++++++++++ .../server/deploy/BeanDescriptorManager.java | 23 +++++++++++++------ .../deploy/BeanEmbeddedMetaFactory.java | 5 ++-- 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 src/main/java/io/ebean/config/BeanNotEnhancedException.java create mode 100644 src/main/java/io/ebean/config/BeanNotRegisteredException.java diff --git a/src/main/java/io/ebean/Ebean.java b/src/main/java/io/ebean/Ebean.java index 09d2d738b..cd27c7109 100644 --- a/src/main/java/io/ebean/Ebean.java +++ b/src/main/java/io/ebean/Ebean.java @@ -2,10 +2,12 @@ package io.ebean; import io.ebean.annotation.TxIsolation; import io.ebean.cache.ServerCacheManager; +import io.ebean.config.BeanNotEnhancedException; import io.ebean.config.ServerConfig; import io.ebean.plugin.Property; import io.ebean.text.csv.CsvReader; import io.ebean.text.json.JsonContext; +import io.ebean.datasource.DataSourceConfigurationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -160,6 +162,15 @@ public final class Ebean { defaultServer = getWithCreate(defaultName.trim()); } } + } catch (BeanNotEnhancedException e) { + throw e; + + } catch (DataSourceConfigurationException e) { + String msg = "Configuration error creating DataSource for the default EbeanServer." + + " This typically means a missing application-test.yml or missing ebean-test-config dependency." + + " See https://ebean-orm.github.io/docs/trouble-shooting#datasource"; + throw new DataSourceConfigurationException(msg, e); + } catch (Throwable e) { logger.error("Error trying to create the default EbeanServer", e); throw new RuntimeException(e); diff --git a/src/main/java/io/ebean/config/BeanNotEnhancedException.java b/src/main/java/io/ebean/config/BeanNotEnhancedException.java new file mode 100644 index 000000000..67ff285da --- /dev/null +++ b/src/main/java/io/ebean/config/BeanNotEnhancedException.java @@ -0,0 +1,13 @@ +package io.ebean.config; + +/** + * Throw when an processing an entity bean that is not bytecode enhanced. + * + * Refer: https://ebean-orm.github.io/docs/trouble-shooting#not-enhanced + */ +public class BeanNotEnhancedException extends IllegalStateException { + + public BeanNotEnhancedException(String msg) { + super(msg); + } +} diff --git a/src/main/java/io/ebean/config/BeanNotRegisteredException.java b/src/main/java/io/ebean/config/BeanNotRegisteredException.java new file mode 100644 index 000000000..29212c24b --- /dev/null +++ b/src/main/java/io/ebean/config/BeanNotRegisteredException.java @@ -0,0 +1,13 @@ +package io.ebean.config; + +/** + * Throw when an processing thinks a bean is not registered. + * + * Refer: https://ebean-orm.github.io/docs/trouble-shooting#not-registered + */ +public class BeanNotRegisteredException extends IllegalStateException { + + public BeanNotRegisteredException(String msg) { + super(msg); + } +} diff --git a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java index 1083235cc..b836faeba 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java +++ b/src/main/java/io/ebeaninternal/server/deploy/BeanDescriptorManager.java @@ -6,6 +6,7 @@ import io.ebean.RawSqlBuilder; import io.ebean.annotation.ConstraintMode; import io.ebean.bean.BeanCollection; import io.ebean.bean.EntityBean; +import io.ebean.config.BeanNotEnhancedException; import io.ebean.config.EncryptKey; import io.ebean.config.EncryptKeyManager; import io.ebean.config.NamingConvention; @@ -388,6 +389,9 @@ public class BeanDescriptorManager implements BeanDescriptorMap { return asOfTableMap; + } catch (BeanNotEnhancedException e) { + throw e; + } catch (RuntimeException e) { logger.error("Error in deployment", e); throw e; @@ -1471,7 +1475,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap { if (isPersistentField(prop)) { throw new IllegalStateException( "If you are running in an IDE with enhancement plugin try a Build -> Rebuild Project to recompile and enhance all entity beans. " + - "Error - property " + propName + " not found in " + reflectProps + " for type " + desc.getBeanType()); + "Error - property " + propName + " not found in " + reflectProps + " for type " + desc.getBeanType()); } } else { @@ -1554,7 +1558,9 @@ public class BeanDescriptorManager implements BeanDescriptorMap { Class beanClass = desc.getBeanType(); if (!hasEntityBeanInterface(beanClass)) { - throw new IllegalStateException("Bean " + beanClass + " is not enhanced?"); + String msg = "Bean " + beanClass + " is not enhanced? If you are running in IDEA or Eclipse check" + + " that the enhancement plugin is installed. See https://ebean-orm.github.io/docs/trouble-shooting#not-enhanced"; + throw new BeanNotEnhancedException(msg); } // the bean already implements EntityBean @@ -1583,7 +1589,7 @@ public class BeanDescriptorManager implements BeanDescriptorMap { // ok to stop and treat just the same as Object.class return; } - throw new IllegalStateException("Super type " + superclass + " is not enhanced?"); + throw new BeanNotEnhancedException("Super type " + superclass + " is not enhanced? See https://ebean-orm.github.io/docs/trouble-shooting#not-enhanced"); } // recursively continue up the inheritance hierarchy @@ -1666,11 +1672,14 @@ public class BeanDescriptorManager implements BeanDescriptorMap { private ElementHelp elementHelper(ManyType manyType) { switch (manyType) { - case LIST: return new ElementHelpList(); - case SET: return new ElementHelpSet(); - case MAP: return new ElementHelpMap(); + case LIST: + return new ElementHelpList(); + case SET: + return new ElementHelpSet(); + case MAP: + return new ElementHelpMap(); default: - throw new IllegalStateException("manyType unexpected "+manyType); + throw new IllegalStateException("manyType unexpected " + manyType); } } diff --git a/src/main/java/io/ebeaninternal/server/deploy/BeanEmbeddedMetaFactory.java b/src/main/java/io/ebeaninternal/server/deploy/BeanEmbeddedMetaFactory.java index cbe297901..016a760e4 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/BeanEmbeddedMetaFactory.java +++ b/src/main/java/io/ebeaninternal/server/deploy/BeanEmbeddedMetaFactory.java @@ -1,5 +1,6 @@ package io.ebeaninternal.server.deploy; +import io.ebean.config.BeanNotRegisteredException; import io.ebeaninternal.server.deploy.meta.DeployBeanPropertyAssocOne; import javax.persistence.PersistenceException; @@ -22,8 +23,8 @@ public class BeanEmbeddedMetaFactory { BeanDescriptor targetDesc = owner.getBeanDescriptor(prop.getTargetType()); if (targetDesc == null) { String msg = "Could not find BeanDescriptor for " + prop.getTargetType() - + ". Perhaps the EmbeddedId class is not registered?"; - throw new PersistenceException(msg); + + ". Perhaps the EmbeddedId class is not registered? See https://ebean-orm.github.io/docs/trouble-shooting#not-registered"; + throw new BeanNotRegisteredException(msg); } // deployment override information (column names)