use of sirius.kernel.health.HandledException in project sirius-biz by scireum.
the class BasicJobFactory method buildAndVerifyContext.
@Override
public Map<String, String> buildAndVerifyContext(Function<String, Value> parameterProvider, boolean enforceRequiredParameters, Consumer<HandledException> errorConsumer) {
Map<String, String> context = new HashMap<>();
for (Parameter<?> parameter : getParameters()) {
try {
Value contextValue = parameterProvider.apply(parameter.getName());
String value = parameter.checkAndTransform(contextValue);
if (enforceRequiredParameters && Strings.isEmpty(value) && parameter.isRequired()) {
errorConsumer.accept(Exceptions.createHandled().withNLSKey("Parameter.required").set("name", parameter.getLabel()).handle());
} else {
context.put(parameter.getName(), value);
}
} catch (HandledException e) {
errorConsumer.accept(e);
}
}
return context;
}
use of sirius.kernel.health.HandledException in project sirius-db by scireum.
the class RequestBuilder method executeAsync.
protected void executeAsync(String uri, @Nullable Consumer<Response> onSuccess, @Nullable Consumer<HandledException> onFailure) {
Request request = setupRequest(uri);
restClient.performRequestAsync(request, new ResponseListener() {
@Override
public void onSuccess(Response response) {
if (onSuccess != null) {
onSuccess.accept(response);
}
}
@Override
public void onFailure(Exception exception) {
HandledException handledException = handleAsyncFailure(exception, uri);
if (onFailure != null) {
onFailure.accept(handledException);
}
}
});
}
use of sirius.kernel.health.HandledException in project sirius-db by scireum.
the class InsertQuery method optimisticInsert.
/**
* Inserts an entity into the database, just like {@link #insert(SQLEntity, boolean, boolean)}, but differs in
* handling integrity exceptions.
* <p>
* If a {@link SQLIntegrityConstraintViolationException} is thrown (i.e. a unique constraint is violated), this
* will be handed to the caller without handling the exception and, more essentially, without closing the underlying
* statement and connection. All other database and general errors are handled and thrown as
* {@link HandledException}.
*
* @param entity the entity to insert
* @param invokeChecks determines if before- and after save checks should be performed (<tt>true</tt>) or
* skipped (<tt>false</tt>)
* @param addBatch determines if the query should be executed instantly (<tt>false</tt>) or added to the
* batch update (<tt>true</tt>).
* @throws SQLIntegrityConstraintViolationException when reported by the underlying database
* @throws sirius.kernel.health.HandledException in case of a database error or a general exception
*/
@SuppressWarnings("unchecked")
public void optimisticInsert(@Nonnull E entity, boolean invokeChecks, boolean addBatch) throws SQLIntegrityConstraintViolationException {
try {
if (this.type == null) {
this.type = (Class<E>) entity.getClass();
}
Watch w = Watch.start();
if (invokeChecks) {
getDescriptor().beforeSave(entity);
}
PreparedStatement stmt = prepareStmt();
int i = 1;
for (Property property : propertiesToUpdate) {
stmt.setObject(i++, property.getValueForDatasource(OMA.class, entity));
}
if (descriptor.isVersioned()) {
stmt.setObject(i, 1);
}
if (addBatch) {
addBatch();
} else {
stmt.executeUpdate();
stmt.getConnection().commit();
if (fetchId) {
Row keys = dbs.fetchGeneratedKeys(stmt);
OMA.loadCreatedId(entity, keys);
}
entity.setVersion(1);
}
if (invokeChecks) {
getDescriptor().afterSave(entity);
}
if (!addBatch) {
avarage.addValue(w.elapsedMillis());
}
} catch (SQLIntegrityConstraintViolationException e) {
throw e;
} catch (SQLException e) {
context.safeClose();
throw Exceptions.handle().to(OMA.LOG).error(e).withSystemErrorMessage("A database error occurred while executing an InsertQuery" + " for %s: %s (%s)", type.getName()).handle();
} catch (Exception e) {
throw Exceptions.handle().to(OMA.LOG).error(e).withSystemErrorMessage("An error occurred while executing an InsertQuery" + " for %s: %s (%s)", type.getName()).handle();
}
}
Aggregations