Search in sources :

Example 6 with HandledException

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;
}
Also used : HandledException(sirius.kernel.health.HandledException) HashMap(java.util.HashMap) Value(sirius.kernel.commons.Value) QueryString(sirius.web.http.QueryString)

Example 7 with HandledException

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);
            }
        }
    });
}
Also used : Response(org.elasticsearch.client.Response) HandledException(sirius.kernel.health.HandledException) Request(org.elasticsearch.client.Request) ResponseListener(org.elasticsearch.client.ResponseListener) ResponseException(org.elasticsearch.client.ResponseException) HandledException(sirius.kernel.health.HandledException) OptimisticLockException(sirius.db.mixing.OptimisticLockException) IOException(java.io.IOException)

Example 8 with 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();
    }
}
Also used : OMA(sirius.db.jdbc.OMA) SQLException(java.sql.SQLException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) Watch(sirius.kernel.commons.Watch) PreparedStatement(java.sql.PreparedStatement) Row(sirius.db.jdbc.Row) Property(sirius.db.mixing.Property) HandledException(sirius.kernel.health.HandledException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SQLException(java.sql.SQLException)

Aggregations

HandledException (sirius.kernel.health.HandledException)8 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Request (org.elasticsearch.client.Request)2 Response (org.elasticsearch.client.Response)2 ResponseException (org.elasticsearch.client.ResponseException)2 OptimisticLockException (sirius.db.mixing.OptimisticLockException)2 Watch (sirius.kernel.commons.Watch)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 StandardCharsets (java.nio.charset.StandardCharsets)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1