Throw proper DecodingException in Kotlin Serialization decoders
Closes gh-33138
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -109,7 +109,15 @@ public abstract class KotlinSerializationBinaryDecoder<T extends BinaryFormat> e
|
||||
}
|
||||
return this.byteArrayDecoder
|
||||
.decodeToMono(inputStream, elementType, mimeType, hints)
|
||||
.map(byteArray -> format().decodeFromByteArray(serializer, byteArray));
|
||||
.handle((byteArray, sink) -> {
|
||||
try {
|
||||
sink.next(format().decodeFromByteArray(serializer, byteArray));
|
||||
sink.complete();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
sink.error(new DecodingException("Decoding error: " + ex.getMessage(), ex));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.CodecException;
|
||||
import org.springframework.core.codec.Decoder;
|
||||
import org.springframework.core.codec.DecodingException;
|
||||
import org.springframework.core.codec.StringDecoder;
|
||||
@@ -101,7 +102,14 @@ public abstract class KotlinSerializationStringDecoder<T extends StringFormat> e
|
||||
}
|
||||
return this.stringDecoder
|
||||
.decode(inputStream, elementType, mimeType, hints)
|
||||
.map(string -> format().decodeFromString(serializer, string));
|
||||
.handle((string, sink) -> {
|
||||
try {
|
||||
sink.next(format().decodeFromString(serializer, string));
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
sink.error(processException(ex));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -115,8 +123,20 @@ public abstract class KotlinSerializationStringDecoder<T extends StringFormat> e
|
||||
}
|
||||
return this.stringDecoder
|
||||
.decodeToMono(inputStream, elementType, mimeType, hints)
|
||||
.map(string -> format().decodeFromString(serializer, string));
|
||||
.handle((string, sink) -> {
|
||||
try {
|
||||
sink.next(format().decodeFromString(serializer, string));
|
||||
sink.complete();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
sink.error(processException(ex));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private CodecException processException(IllegalArgumentException ex) {
|
||||
return new DecodingException("Decoding error: " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user