use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.
the class ParameterProcessorUnitTest method testParsingFailure.
@Test
public void testParsingFailure() {
ParameterProcessor processor = new ParameterProcessorImpl("myParam", ParameterLocation.QUERY, false, mockedParser, mockedValidator);
when(mockedParser.parseParameter(any())).thenThrow(new MalformedValueException("bla"));
assertThatCode(() -> processor.process(new HashMap<>())).isInstanceOf(ParameterProcessorException.class).hasFieldOrPropertyWithValue("errorType", ParameterProcessorException.ParameterProcessorErrorType.PARSING_ERROR).hasFieldOrPropertyWithValue("location", ParameterLocation.QUERY).hasFieldOrPropertyWithValue("parameterName", "myParam").hasCauseInstanceOf(MalformedValueException.class);
}
use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.
the class FormBodyProcessorImpl method process.
@Override
public Future<RequestParameter> process(RoutingContext requestContext) {
try {
MultiMap multiMap = requestContext.request().formAttributes();
JsonObject object = new JsonObject();
for (String key : multiMap.names()) {
List<String> serialized = multiMap.getAll(key);
Map.Entry<String, Object> parsed = parseField(key, serialized);
if (parsed != null)
object.put(parsed.getKey(), parsed.getValue());
}
return valueValidator.validate(object).recover(err -> Future.failedFuture(BodyProcessorException.createValidationError(requestContext.parsedHeaders().contentType().value(), err)));
} catch (MalformedValueException e) {
return Future.failedFuture(BodyProcessorException.createParsingError(requestContext.request().getHeader(HttpHeaders.CONTENT_TYPE), e));
}
}
use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.
the class JsonBodyProcessorImpl method process.
@Override
public Future<RequestParameter> process(RoutingContext requestContext) {
try {
Buffer body = requestContext.getBody();
if (body == null) {
throw BodyProcessorException.createParsingError(requestContext.request().getHeader(HttpHeaders.CONTENT_TYPE), new MalformedValueException("Null body"));
}
Object json = Json.decodeValue(body);
return valueValidator.validate(json).recover(err -> Future.failedFuture(BodyProcessorException.createValidationError(requestContext.request().getHeader(HttpHeaders.CONTENT_TYPE), err)));
} catch (DecodeException e) {
throw BodyProcessorException.createParsingError(requestContext.request().getHeader(HttpHeaders.CONTENT_TYPE), e);
}
}
use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.
the class SplitterCharObjectParser method parse.
@Override
public JsonObject parse(String serialized) throws MalformedValueException {
Map<String, Object> result = new HashMap<>();
String[] values = serialized.split(separator, -1);
// Key value pairs -> odd length not allowed
if (values.length % 2 != 0)
throw new MalformedValueException("Key value pair Object must have odd number of deserialized values");
for (int i = 0; i < values.length; i += 2) {
// empty key not allowed!
if (values[i].length() == 0) {
throw new MalformedValueException("Empty key not allowed");
} else {
Map.Entry<String, Object> parsed = parseField(values[i], values[i + 1]);
if (parsed != null)
result.put(parsed.getKey(), parsed.getValue());
}
}
return new JsonObject(result);
}
use of io.vertx.ext.web.validation.MalformedValueException in project vertx-web by vert-x3.
the class AnyOfOneOfSingleParameterParser method parseParameter.
@Override
@Nullable
public Object parseParameter(Map<String, List<String>> parameterValue) throws MalformedValueException {
List<String> extractedList = parameterValue.remove(parameterName);
if (extractedList == null)
return null;
String extracted = extractedList.get(0);
MalformedValueException lastException = new MalformedValueException("Cannot deserialize parameter " + parameterName + " with value " + extracted);
for (ValueParser<String> p : valueParsers) {
try {
return p.parse(extracted);
} catch (MalformedValueException e) {
lastException = e;
}
}
throw lastException;
}
Aggregations