chibenwa commented on code in PR #2991:
URL: https://github.com/apache/james-project/pull/2991#discussion_r3032457950
##########
mailbox/event/json/src/main/scala/org/apache/james/event/json/MailboxEventSerializer.scala:
##########
@@ -456,17 +456,31 @@ class JsonSerialize(mailboxIdFactory: MailboxId.Factory,
messageIdFactory: Messa
class MailboxEventSerializer @Inject()(mailboxIdFactory: MailboxId.Factory,
messageIdFactory: MessageId.Factory, quotaRootDeserializer:
QuotaRootDeserializer) extends EventSerializer {
private val jsonSerialize = new JsonSerialize(mailboxIdFactory,
messageIdFactory, quotaRootDeserializer)
- override def toJson(event: JavaEvent): String = jsonSerialize.toJson(event)
+ override def toJson(event: JavaEvent): Optional[String] =
Optional.of(jsonSerialize.toJson(event))
- override def toJsonBytes(event: JavaEvent): Array[Byte] =
jsonSerialize.toJsonBytes(event)
+ override def toJsonBytes(event: JavaEvent): Optional[Array[Byte]] =
Optional.of(jsonSerialize.toJsonBytes(event))
- override def toJsonBytes(event: util.Collection[JavaEvent]): Array[Byte] =
jsonSerialize.toJsonBytes(event)
+ override def toJsonBytes(event: util.Collection[JavaEvent]):
Optional[Array[Byte]] = Optional.of(jsonSerialize.toJsonBytes(event))
def fromJson(json: String): JsResult[JavaEvent] =
jsonSerialize.fromJson(json)
- override def toJson(event: util.Collection[JavaEvent]): String =
jsonSerialize.toJson(event)
+ override def toJson(event: util.Collection[JavaEvent]): Optional[String] =
Optional.of(jsonSerialize.toJson(event))
- override def asEvents(serialized: String): util.List[JavaEvent] =
jsonSerialize.fromJsonAsEvents(serialized).get.asJava
+ override def asEvents(serialized: String): Optional[util.List[JavaEvent]] = {
+ val result = jsonSerialize.fromJsonAsEvents(serialized)
+ if (result.isError) {
+ Optional.empty()
Review Comment:
It feels sad to swallow the error IMO
Maybe not relying on optional but a specifalized Either?
```
sealed interface DeserializationResult {
static DeserializationResult of(Optional<Event> maybeEvent) {
return event.map(Success::new)
.getOrElse(() -> new Failure("Could not deserialize event"));
}
Event event();
class Success(Event event) implements DeserializationResult {}
class Failure(String validationMessage) implement DeserializationResult {
Event event() {
throw new RuntimeException(validationMessage);
}
}
}
```
Use it in place of ambiguous `Optional<Event>` in the API
And then here we could build a specialized exception message ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]