Search in sources :

Example 1 with EInputValidation

use of com.accenture.trac.common.exception.EInputValidation in project tracdap by finos.

the class RestApiTranslator method translateRequestBody.

public Message translateRequestBody(ByteBuf bodyBuffer) {
    if (!hasBody)
        throw new EUnexpected();
    var bodyType = (blankBody != null) ? blankBody : blankRequest;
    try (var jsonStream = new ByteBufInputStream(bodyBuffer);
        var jsonReader = new InputStreamReader(jsonStream)) {
        var bodyBuilder = bodyType.newBuilderForType();
        var jsonParser = JsonFormat.parser();
        jsonParser.merge(jsonReader, bodyBuilder);
        return bodyBuilder.build();
    } catch (InvalidProtocolBufferException e) {
        // Validation failures will go back to users (API users, i.e. application developers)
        // Strip out GSON class name from the error message for readability
        var detailMessage = e.getLocalizedMessage();
        var classNamePrefix = MalformedJsonException.class.getName() + ": ";
        if (detailMessage.startsWith(classNamePrefix))
            detailMessage = detailMessage.substring(classNamePrefix.length());
        var message = String.format("Invalid JSON input for type [%s]: %s", bodyType.getDescriptorForType().getName(), detailMessage);
        log.warn(message);
        throw new EInputValidation(message, e);
    } catch (IOException e) {
        // Shouldn't happen, reader source is a buffer already held in memory
        log.error("Unexpected IO error reading from internal buffer", e);
        throw new EUnexpected();
    }
}
Also used : EInputValidation(com.accenture.trac.common.exception.EInputValidation) InputStreamReader(java.io.InputStreamReader) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) EUnexpected(com.accenture.trac.common.exception.EUnexpected) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 2 with EInputValidation

use of com.accenture.trac.common.exception.EInputValidation in project tracdap by finos.

the class JobLifecycle method convertFlowToModel.

JobState convertFlowToModel(JobState jobState) {
    if (jobState.definition.getJobType() != JobType.RUN_FLOW)
        return jobState;
    var runFlow = jobState.definition.getRunFlow();
    if (runFlow.getModelsCount() != 1)
        throw new EInputValidation("Run flow must be supplied with a single model for the preview implementation");
    var modelSelector = runFlow.getModelsMap().values().stream().findFirst();
    if (modelSelector.isEmpty())
        throw new EInputValidation("Run flow must be supplied with a single model for the preview implementation");
    var runModel = RunModelJob.newBuilder().setModel(modelSelector.get()).putAllParameters(runFlow.getParametersMap()).putAllInputs(runFlow.getInputsMap()).putAllOutputs(runFlow.getOutputsMap()).putAllPriorOutputs(runFlow.getPriorOutputsMap()).addAllOutputAttrs(runFlow.getOutputAttrsList()).build();
    var jobDef = jobState.definition.toBuilder().setJobType(JobType.IMPORT_MODEL).setRunModel(runModel).build();
    var newState = jobState.clone();
    newState.definition = jobDef;
    return newState;
}
Also used : EInputValidation(com.accenture.trac.common.exception.EInputValidation)

Aggregations

EInputValidation (com.accenture.trac.common.exception.EInputValidation)2 EUnexpected (com.accenture.trac.common.exception.EUnexpected)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1