private AccessPath expandAccessPath(AccessPath accessPath, Field field) { return accessPath.append(field.getName() + Mapping.SUBFIELD_SEPARATOR, obj -> { try { return field.get(obj); } catch (Exception e) { throw Exceptions.handle() .to(Mixing.LOG) .error(e) .withSystemErrorMessage("Cannot access composite property %s in %s: %s (%s)", field.getName(), field.getDeclaringClass().getName()) .handle(); } }); } }
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(); } }
/** * Releases all resources with graceful error handling */ protected void safeClose() { try { if (stmt != null) { stmt.close(); } } catch (SQLException e) { Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage("An error occured while closing a prepared statement: %s (%s)") .handle(); } }
private void createIndices(MongoDatabase db) { for (IndexDescription idx : indexDescriptions) { Watch w = Watch.start(); try { LOG.INFO("Creating indices in Mongo DB: %s", idx.getClass().getName()); idx.createIndices(db); LOG.INFO("Completed indices for: %s (%s)", idx.getClass().getName(), w.duration()); } catch (Exception t) { Exceptions.handle() .to(LOG) .error(t) .withSystemErrorMessage("Error while creating indices for '%s': %s (%s)", idx.getClass().getName()) .handle(); } } }
/** * Parses the given value and applies it to the given entity if possible. * * @param e the entity to receive the parsed value * @param value the value to parse and apply */ public void parseValue(Object e, Value value) { try { setValue(e, transformValue(value)); } catch (IllegalArgumentException exception) { throw Exceptions.createHandled() .withNLSKey("Property.parseValueErrorMessage") .set("message", exception.getMessage()) .error(exception) .handle(); } }
@Override public void iterate(Function<E, Boolean> handler) { try { qry.iterate(row -> { return invokeHandlerForRow(handler, row); }, getLimit()); } catch (SQLException e) { throw Exceptions.handle() .to(OMA.LOG) .error(e) .withSystemErrorMessage("Cannot transform a row into an entity of type '%s' for query '%s'", descriptor.getType().getName(), qry.toString()) .handle(); } }
@SuppressWarnings("unchecked") protected R getEntityRef(Object entity) { try { return (R) 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(); } }
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(); } }
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(); }
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 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(); } }
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(); }
/** * Boilerplate method the directly handle the given exception without a special message or logger * * @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(Throwable e) { return handle().error(e).handle(); }