Search in sources :

Example 1 with ExceptionContext

use of org.talend.daikon.exception.ExceptionContext in project data-prep by Talend.

the class BaseExportStrategy method getActions.

/**
 * Returns the actions for the preparation with <code>preparationId</code> between <code>startStepId</code> and
 * <code>endStepId</code>.
 *
 * @param preparationId The preparation id, if <code>null</code> or blank, returns <code>{actions: []}</code>
 * @param startStepId A step id that must exist in given preparation id.
 * @param endStepId A step id that must exist in given preparation id.
 * @return The actions that can be parsed by ActionParser.
 * @see org.talend.dataprep.transformation.api.action.ActionParser
 */
protected String getActions(String preparationId, String startStepId, String endStepId) {
    if (Step.ROOT_STEP.id().equals(startStepId)) {
        return getActions(preparationId, endStepId);
    }
    String actions;
    if (StringUtils.isBlank(preparationId)) {
        actions = "{\"actions\": []}";
    } else {
        try {
            final PreparationGetActions startStepActions = applicationContext.getBean(PreparationGetActions.class, preparationId, startStepId);
            final PreparationGetActions endStepActions = applicationContext.getBean(PreparationGetActions.class, preparationId, endStepId);
            final StringWriter actionsAsString = new StringWriter();
            final Action[] startActions = mapper.readValue(startStepActions.execute(), Action[].class);
            final Action[] endActions = mapper.readValue(endStepActions.execute(), Action[].class);
            if (endActions.length > startActions.length) {
                final Action[] filteredActions = (Action[]) ArrayUtils.subarray(endActions, startActions.length, endActions.length);
                LOGGER.debug("Reduced actions list from {} to {} action(s)", endActions.length, filteredActions.length);
                mapper.writeValue(actionsAsString, filteredActions);
            } else {
                LOGGER.debug("Unable to reduce list of actions (has {})", endActions.length);
                mapper.writeValue(actionsAsString, endActions);
            }
            return "{\"actions\": " + actionsAsString + '}';
        } catch (IOException e) {
            final ExceptionContext context = ExceptionContext.build().put("id", preparationId).put("version", endStepId);
            throw new TDPException(UNABLE_TO_READ_PREPARATION, e, context);
        }
    }
    return actions;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Action(org.talend.dataprep.api.preparation.Action) StringWriter(java.io.StringWriter) ExceptionContext(org.talend.daikon.exception.ExceptionContext) PreparationGetActions(org.talend.dataprep.command.preparation.PreparationGetActions) IOException(java.io.IOException)

Example 2 with ExceptionContext

use of org.talend.daikon.exception.ExceptionContext in project data-prep by Talend.

the class DataSetDelete method onExecute.

private HttpRequestBase onExecute(final String dataSetId) {
    final boolean isDatasetUsed = isDatasetUsed(dataSetId);
    // if the dataset is used by preparation(s), the deletion is forbidden
    if (isDatasetUsed) {
        LOG.debug("DataSet {} is used by {} preparation(s) and cannot be deleted", dataSetId);
        final ExceptionContext context = ExceptionContext.build().put("dataSetId", dataSetId);
        throw new TDPException(DATASET_STILL_IN_USE, context);
    }
    return new HttpDelete(datasetServiceUrl + "/datasets/" + dataSetId);
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) HttpDelete(org.apache.http.client.methods.HttpDelete) ExceptionContext(org.talend.daikon.exception.ExceptionContext)

Example 3 with ExceptionContext

use of org.talend.daikon.exception.ExceptionContext in project components by Talend.

the class JDBCRowTestIT method test_die_on_error_as_output.

@SuppressWarnings("rawtypes")
@Test
public void test_die_on_error_as_output() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(?,?)");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    properties.usePreparedStatement.setValue(true);
    properties.preparedStatementTable.indexs.setValue(Arrays.asList(1, 2));
    properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name(), PreparedStatementTable.Type.String.name()));
    properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(4, "a too long value"));
    JDBCRowSink sink = new JDBCRowSink();
    sink.initialize(null, properties);
    ValidationResult result = sink.validate(null);
    Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
    WriteOperation operation = sink.createWriteOperation();
    JDBCRowWriter writer = (JDBCRowWriter) operation.createWriter(null);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
        r1.put(0, 4);
        r1.put(1, "xiaoming");
        writer.write(r1);
        writer.close();
    } catch (ComponentException e) {
        ExceptionContext context = e.getContext();
        Assert.assertTrue(context != null);
        String contextMessage = context.toString();
        Assert.assertTrue(contextMessage != null && !contextMessage.isEmpty());
        Assert.assertNotNull(e.getCause());
    } finally {
        writer.close();
    }
}
Also used : TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ExceptionContext(org.talend.daikon.exception.ExceptionContext) ComponentException(org.talend.components.api.exception.ComponentException) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCRowSink(org.talend.components.jdbc.runtime.JDBCRowSink) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowWriter(org.talend.components.jdbc.runtime.writer.JDBCRowWriter) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 4 with ExceptionContext

