Search in sources :

Example 1 with ProtocolMessageEnum

use of com.google.protobuf.ProtocolMessageEnum in project core-java by SpineEventEngine.

the class RequestValidator method validateMessage.

/**
 * Checks whether the {@code Message} of the given request conforms the constraints
 *
 * @param request the request message to validate.
 * @return an instance of exception,
 *         or {@code Optional.absent()} if the request message is valid.
 */
private Optional<InvalidRequestException> validateMessage(M request) {
    final List<ConstraintViolation> violations = MessageValidator.newInstance().validate(request);
    if (violations.isEmpty()) {
        return Optional.absent();
    }
    final ValidationError validationError = ValidationError.newBuilder().addAllConstraintViolation(violations).build();
    final ProtocolMessageEnum errorCode = getInvalidMessageErrorCode();
    final String typeName = errorCode.getDescriptorForType().getFullName();
    final String errorTextTemplate = getErrorText(request);
    final String errorText = format("%s %s", errorTextTemplate, toText(violations));
    final Error.Builder errorBuilder = Error.newBuilder().setType(typeName).setCode(errorCode.getNumber()).setValidationError(validationError).setMessage(errorText);
    final Error error = errorBuilder.build();
    return Optional.of(onInvalidMessage(formatExceptionMessage(request), request, error));
}
Also used : ProtocolMessageEnum(com.google.protobuf.ProtocolMessageEnum) ConstraintViolation(io.spine.validate.ConstraintViolation) ValidationError(io.spine.validate.ValidationError) Error(io.spine.base.Error) ValidationError(io.spine.validate.ValidationError)

Example 2 with ProtocolMessageEnum

use of com.google.protobuf.ProtocolMessageEnum in project core-java by SpineEventEngine.

the class RequestValidator method checkSupported.

/**
 * Checks whether this request is supported, forms the proper {@link Error error}
 * and packs it into an exception.
 *
 * @param request the request to check for support
 * @return an instance of exception or {@code Optional.absent()} if the request is supported.
 */
private Optional<InvalidRequestException> checkSupported(M request) {
    final Optional<RequestNotSupported<M>> supported = isSupported(request);
    if (!supported.isPresent()) {
        return Optional.absent();
    }
    final RequestNotSupported<M> result = supported.get();
    final ProtocolMessageEnum unsupportedErrorCode = result.getErrorCode();
    final String errorMessage = result.getErrorMessage();
    final String errorTypeName = unsupportedErrorCode.getDescriptorForType().getFullName();
    final Error.Builder errorBuilder = Error.newBuilder().setType(errorTypeName).setCode(unsupportedErrorCode.getNumber()).setMessage(errorMessage);
    final Error error = errorBuilder.build();
    final InvalidRequestException exception = result.createException(errorMessage, request, error);
    return Optional.of(exception);
}
Also used : ProtocolMessageEnum(com.google.protobuf.ProtocolMessageEnum) ValidationError(io.spine.validate.ValidationError) Error(io.spine.base.Error)

Example 3 with ProtocolMessageEnum

use of com.google.protobuf.ProtocolMessageEnum in project j2objc by google.

the class CompatibilityTest method testProtocolMessageEnum.

public void testProtocolMessageEnum() throws Exception {
    TypicalData data = TypicalData.newBuilder().setMyEnumType(TypicalData.EnumType.VALUE1).build();
    ProtocolMessageEnum type = data.getMyEnumType();
    assertEquals(1, type.getNumber());
    // Test casting to ProtocolMessageEnum.
    Object value = TypicalData.EnumType.VALUE2;
    type = (ProtocolMessageEnum) value;
}
Also used : TypicalData(protos.TypicalData) ProtocolMessageEnum(com.google.protobuf.ProtocolMessageEnum)

Aggregations

ProtocolMessageEnum (com.google.protobuf.ProtocolMessageEnum)3 Error (io.spine.base.Error)2 ValidationError (io.spine.validate.ValidationError)2 ConstraintViolation (io.spine.validate.ConstraintViolation)1 TypicalData (protos.TypicalData)1