private static void createAndCollectClassLoadAction(String className, Class<?> clazz) { try { actions.add((ClassLoadAction) clazz.getDeclaredConstructor().newInstance()); } catch (Exception e) { Exceptions.handle() .error(e) .to(LOG) .withSystemErrorMessage("Failed to instantiate ClassLoadAction: %s - %s (%s)", className) .handle(); } }
private static void startLifecycle(Startable lifecycle) { LOG.INFO("Starting: %s", lifecycle.getClass().getName()); try { lifecycle.started(); } catch (Exception e) { Exceptions.handle() .error(e) .to(LOG) .withSystemErrorMessage("Startup of: %s failed!", lifecycle.getClass().getName()) .handle(); } }
private static void stopLifecycle(Stoppable lifecycle) { LOG.INFO("Stopping: %s", lifecycle.getClass().getName()); try { lifecycle.stopped(); } catch (Exception e) { Exceptions.handle() .error(e) .to(LOG) .withSystemErrorMessage("Stop of: %s failed!", lifecycle.getClass().getName()) .handle(); } }
private Tuple<String, Integer> parsePort(Tuple<String, String> hostnameAndPort) { if (Strings.isFilled(hostnameAndPort.getSecond())) { try { return Tuple.create(hostnameAndPort.getFirst(), Integer.parseInt(hostnameAndPort.getSecond())); } catch (NumberFormatException e) { Exceptions.handle() .to(LOG) .withSystemErrorMessage("Invalid port in 'elasticsearch.hosts': %s %s", hostnameAndPort.getFirst(), hostnameAndPort.getSecond()) .handle(); } } return Tuple.create(hostnameAndPort.getFirst(), DEFAULT_HTTP_PORT); }
@Override protected Object transformToDatasource(Class<? extends BaseMapper<?, ?, ?>> mapperType, Object object) { if (length > 0 && !lob && object != null && ((String) object).length() > length) { throw Exceptions.handle() .to(Mixing.LOG) .withNLSKey("StringProperty.dataTruncation") .set("value", Strings.limit(object, 30)) .set("field", getLabel()) .set("length", ((String) object).length()) .set("maxLength", length) .handle(); } return object; }
private static void handleBeforeSaveMethod(EntityDescriptor descriptor, AccessPath accessPath, Method method) { warnOnWrongVisibility(method); if (descriptor.beforeSaveHandlerCollector == null) { throw Exceptions.handle() .to(Mixing.LOG) .withSystemErrorMessage("Cannot provide a before-save-handler, as the sorted list was" + " already computed. Descriptor: %s, Method: %s.%s", descriptor.getType().getName(), method.getDeclaringClass().getName(), method.getName()) .handle(); } descriptor.beforeSaveHandlerCollector.add(method.getAnnotation(BeforeSave.class).priority(), e -> invokeHandler(accessPath, method, e)); }
@Override protected void onBeforeSaveChecks(Object entity) { R ref = getEntityRef(accessPath.apply(entity)); if (ref.containsNonpersistentValue()) { throw Exceptions.handle() .to(OMA.LOG) .withSystemErrorMessage( "Cannot save '%s' (%s) because the referenced entity '%s' in '%s' was not persisted yet.", entity, entity.getClass().getName(), ref.getValue(), getName()) .handle(); } }
protected SafeMap<?, ?> getMap(Object entity) { try { return (SafeMap<?, ?>) super.getValueFromField(entity); } catch (Exception e) { throw Exceptions.handle() .to(Mixing.LOG) .error(e) .withSystemErrorMessage( "Unable to obtain EntityRef object from entity ref field ('%s' in '%s'): %s (%s)", getName(), descriptor.getType().getName()) .handle(); } }
protected BaseEntityRefList<?, ?> getEntityRefList(Object entity) { try { return (BaseEntityRefList<?, ?>) super.getValueFromField(entity); } catch (Exception e) { throw Exceptions.handle() .to(Mixing.LOG) .error(e) .withSystemErrorMessage( "Unable to obtain EntityRef object from entity ref field ('%s' in '%s'): %s (%s)", getName(), descriptor.getType().getName()) .handle(); } }
protected NestedList<?> getNestedList(Object entity) { try { return (NestedList<?>) super.getValueFromField(entity); } catch (Exception e) { throw Exceptions.handle() .to(Mixing.LOG) .error(e) .withSystemErrorMessage( "Unable to obtain EntityRef object from entity ref field ('%s' in '%s'): %s (%s)", getName(), descriptor.getType().getName()) .handle(); } }
protected JSONObject extractErrorJSON(ResponseException e) { try { JSONObject response = JSON.parseObject(EntityUtils.toString(e.getResponse().getEntity())); return response.getJSONObject(PARAM_ERROR); } catch (IOException ex) { Exceptions.handle(Elastic.LOG, ex); throw Exceptions.handle() .to(Elastic.LOG) .error(e) .withSystemErrorMessage("Elasticsearch (%s) reported an error which cannot be unpacked: %s", e.getResponse().getHost()) .handle(); } }
private void executeInitialStatement(Database database, String initSql, Connection connection) { try (Statement stmt = connection.createStatement()) { stmt.execute(initSql); } catch (SQLException e) { Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage( "An error occured while executing the initial SQL statement for: %s (%s) - %s (%s)", database.getUrl(), initSql) .handle(); } }
protected HandledException queryError(Compiler compiler, Exception e) { return Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage("Error executing query '%s' for type '%s': %s (%s)", compiler, descriptor.getType().getName()) .handle(); }
protected void failOnOverflow(List<E> result) { if (result.size() > MAX_LIST_SIZE) { throw Exceptions.handle() .to(Mixing.LOG) .withSystemErrorMessage("More than %s results were loaded into a list by executing: %s", MAX_LIST_SIZE, this) .handle(); } }
private void safeCloseConnection(Connection connection) { try { connection.close(); } catch (SQLException e) { Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage("An exception occured while closing a database connection: %s (%s)") .handle(); } }
protected RequestBuilder execute(String uri) { try { return tryExecute(uri); } catch (OptimisticLockException e) { throw Exceptions.handle() .to(Elastic.LOG) .error(e) .withSystemErrorMessage("An unexpected optimitic locking error ocurred: %s") .handle(); } }
private void ensureReady() { if (!ready) { throw Exceptions.handle().to(LOG).withSystemErrorMessage("Index is not ready yet.").handle(); } }
protected Connection createConnection(String realm) { try { return oma.getDatabase(realm).getConnection(); } catch (SQLException e) { throw Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage("Failed to open a database connection for realm '%s': %s (%s)", realm) .handle(); } }
/** * Boilerplate method the directly handle the given exception without a special message * * @param log the logger used to log the exception * @param e the exception to handle * @return a <tt>HandledException</tt> which notifies surrounding calls that an error occurred, which has * already been taken care of. */ public static HandledException handle(Log log, Throwable e) { return handle().error(e).to(log).handle(); }
private static void verifyUniquenessOfName(String name) { if (caches.containsKey(name)) { throw Exceptions.handle() .to(LOG) .withSystemErrorMessage("A cache named '%s' has already been created!", name) .handle(); } }