use of org.talend.daikon.exception.ExceptionContext in project components by Talend.

the class JDBCOutputTestIT method testDieOnError.

@Test(expected = ComponentException.class)
public void testDieOnError() throws Exception {
    TJDBCOutputDefinition definition = new TJDBCOutputDefinition();
    TJDBCOutputProperties properties = DBTestUtils.createCommonJDBCOutputProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema2(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    DataAction action = DBTestUtils.randomDataActionExceptDelete();
    properties.dataAction.setValue(action);
    properties.dieOnError.setValue(true);
    properties.useBatch.setValue(DBTestUtils.randomBoolean());
    properties.batchSize.setValue(DBTestUtils.randomInt());
    // we set it like this to avoid the dead lock when this case :
    // when die on error and not auto commit mode, we throw the exception, but not call commit or rollback in the finally
    // part, it may make the dead lock for derby
    // in all the javajet db components, we have this issue too, but different db, different result, in my view, we should
    // process
    // it, will create another test to show the dead lock issue for derby
    // or set value to 0 mean use the default commit mode, for derby, it's auto commit
    properties.commitEvery.setValue(null);
    JDBCOutputWriter writer = DBTestUtils.createCommonJDBCOutputWriter(definition, properties);
    try {
        writer.open("wid");
        IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
        r1.put(0, 1);
        r1.put(1, "xiaoming");
        writer.write(r1);
        DBTestUtils.assertSuccessRecord(writer, r1);
        IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
        r2.put(0, 2);
        r2.put(1, "too long value");
        writer.write(r2);
        writer.close();
        Assert.fail("should not run here");
    } catch (ComponentException e) {
        ExceptionContext context = e.getContext();
        Assert.assertTrue(context != null);
        String contextMessage = context.toString();
        Assert.assertTrue(contextMessage != null && !contextMessage.isEmpty());
        Assert.assertNotNull(e.getCause());
        throw e;
    } finally {
        writer.close();
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCOutputDefinition(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition) Schema(org.apache.avro.Schema) ExceptionContext(org.talend.daikon.exception.ExceptionContext) ComponentException(org.talend.components.api.exception.ComponentException) TJDBCOutputProperties(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties) DataAction(org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties.DataAction) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCOutputWriter(org.talend.components.jdbc.runtime.writer.JDBCOutputWriter) Test(org.junit.Test)

Aggregations

ExceptionContext (org.talend.daikon.exception.ExceptionContext)4 Schema (org.apache.avro.Schema)2 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 Test (org.junit.Test)2 ComponentException (org.talend.components.api.exception.ComponentException)2 TDPException (org.talend.dataprep.exception.TDPException)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)1 JDBCRowSink (org.talend.components.jdbc.runtime.JDBCRowSink)1 JDBCOutputWriter (org.talend.components.jdbc.runtime.writer.JDBCOutputWriter)1 JDBCRowWriter (org.talend.components.jdbc.runtime.writer.JDBCRowWriter)1 TJDBCOutputDefinition (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputDefinition)1 TJDBCOutputProperties (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties)1 DataAction (org.talend.components.jdbc.tjdbcoutput.TJDBCOutputProperties.DataAction)1 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)1 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)1 ValidationResult (org.talend.daikon.properties.ValidationResult)1 Action (org.talend.dataprep.api.preparation.Action)1