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;
}
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);
}
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();
}
}
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();
}
}
Aggregations