use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.
the class GenericJsonDeserializer method deserialize.
@Override
public DeserializedEvent deserialize(String raw) {
GenericJsonEvent devent = new GenericJsonEvent(null);
JsonElement elm;
try {
elm = parser.parse(raw);
} catch (JsonSyntaxException e) {
throw new DeserializationException(e);
}
if (!elm.isJsonObject()) {
throw new DeserializationException("event is not a json object");
}
JsonObject obj = elm.getAsJsonObject();
/*
* Convert fields which are nested json strings into json objects
*/
for (FieldConfig fconfig : this.nestedFieldConfigs) {
if (obj.has(fconfig.getField())) {
JsonElement msg = obj.get(fconfig.getField());
/*
* Find the JSON in the string and replace the field
*/
NestedData data = deserialize(msg);
obj.remove(fconfig.getField());
obj.add(fconfig.getField(), data.nested);
/*
* If the string contained data before the JSON store it in a new field
*/
if (fconfig.getPrefixField() != null && data.prefix != null) {
obj.add(fconfig.getPrefixField(), data.prefix);
}
}
}
if (rootNodeOverridePath != null) {
obj = JsonPathProvider.read(obj, rootNodeOverridePath);
if (obj == null) {
throw new DeserializationException(rootNodeOverridePath + " path not found in object");
}
}
devent.setPayload(obj);
return devent;
}
use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.
the class Re2jRegexDeserializer method deserialize.
@Override
public DeserializedEvent deserialize(String raw) {
Matcher m = this.pattern.matcher(raw);
if (!m.matches()) {
throw new DeserializationException("raw event does not match string");
}
int groups = m.groupCount();
Map<String, Object> mapping = new HashMap<>(groups);
for (int i = 0; i < groups && i < fields.size(); i++) {
String str = m.group(i + 1);
ReFieldConfig field = this.fields.get(i);
mapping.put(field.getName(), RegexDeserializer.parse(str, field.getType()));
}
return new RegexEvent(mapping);
}
use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.
the class RegexDeserializer method deserialize.
@Override
public DeserializedEvent deserialize(String raw) {
Matcher m = this.pattern.matcher(raw);
if (!m.matches()) {
throw new DeserializationException("raw event does not match string");
}
int groups = m.groupCount();
Map<String, Object> mapping = new HashMap<>(groups);
for (int i = 0; i < groups && i < fields.size(); i++) {
String str = m.group(i + 1);
ReFieldConfig field = this.fields.get(i);
mapping.put(field.getName(), parse(str, field.getType()));
}
return new RegexEvent(mapping);
}
use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.
the class BaseHandlerTest method testFilterFailedDeserialization.
@Test
public void testFilterFailedDeserialization() throws HandlerException {
BaseHandler.CONFIG_FILE = "/config/handler_config.json";
handler.skipWriteStats = true;
List<DummyEvent> events = new ArrayList<DummyEvent>(1);
events.add(new DummyEvent("foo", 0));
TestContext context = new TestContext();
context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:tag");
handler.init(context);
DeserializerProcessor proc = handler.sources.get(0).getDeserProcessor();
Deserializer deserSpy = spy(proc.getDeserializer());
doThrow(new DeserializationException("expected")).when(deserSpy).deserialize(anyString());
proc.setDeserializer(deserSpy);
handler.handler(events, context);
assertEquals(0, BufferedTransporter.output.size());
}
use of com.nextdoor.bender.deserializer.DeserializationException in project bender by Nextdoor.
the class BaseHandlerTest method testSerializationException.
@Test
public void testSerializationException() throws HandlerException, SerializationException {
BaseHandler.CONFIG_FILE = "/config/handler_config.json";
handler.skipWriteStats = true;
List<DummyEvent> events = new ArrayList<DummyEvent>(1);
events.add(new DummyEvent("foo", 0));
TestContext context = new TestContext();
context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:tag");
handler.init(context);
Serializer serSpy = spy(handler.ser.getSerializer());
doThrow(new DeserializationException("")).when(serSpy).serialize(any());
handler.ser.setSerializer(serSpy);
handler.handler(events, context);
assertEquals(1, handler.ser.getErrorCountStat().getValue());
}
Aggregations