From c266482e3f443721ac6314b05ffd37a5c676f9a1 Mon Sep 17 00:00:00 2001 From: Lucas Pedroza Date: Tue, 10 Sep 2024 18:28:18 +0200 Subject: [PATCH 1/4] wip --- .../java/com/fauna/client/FaunaClient.java | 3 +- .../java/com/fauna/client/RequestBuilder.java | 5 ++- .../com/fauna/codec/UTF8FaunaGenerator.java | 20 +++------- .../java/com/fauna/codec/UTF8FaunaParser.java | 39 +++++++++---------- .../fauna/codec/codecs/BaseDocumentCodec.java | 8 ++-- .../com/fauna/codec/codecs/BaseRefCodec.java | 6 +-- .../com/fauna/codec/codecs/BoolCodec.java | 4 +- .../fauna/codec/codecs/ByteArrayCodec.java | 4 +- .../com/fauna/codec/codecs/ByteCodec.java | 4 +- .../com/fauna/codec/codecs/CharCodec.java | 4 +- .../com/fauna/codec/codecs/ClassCodec.java | 8 ++-- .../com/fauna/codec/codecs/DoubleCodec.java | 4 +- .../com/fauna/codec/codecs/DynamicCodec.java | 4 +- .../com/fauna/codec/codecs/EnumCodec.java | 4 +- .../com/fauna/codec/codecs/FloatCodec.java | 4 +- .../com/fauna/codec/codecs/InstantCodec.java | 4 +- .../java/com/fauna/codec/codecs/IntCodec.java | 4 +- .../com/fauna/codec/codecs/ListCodec.java | 4 +- .../fauna/codec/codecs/LocalDateCodec.java | 4 +- .../com/fauna/codec/codecs/LongCodec.java | 4 +- .../java/com/fauna/codec/codecs/MapCodec.java | 6 +-- .../com/fauna/codec/codecs/ModuleCodec.java | 4 +- .../com/fauna/codec/codecs/NullableCodec.java | 4 +- .../com/fauna/codec/codecs/PageCodec.java | 8 ++-- .../com/fauna/codec/codecs/QueryArrCodec.java | 4 +- .../com/fauna/codec/codecs/QueryCodec.java | 4 +- .../fauna/codec/codecs/QueryLiteralCodec.java | 4 +- .../com/fauna/codec/codecs/QueryObjCodec.java | 4 +- .../com/fauna/codec/codecs/QueryValCodec.java | 4 +- .../com/fauna/codec/codecs/ShortCodec.java | 4 +- .../codecs/StreamTokenResponseCodec.java | 6 +-- .../com/fauna/codec/codecs/StringCodec.java | 4 +- .../exception/ClientRequestException.java | 12 ++++++ .../exception/ClientResponseException.java | 22 +++++++++++ .../com/fauna/exception/CodecException.java | 12 ++++++ .../fauna/exception/ProtocolException.java | 8 ++-- .../fauna/exception/WireParseException.java | 12 ++++++ .../com/fauna/response/QueryResponse.java | 1 + .../java/com/fauna/response/QuerySuccess.java | 1 + 39 files changed, 158 insertions(+), 107 deletions(-) create mode 100644 src/main/java/com/fauna/exception/ClientRequestException.java create mode 100644 src/main/java/com/fauna/exception/ClientResponseException.java create mode 100644 src/main/java/com/fauna/exception/CodecException.java create mode 100644 src/main/java/com/fauna/exception/WireParseException.java diff --git a/src/main/java/com/fauna/client/FaunaClient.java b/src/main/java/com/fauna/client/FaunaClient.java index d1d225e6..38928389 100644 --- a/src/main/java/com/fauna/client/FaunaClient.java +++ b/src/main/java/com/fauna/client/FaunaClient.java @@ -5,6 +5,7 @@ import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.DefaultCodecRegistry; import com.fauna.exception.ClientException; +import com.fauna.exception.ClientRequestException; import com.fauna.exception.FaunaException; import com.fauna.query.QueryOptions; import com.fauna.stream.StreamRequest; @@ -85,7 +86,7 @@ public CompletableFuture> asyncQuery(Query fql) { */ public CompletableFuture> asyncQuery(Query fql, Class resultClass, QueryOptions options) { if (Objects.isNull(fql)) { - throw new IllegalArgumentException("The provided FQL query is null."); + throw new ClientRequestException("The provided FQL query is null."); } Codec codec = codecProvider.get(resultClass, null); return new RetryHandler>(getRetryStrategy()).execute(FaunaClient.makeAsyncRequest( diff --git a/src/main/java/com/fauna/client/RequestBuilder.java b/src/main/java/com/fauna/client/RequestBuilder.java index 20028b4f..fe396722 100644 --- a/src/main/java/com/fauna/client/RequestBuilder.java +++ b/src/main/java/com/fauna/client/RequestBuilder.java @@ -6,6 +6,7 @@ import com.fauna.codec.CodecProvider; import com.fauna.env.DriverEnvironment; import com.fauna.exception.ClientException; +import com.fauna.exception.ClientRequestException; import com.fauna.query.QueryOptions; import com.fauna.stream.StreamRequest; import com.fauna.query.builder.Query; @@ -107,8 +108,8 @@ public HttpRequest buildRequest(Query fql, QueryOptions options, CodecProvider p gen.writeEndObject(); String body = gen.serialize(); return builder.POST(HttpRequest.BodyPublishers.ofString(body)).build(); - } catch (IOException e) { - throw new ClientException("Unable to build Fauna Query request.", e); + } catch (Exception e) { + throw new ClientRequestException("Unable to build Fauna Query request.", e); } } diff --git a/src/main/java/com/fauna/codec/UTF8FaunaGenerator.java b/src/main/java/com/fauna/codec/UTF8FaunaGenerator.java index 8fed3aa2..73ca9623 100644 --- a/src/main/java/com/fauna/codec/UTF8FaunaGenerator.java +++ b/src/main/java/com/fauna/codec/UTF8FaunaGenerator.java @@ -4,11 +4,9 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fauna.types.Module; -import com.fauna.exception.ClientException; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.time.DateTimeException; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneOffset; @@ -319,12 +317,8 @@ public void writeStringValue(String value) throws IOException { * @throws IOException If an I/O error occurs. */ public void writeDateValue(LocalDate value) throws IOException { - try { - String str = value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); - writeTaggedValue("@date", str); - } catch (DateTimeException e) { - throw new ClientException("Error writing date value", e); - } + String str = value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + writeTaggedValue("@date", str); } /** @@ -334,13 +328,9 @@ public void writeDateValue(LocalDate value) throws IOException { * @throws IOException If an I/O error occurs. */ public void writeTimeValue(Instant value) throws IOException { - try { - Instant instant = value.atZone(ZoneOffset.UTC).toInstant(); - String formattedTime = instant.toString(); - writeTaggedValue("@time", formattedTime); - } catch (DateTimeException e) { - throw new ClientException("Error writing time value", e); - } + Instant instant = value.atZone(ZoneOffset.UTC).toInstant(); + String formattedTime = instant.toString(); + writeTaggedValue("@time", formattedTime); } /** diff --git a/src/main/java/com/fauna/codec/UTF8FaunaParser.java b/src/main/java/com/fauna/codec/UTF8FaunaParser.java index e696814a..38c00468 100644 --- a/src/main/java/com/fauna/codec/UTF8FaunaParser.java +++ b/src/main/java/com/fauna/codec/UTF8FaunaParser.java @@ -3,8 +3,8 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; +import com.fauna.exception.WireParseException; import com.fauna.types.Module; -import com.fauna.exception.ClientException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -152,7 +152,7 @@ public boolean read() throws IOException { currentFaunaTokenType = FaunaTokenType.NULL; break; default: - throw new ClientException( + throw new WireParseException( "Unhandled JSON token type " + currentToken + "."); } } else { @@ -225,7 +225,7 @@ private void handleStartObject() throws IOException { currentFaunaTokenType = FaunaTokenType.START_OBJECT; break; default: - throw new ClientException( + throw new WireParseException( "Unexpected token following StartObject: " + jsonParser.currentToken()); } } @@ -247,7 +247,7 @@ private void handleEndObject() { } else if (startToken.equals(FaunaTokenType.START_OBJECT)) { currentFaunaTokenType = END_OBJECT; } else { - throw new ClientException( + throw new WireParseException( "Unexpected token " + startToken + ". This might be a bug."); } } @@ -261,7 +261,7 @@ private void handleTaggedString(FaunaTokenType token) throws IOException { private void advanceTrue() { if (!advance()) { - throw new ClientException("Unexpected end of underlying JSON reader."); + throw new WireParseException("Unexpected end of underlying JSON reader."); } } @@ -269,13 +269,12 @@ private boolean advance() { try { return Objects.nonNull(jsonParser.nextToken()); } catch (IOException e) { - throw new ClientException("Failed to advance underlying JSON reader.", e); + throw new WireParseException("Failed to advance underlying JSON reader.", e); } } private void validateTaggedType(FaunaTokenType type) { - if (currentFaunaTokenType != type || taggedTokenValue == null - || !(taggedTokenValue instanceof String)) { + if (currentFaunaTokenType != type || taggedTokenValue == null) { throw new IllegalStateException( "CurrentTokenType is a " + currentFaunaTokenType.toString() + ", not a " + type.toString() + "."); @@ -291,14 +290,14 @@ private void validateTaggedTypes(FaunaTokenType... types) { public Character getValueAsCharacter() { validateTaggedType(INT); - return Character.valueOf((char) Integer.parseInt(taggedTokenValue)); + return (char) Integer.parseInt(taggedTokenValue); } public String getValueAsString() { try { return jsonParser.getValueAsString(); } catch (IOException e) { - throw new ClientException("Error getting the current token as String", e); + throw new WireParseException("Error getting the current token as String", e); } } @@ -316,7 +315,7 @@ public Byte getValueAsByte() { try { return Byte.parseByte(taggedTokenValue); } catch (NumberFormatException e) { - throw new ClientException("Error getting the current token as Byte", e); + throw new WireParseException("Error getting the current token as Byte", e); } } @@ -325,7 +324,7 @@ public Short getValueAsShort() { try { return Short.parseShort(taggedTokenValue); } catch (NumberFormatException e) { - throw new ClientException("Error getting the current token as Short", e); + throw new WireParseException("Error getting the current token as Short", e); } } @@ -335,7 +334,7 @@ public Integer getValueAsInt() { try { return Integer.parseInt(taggedTokenValue); } catch (NumberFormatException e) { - throw new ClientException("Error getting the current token as Integer", e); + throw new WireParseException("Error getting the current token as Integer", e); } } @@ -343,7 +342,7 @@ public Boolean getValueAsBoolean() { try { return jsonParser.getValueAsBoolean(); } catch (IOException e) { - throw new ClientException("Error getting the current token as Boolean", e); + throw new WireParseException("Error getting the current token as Boolean", e); } } @@ -352,7 +351,7 @@ public LocalDate getValueAsLocalDate() { try { return LocalDate.parse(taggedTokenValue); } catch (DateTimeParseException e) { - throw new ClientException("Error getting the current token as LocalDate", e); + throw new WireParseException("Error getting the current token as LocalDate", e); } } @@ -361,7 +360,7 @@ public Instant getValueAsTime() { try { return Instant.parse(taggedTokenValue); } catch (DateTimeParseException e) { - throw new ClientException("Error getting the current token as LocalDateTime", e); + throw new WireParseException("Error getting the current token as LocalDateTime", e); } } @@ -370,7 +369,7 @@ public Float getValueAsFloat() { try { return Float.parseFloat(taggedTokenValue); } catch (NumberFormatException e) { - throw new ClientException("Error getting the current token as Float", e); + throw new WireParseException("Error getting the current token as Float", e); } } @@ -379,7 +378,7 @@ public Double getValueAsDouble() { try { return Double.parseDouble(taggedTokenValue); } catch (NumberFormatException e) { - throw new ClientException("Error getting the current token as Double", e); + throw new WireParseException("Error getting the current token as Double", e); } } @@ -388,7 +387,7 @@ public Long getValueAsLong() { try { return Long.parseLong(taggedTokenValue); } catch (NumberFormatException e) { - throw new ClientException("Error getting the current token as Long", e); + throw new WireParseException("Error getting the current token as Long", e); } } @@ -396,7 +395,7 @@ public Module getValueAsModule() { try { return new Module(taggedTokenValue); } catch (Exception e) { - throw new ClientException("Error getting the current token as Module", e); + throw new WireParseException("Error getting the current token as Module", e); } } } \ No newline at end of file diff --git a/src/main/java/com/fauna/codec/codecs/BaseDocumentCodec.java b/src/main/java/com/fauna/codec/codecs/BaseDocumentCodec.java index 0e444eed..46719f27 100644 --- a/src/main/java/com/fauna/codec/codecs/BaseDocumentCodec.java +++ b/src/main/java/com/fauna/codec/codecs/BaseDocumentCodec.java @@ -3,9 +3,9 @@ import com.fauna.codec.CodecProvider; import com.fauna.codec.FaunaTokenType; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; +import com.fauna.exception.CodecException; import com.fauna.types.BaseDocument; import com.fauna.types.Document; import com.fauna.types.NamedDocument; @@ -28,11 +28,11 @@ public BaseDocument decode(UTF8FaunaParser parser) throws IOException { case START_REF: var o = BaseRefCodec.SINGLETON.decode(parser); // if we didn't throw a null ref, we can't deal with it - throw new ClientException(unexpectedTypeWhileDecoding(o.getClass())); + throw new CodecException(unexpectedTypeWhileDecoding(o.getClass())); case START_DOCUMENT: return (BaseDocument) decodeInternal(parser); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } @@ -42,7 +42,7 @@ private Object decodeInternal(UTF8FaunaParser parser) throws IOException { while (parser.read() && parser.getCurrentTokenType() != FaunaTokenType.END_DOCUMENT) { if (parser.getCurrentTokenType() != FaunaTokenType.FIELD_NAME) { - throw new ClientException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType())); + throw new CodecException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType())); } String fieldName = parser.getValueAsString(); diff --git a/src/main/java/com/fauna/codec/codecs/BaseRefCodec.java b/src/main/java/com/fauna/codec/codecs/BaseRefCodec.java index 3e601e67..25641204 100644 --- a/src/main/java/com/fauna/codec/codecs/BaseRefCodec.java +++ b/src/main/java/com/fauna/codec/codecs/BaseRefCodec.java @@ -2,9 +2,9 @@ import com.fauna.codec.FaunaTokenType; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; +import com.fauna.exception.CodecException; import com.fauna.types.*; import java.io.IOException; @@ -21,7 +21,7 @@ public BaseRef decode(UTF8FaunaParser parser) throws IOException { case START_REF: return (BaseRef) decodeInternal(parser); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } @@ -30,7 +30,7 @@ private Object decodeInternal(UTF8FaunaParser parser) throws IOException { while (parser.read() && parser.getCurrentTokenType() != FaunaTokenType.END_REF) { if (parser.getCurrentTokenType() != FaunaTokenType.FIELD_NAME) { - throw new ClientException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType())); + throw new CodecException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType())); } String fieldName = parser.getValueAsString(); diff --git a/src/main/java/com/fauna/codec/codecs/BoolCodec.java b/src/main/java/com/fauna/codec/codecs/BoolCodec.java index 58bc68d2..e1aa3e1d 100644 --- a/src/main/java/com/fauna/codec/codecs/BoolCodec.java +++ b/src/main/java/com/fauna/codec/codecs/BoolCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -20,7 +20,7 @@ public Boolean decode(UTF8FaunaParser parser) throws IOException { case FALSE: return parser.getValueAsBoolean(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/ByteArrayCodec.java b/src/main/java/com/fauna/codec/codecs/ByteArrayCodec.java index 94db847e..cecf463a 100644 --- a/src/main/java/com/fauna/codec/codecs/ByteArrayCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ByteArrayCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -19,7 +19,7 @@ public byte[] decode(UTF8FaunaParser parser) throws IOException { case BYTES: return parser.getValueAsByteArray(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/ByteCodec.java b/src/main/java/com/fauna/codec/codecs/ByteCodec.java index 486c53b3..aa4d3a50 100644 --- a/src/main/java/com/fauna/codec/codecs/ByteCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ByteCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -19,7 +19,7 @@ public Byte decode(UTF8FaunaParser parser) throws IOException { case INT: return parser.getValueAsByte(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/CharCodec.java b/src/main/java/com/fauna/codec/codecs/CharCodec.java index a8adad6f..b528f3be 100644 --- a/src/main/java/com/fauna/codec/codecs/CharCodec.java +++ b/src/main/java/com/fauna/codec/codecs/CharCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -19,7 +19,7 @@ public Character decode(UTF8FaunaParser parser) throws IOException { case INT: return parser.getValueAsCharacter(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/ClassCodec.java b/src/main/java/com/fauna/codec/codecs/ClassCodec.java index a113d852..3429680b 100644 --- a/src/main/java/com/fauna/codec/codecs/ClassCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ClassCodec.java @@ -11,7 +11,7 @@ import com.fauna.codec.CodecProvider; import com.fauna.codec.FaunaTokenType; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.mapping.FieldInfo; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -119,7 +119,7 @@ public T decode(UTF8FaunaParser parser) { throw new RuntimeException(e); } default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } @@ -155,7 +155,7 @@ public void encode(UTF8FaunaGenerator gen, T obj) throws IOException { Codec codec = fi.getCodec(); codec.encode(gen, value); } catch (IllegalAccessException e) { - throw new ClientException("Error accessing field: " + fi.getName(), + throw new CodecException("Error accessing field: " + fi.getName(), e); } } @@ -190,7 +190,7 @@ private void setFields(Object instance, UTF8FaunaParser parser, while (parser.read() && parser.getCurrentTokenType() != endToken) { if (parser.getCurrentTokenType() != FaunaTokenType.FIELD_NAME) { - throw new ClientException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType())); + throw new CodecException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType())); } String fieldName = parser.getValueAsString(); diff --git a/src/main/java/com/fauna/codec/codecs/DoubleCodec.java b/src/main/java/com/fauna/codec/codecs/DoubleCodec.java index 9584f4af..2ab4b2e0 100644 --- a/src/main/java/com/fauna/codec/codecs/DoubleCodec.java +++ b/src/main/java/com/fauna/codec/codecs/DoubleCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -21,7 +21,7 @@ public Double decode(UTF8FaunaParser parser) throws IOException { case DOUBLE: return parser.getValueAsDouble(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/DynamicCodec.java b/src/main/java/com/fauna/codec/codecs/DynamicCodec.java index 03143120..e9c769a8 100644 --- a/src/main/java/com/fauna/codec/codecs/DynamicCodec.java +++ b/src/main/java/com/fauna/codec/codecs/DynamicCodec.java @@ -5,7 +5,7 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.query.StreamTokenResponse; import com.fauna.types.*; @@ -62,7 +62,7 @@ public Object decode(UTF8FaunaParser parser) throws IOException { return parser.getValueAsBoolean(); } - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } @Override diff --git a/src/main/java/com/fauna/codec/codecs/EnumCodec.java b/src/main/java/com/fauna/codec/codecs/EnumCodec.java index 027ab3f6..7d4a3721 100644 --- a/src/main/java/com/fauna/codec/codecs/EnumCodec.java +++ b/src/main/java/com/fauna/codec/codecs/EnumCodec.java @@ -3,7 +3,7 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import java.io.IOException; @@ -23,7 +23,7 @@ public T decode(UTF8FaunaParser parser) throws IOException { //noinspection unchecked return (T) Enum.valueOf((Class) enumType, parser.getValueAsString()); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/FloatCodec.java b/src/main/java/com/fauna/codec/codecs/FloatCodec.java index a8c2578d..54c2b6c1 100644 --- a/src/main/java/com/fauna/codec/codecs/FloatCodec.java +++ b/src/main/java/com/fauna/codec/codecs/FloatCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -21,7 +21,7 @@ public Float decode(UTF8FaunaParser parser) throws IOException { case DOUBLE: return parser.getValueAsFloat(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/InstantCodec.java b/src/main/java/com/fauna/codec/codecs/InstantCodec.java index 4311e808..8d2d7b89 100644 --- a/src/main/java/com/fauna/codec/codecs/InstantCodec.java +++ b/src/main/java/com/fauna/codec/codecs/InstantCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -20,7 +20,7 @@ public Instant decode(UTF8FaunaParser parser) throws IOException { case TIME: return parser.getValueAsTime(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/IntCodec.java b/src/main/java/com/fauna/codec/codecs/IntCodec.java index 86d450aa..dc3377b5 100644 --- a/src/main/java/com/fauna/codec/codecs/IntCodec.java +++ b/src/main/java/com/fauna/codec/codecs/IntCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -20,7 +20,7 @@ public Integer decode(UTF8FaunaParser parser) throws IOException { case LONG: return parser.getValueAsInt(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/ListCodec.java b/src/main/java/com/fauna/codec/codecs/ListCodec.java index 68eb3e1e..b2403dc4 100644 --- a/src/main/java/com/fauna/codec/codecs/ListCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ListCodec.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.FaunaTokenType; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -35,7 +35,7 @@ public L decode(UTF8FaunaParser parser) throws IOException { var typed = (L) list; return typed; default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/LocalDateCodec.java b/src/main/java/com/fauna/codec/codecs/LocalDateCodec.java index 09b52bd0..28274f86 100644 --- a/src/main/java/com/fauna/codec/codecs/LocalDateCodec.java +++ b/src/main/java/com/fauna/codec/codecs/LocalDateCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -20,7 +20,7 @@ public LocalDate decode(UTF8FaunaParser parser) throws IOException { case DATE: return parser.getValueAsLocalDate(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/LongCodec.java b/src/main/java/com/fauna/codec/codecs/LongCodec.java index 0fd37ef7..1445f6a7 100644 --- a/src/main/java/com/fauna/codec/codecs/LongCodec.java +++ b/src/main/java/com/fauna/codec/codecs/LongCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -20,7 +20,7 @@ public Long decode(UTF8FaunaParser parser) throws IOException { case LONG: return parser.getValueAsLong(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/MapCodec.java b/src/main/java/com/fauna/codec/codecs/MapCodec.java index 2c4daad4..a3d81060 100644 --- a/src/main/java/com/fauna/codec/codecs/MapCodec.java +++ b/src/main/java/com/fauna/codec/codecs/MapCodec.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.FaunaTokenType; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -29,7 +29,7 @@ public L decode(UTF8FaunaParser parser) throws IOException { while (parser.read() && parser.getCurrentTokenType() != FaunaTokenType.END_OBJECT) { if (parser.getCurrentTokenType() != FaunaTokenType.FIELD_NAME) { - throw new ClientException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType())); + throw new CodecException(unexpectedTokenExceptionMessage(parser.getCurrentTokenType())); } String fieldName = parser.getValueAsString(); @@ -42,7 +42,7 @@ public L decode(UTF8FaunaParser parser) throws IOException { L typed = (L) map; return typed; default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/ModuleCodec.java b/src/main/java/com/fauna/codec/codecs/ModuleCodec.java index f2d8287e..0ee12ba7 100644 --- a/src/main/java/com/fauna/codec/codecs/ModuleCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ModuleCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; import com.fauna.types.Module; @@ -20,7 +20,7 @@ public Module decode(UTF8FaunaParser parser) throws IOException { case MODULE: return parser.getValueAsModule(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/NullableCodec.java b/src/main/java/com/fauna/codec/codecs/NullableCodec.java index bca77f59..d51e8b3b 100644 --- a/src/main/java/com/fauna/codec/codecs/NullableCodec.java +++ b/src/main/java/com/fauna/codec/codecs/NullableCodec.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.FaunaTokenType; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.exception.NullDocumentException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -46,7 +46,7 @@ public void encode(UTF8FaunaGenerator gen, L obj) throws IOException { NonNull nn = (NonNull) obj; valueCodec.encode(gen, nn.get()); } else { - throw new ClientException(unsupportedTypeMessage(obj.getClass())); + throw new CodecException(unsupportedTypeMessage(obj.getClass())); } } diff --git a/src/main/java/com/fauna/codec/codecs/PageCodec.java b/src/main/java/com/fauna/codec/codecs/PageCodec.java index 4a2b1ac9..0e508a0e 100644 --- a/src/main/java/com/fauna/codec/codecs/PageCodec.java +++ b/src/main/java/com/fauna/codec/codecs/PageCodec.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.FaunaTokenType; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; import com.fauna.types.Page; @@ -51,7 +51,7 @@ public L decode(UTF8FaunaParser parser) throws IOException { // In the event the user requests a Page but the query just returns T return wrapInPage(parser); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } @@ -60,7 +60,7 @@ public void encode(UTF8FaunaGenerator gen, L obj) throws IOException { if (obj == null) { gen.writeNullValue(); } else { - throw new ClientException(this.unsupportedTypeMessage(obj.getClass())); + throw new CodecException(this.unsupportedTypeMessage(obj.getClass())); } } @@ -89,7 +89,7 @@ private L decodePage(UTF8FaunaParser parser, FaunaTokenType endToken) throws IOE } if (data == null) { - throw new ClientException("No page data found while deserializing into Page<>"); + throw new CodecException("No page data found while deserializing into Page<>"); } @SuppressWarnings("unchecked") diff --git a/src/main/java/com/fauna/codec/codecs/QueryArrCodec.java b/src/main/java/com/fauna/codec/codecs/QueryArrCodec.java index e3387e5e..02118f82 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryArrCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryArrCodec.java @@ -5,7 +5,7 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.query.builder.QueryArr; import com.fauna.query.builder.QueryFragment; @@ -22,7 +22,7 @@ public QueryArrCodec(CodecProvider provider) { @Override public QueryArr decode(UTF8FaunaParser parser) throws IOException { - throw new ClientException("Decoding into a QueryFragment is not supported"); + throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override diff --git a/src/main/java/com/fauna/codec/codecs/QueryCodec.java b/src/main/java/com/fauna/codec/codecs/QueryCodec.java index e71b3b1b..63f5a0af 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryCodec.java @@ -5,7 +5,7 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.query.builder.Query; import com.fauna.query.builder.QueryFragment; @@ -20,7 +20,7 @@ public QueryCodec(CodecProvider provider) { @Override public Query decode(UTF8FaunaParser parser) throws IOException { - throw new ClientException("Decoding into a QueryFragment is not supported"); + throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override diff --git a/src/main/java/com/fauna/codec/codecs/QueryLiteralCodec.java b/src/main/java/com/fauna/codec/codecs/QueryLiteralCodec.java index 40ce403a..cf7778f3 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryLiteralCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryLiteralCodec.java @@ -3,7 +3,7 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.query.builder.QueryLiteral; import java.io.IOException; @@ -12,7 +12,7 @@ public class QueryLiteralCodec extends BaseCodec { @Override public QueryLiteral decode(UTF8FaunaParser parser) throws IOException { - throw new ClientException("Decoding into a QueryFragment is not supported"); + throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override diff --git a/src/main/java/com/fauna/codec/codecs/QueryObjCodec.java b/src/main/java/com/fauna/codec/codecs/QueryObjCodec.java index e77f6995..f7246ae7 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryObjCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryObjCodec.java @@ -5,7 +5,7 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.query.builder.QueryFragment; import com.fauna.query.builder.QueryObj; @@ -22,7 +22,7 @@ public QueryObjCodec(CodecProvider provider) { @Override public QueryObj decode(UTF8FaunaParser parser) throws IOException { - throw new ClientException("Decoding into a QueryFragment is not supported"); + throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override diff --git a/src/main/java/com/fauna/codec/codecs/QueryValCodec.java b/src/main/java/com/fauna/codec/codecs/QueryValCodec.java index 22b9025b..326261ce 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryValCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryValCodec.java @@ -5,7 +5,7 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.query.builder.QueryArr; import com.fauna.query.builder.QueryVal; @@ -22,7 +22,7 @@ public QueryValCodec(CodecProvider provider) { @Override public QueryVal decode(UTF8FaunaParser parser) throws IOException { - throw new ClientException("Decoding into a QueryFragment is not supported"); + throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override diff --git a/src/main/java/com/fauna/codec/codecs/ShortCodec.java b/src/main/java/com/fauna/codec/codecs/ShortCodec.java index ca7b0aac..f38761c1 100644 --- a/src/main/java/com/fauna/codec/codecs/ShortCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ShortCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -19,7 +19,7 @@ public Short decode(UTF8FaunaParser parser) throws IOException { case INT: return parser.getValueAsShort(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/codec/codecs/StreamTokenResponseCodec.java b/src/main/java/com/fauna/codec/codecs/StreamTokenResponseCodec.java index b17f4cc9..09298479 100644 --- a/src/main/java/com/fauna/codec/codecs/StreamTokenResponseCodec.java +++ b/src/main/java/com/fauna/codec/codecs/StreamTokenResponseCodec.java @@ -4,7 +4,7 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; import com.fauna.codec.FaunaTokenType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.query.StreamTokenResponse; import java.io.IOException; @@ -16,13 +16,13 @@ public StreamTokenResponse decode(UTF8FaunaParser parser) throws IOException { if (parser.getCurrentTokenType() == FaunaTokenType.STREAM) { return new StreamTokenResponse(parser.getTaggedValueAsString()); } else { - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } @Override public void encode(UTF8FaunaGenerator gen, StreamTokenResponse obj) throws IOException { - throw new ClientException("Cannot encode StreamTokenResponse"); + throw new CodecException("Cannot encode StreamTokenResponse"); } diff --git a/src/main/java/com/fauna/codec/codecs/StringCodec.java b/src/main/java/com/fauna/codec/codecs/StringCodec.java index df9e4786..e937ffe8 100644 --- a/src/main/java/com/fauna/codec/codecs/StringCodec.java +++ b/src/main/java/com/fauna/codec/codecs/StringCodec.java @@ -1,7 +1,7 @@ package com.fauna.codec.codecs; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; @@ -21,7 +21,7 @@ public String decode(UTF8FaunaParser parser) throws IOException { case BYTES: return parser.getTaggedValueAsString(); default: - throw new ClientException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); + throw new CodecException(this.unsupportedTypeDecodingMessage(parser.getCurrentTokenType().getFaunaType(), getSupportedTypes())); } } diff --git a/src/main/java/com/fauna/exception/ClientRequestException.java b/src/main/java/com/fauna/exception/ClientRequestException.java new file mode 100644 index 00000000..226adfe9 --- /dev/null +++ b/src/main/java/com/fauna/exception/ClientRequestException.java @@ -0,0 +1,12 @@ +package com.fauna.exception; + +public class ClientRequestException extends ClientException { + + public ClientRequestException(String message) { + super(message); + } + + public ClientRequestException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/fauna/exception/ClientResponseException.java b/src/main/java/com/fauna/exception/ClientResponseException.java new file mode 100644 index 00000000..6768dad4 --- /dev/null +++ b/src/main/java/com/fauna/exception/ClientResponseException.java @@ -0,0 +1,22 @@ +package com.fauna.exception; + +import java.text.MessageFormat; + +public class ClientResponseException extends ClientException { + + public ClientResponseException(String message) { + super(message); + } + + private static String buildMessage(String message, int statusCode) { + return MessageFormat.format("ClientResponseException HTTP {0}: {1}", statusCode, message); + } + + public ClientResponseException(String message, Throwable exc, int statusCode) { + super(buildMessage(message, statusCode), exc); + } + + public ClientResponseException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/fauna/exception/CodecException.java b/src/main/java/com/fauna/exception/CodecException.java new file mode 100644 index 00000000..46d22a4f --- /dev/null +++ b/src/main/java/com/fauna/exception/CodecException.java @@ -0,0 +1,12 @@ +package com.fauna.exception; + +public class CodecException extends FaunaException { + + public CodecException(String message) { + super(message); + } + + public CodecException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/fauna/exception/ProtocolException.java b/src/main/java/com/fauna/exception/ProtocolException.java index e6696af2..620519c1 100644 --- a/src/main/java/com/fauna/exception/ProtocolException.java +++ b/src/main/java/com/fauna/exception/ProtocolException.java @@ -6,18 +6,18 @@ public class ProtocolException extends FaunaException { private final int statusCode; private final String body; - private static String buildMessage(int statusCode, String body) { - return MessageFormat.format("ProtocolException HTTP {0} with body: {1}", statusCode, body); + private static String buildMessage(int statusCode) { + return MessageFormat.format("ProtocolException HTTP {0}", statusCode); } public ProtocolException(Throwable exc, int statusCode, String body) { - super(buildMessage(statusCode, body), exc); + super(buildMessage(statusCode), exc); this.statusCode = statusCode; this.body = body; } public ProtocolException(int statusCode, String body) { - super(buildMessage(statusCode, body)); + super(buildMessage(statusCode)); this.statusCode = statusCode; this.body = body; } diff --git a/src/main/java/com/fauna/exception/WireParseException.java b/src/main/java/com/fauna/exception/WireParseException.java new file mode 100644 index 00000000..78e3d03a --- /dev/null +++ b/src/main/java/com/fauna/exception/WireParseException.java @@ -0,0 +1,12 @@ +package com.fauna.exception; + +public class WireParseException extends FaunaException { + + public WireParseException(String message) { + super(message); + } + + public WireParseException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/src/main/java/com/fauna/response/QueryResponse.java b/src/main/java/com/fauna/response/QueryResponse.java index 5efc96a5..e57f5782 100644 --- a/src/main/java/com/fauna/response/QueryResponse.java +++ b/src/main/java/com/fauna/response/QueryResponse.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fauna.codec.Codec; +import com.fauna.exception.ClientResponseException; import com.fauna.exception.ErrorHandler; import com.fauna.exception.FaunaException; import com.fauna.exception.ProtocolException; diff --git a/src/main/java/com/fauna/response/QuerySuccess.java b/src/main/java/com/fauna/response/QuerySuccess.java index 9541c77c..d3c9c6b3 100644 --- a/src/main/java/com/fauna/response/QuerySuccess.java +++ b/src/main/java/com/fauna/response/QuerySuccess.java @@ -2,6 +2,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.UTF8FaunaParser; +import com.fauna.exception.ClientResponseException; import com.fauna.response.wire.QueryResponseWire; import java.io.IOException; From 61eaeb0f01856abe7b7eacc499ba02218b091126 Mon Sep 17 00:00:00 2001 From: David Griffin Date: Tue, 10 Sep 2024 12:22:35 -0700 Subject: [PATCH 2/4] Refactor exception handling. --- .../java/com/fauna/client/FaunaClient.java | 2 +- .../java/com/fauna/client/RequestBuilder.java | 5 +- src/main/java/com/fauna/codec/Codec.java | 6 +- .../com/fauna/codec/UTF8FaunaGenerator.java | 208 +++++++++++------- .../java/com/fauna/codec/UTF8FaunaParser.java | 93 ++++---- .../fauna/codec/codecs/BaseDocumentCodec.java | 6 +- .../com/fauna/codec/codecs/BaseRefCodec.java | 6 +- .../com/fauna/codec/codecs/BoolCodec.java | 6 +- .../fauna/codec/codecs/ByteArrayCodec.java | 6 +- .../com/fauna/codec/codecs/ByteCodec.java | 6 +- .../com/fauna/codec/codecs/CharCodec.java | 6 +- .../com/fauna/codec/codecs/ClassCodec.java | 4 +- .../com/fauna/codec/codecs/DoubleCodec.java | 6 +- .../com/fauna/codec/codecs/DynamicCodec.java | 5 +- .../com/fauna/codec/codecs/EnumCodec.java | 6 +- .../com/fauna/codec/codecs/FloatCodec.java | 6 +- .../com/fauna/codec/codecs/InstantCodec.java | 5 +- .../java/com/fauna/codec/codecs/IntCodec.java | 6 +- .../com/fauna/codec/codecs/ListCodec.java | 5 +- .../fauna/codec/codecs/LocalDateCodec.java | 5 +- .../com/fauna/codec/codecs/LongCodec.java | 6 +- .../java/com/fauna/codec/codecs/MapCodec.java | 5 +- .../com/fauna/codec/codecs/ModuleCodec.java | 6 +- .../com/fauna/codec/codecs/NullableCodec.java | 6 +- .../com/fauna/codec/codecs/OptionalCodec.java | 6 +- .../com/fauna/codec/codecs/PageCodec.java | 8 +- .../com/fauna/codec/codecs/QueryArrCodec.java | 7 +- .../com/fauna/codec/codecs/QueryCodec.java | 6 +- .../fauna/codec/codecs/QueryLiteralCodec.java | 6 +- .../com/fauna/codec/codecs/QueryObjCodec.java | 7 +- .../com/fauna/codec/codecs/QueryValCodec.java | 7 +- .../com/fauna/codec/codecs/ShortCodec.java | 6 +- .../codecs/StreamTokenResponseCodec.java | 6 +- .../com/fauna/codec/codecs/StringCodec.java | 6 +- .../com/fauna/exception/AbortException.java | 2 +- .../com/fauna/exception/CodecException.java | 11 + .../com/fauna/exception/ErrorHandler.java | 2 +- .../java/com/fauna/response/QueryFailure.java | 15 +- .../com/fauna/response/QueryResponse.java | 5 +- .../java/com/fauna/response/QuerySuccess.java | 13 +- src/test/java/com/fauna/codec/Helpers.java | 2 +- .../fauna/codec/UTF8FaunaGeneratorTest.java | 4 +- .../com/fauna/codec/UTF8FaunaParserTest.java | 80 +++---- .../codec/codecs/BaseDocumentCodecTest.java | 11 +- .../fauna/codec/codecs/BaseRefCodecTest.java | 4 +- .../com/fauna/codec/codecs/BoolCodecTest.java | 4 +- .../codec/codecs/ByteArrayCodecTest.java | 4 +- .../com/fauna/codec/codecs/ByteCodecTest.java | 4 +- .../com/fauna/codec/codecs/CharCodecTest.java | 4 +- .../fauna/codec/codecs/ClassCodecTest.java | 4 +- .../fauna/codec/codecs/DoubleCodecTest.java | 4 +- .../com/fauna/codec/codecs/EnumCodecTest.java | 4 +- .../fauna/codec/codecs/FloatCodecTest.java | 4 +- .../fauna/codec/codecs/InstantCodecTest.java | 4 +- .../com/fauna/codec/codecs/IntCodecTest.java | 4 +- .../com/fauna/codec/codecs/ListCodecTest.java | 4 +- .../codec/codecs/LocalDateCodecTest.java | 4 +- .../com/fauna/codec/codecs/LongCodecTest.java | 4 +- .../com/fauna/codec/codecs/MapCodecTest.java | 4 +- .../fauna/codec/codecs/ModuleCodecTest.java | 4 +- .../fauna/codec/codecs/NullableCodecTest.java | 4 +- .../fauna/codec/codecs/OptionalCodecTest.java | 4 +- .../com/fauna/codec/codecs/PageCodecTest.java | 4 +- .../fauna/codec/codecs/ShortCodecTest.java | 4 +- .../fauna/codec/codecs/StringCodecTest.java | 4 +- .../com/fauna/response/QueryResponseTest.java | 15 +- 66 files changed, 386 insertions(+), 344 deletions(-) diff --git a/src/main/java/com/fauna/client/FaunaClient.java b/src/main/java/com/fauna/client/FaunaClient.java index 53f820e2..dea1b17f 100644 --- a/src/main/java/com/fauna/client/FaunaClient.java +++ b/src/main/java/com/fauna/client/FaunaClient.java @@ -88,7 +88,7 @@ public CompletableFuture> asyncQuery(Query fql) { */ public CompletableFuture> asyncQuery(Query fql, Class resultClass, QueryOptions options) { if (Objects.isNull(fql)) { - throw new ClientRequestException("The provided FQL query is null."); + throw new IllegalArgumentException("The provided FQL query is null."); } Codec codec = codecProvider.get(resultClass, null); return new RetryHandler>(getRetryStrategy()).execute(FaunaClient.makeAsyncRequest( diff --git a/src/main/java/com/fauna/client/RequestBuilder.java b/src/main/java/com/fauna/client/RequestBuilder.java index fe396722..269d246e 100644 --- a/src/main/java/com/fauna/client/RequestBuilder.java +++ b/src/main/java/com/fauna/client/RequestBuilder.java @@ -99,8 +99,7 @@ public HttpRequest buildRequest(Query fql, QueryOptions options, CodecProvider p addOptionalHeaders(builder, options); } // TODO: set last-txn-ts and max-contention-retries. - try { - UTF8FaunaGenerator gen = new UTF8FaunaGenerator(); + try (UTF8FaunaGenerator gen = UTF8FaunaGenerator.create()) { gen.writeStartObject(); gen.writeFieldName(FieldNames.QUERY); Codec codec = provider.get(Query.class); @@ -108,8 +107,6 @@ public HttpRequest buildRequest(Query fql, QueryOptions options, CodecProvider p gen.writeEndObject(); String body = gen.serialize(); return builder.POST(HttpRequest.BodyPublishers.ofString(body)).build(); - } catch (Exception e) { - throw new ClientRequestException("Unable to build Fauna Query request.", e); } } diff --git a/src/main/java/com/fauna/codec/Codec.java b/src/main/java/com/fauna/codec/Codec.java index 4f05a619..efcb2d22 100644 --- a/src/main/java/com/fauna/codec/Codec.java +++ b/src/main/java/com/fauna/codec/Codec.java @@ -1,12 +1,12 @@ package com.fauna.codec; -import java.io.IOException; +import com.fauna.exception.CodecException; public interface Codec { - T decode(UTF8FaunaParser parser) throws IOException; - void encode(UTF8FaunaGenerator gen, T obj) throws IOException; + T decode(UTF8FaunaParser parser) throws CodecException; + void encode(UTF8FaunaGenerator gen, T obj) throws CodecException; Class getCodecClass(); FaunaType[] getSupportedTypes(); diff --git a/src/main/java/com/fauna/codec/UTF8FaunaGenerator.java b/src/main/java/com/fauna/codec/UTF8FaunaGenerator.java index 73ca9623..174df565 100644 --- a/src/main/java/com/fauna/codec/UTF8FaunaGenerator.java +++ b/src/main/java/com/fauna/codec/UTF8FaunaGenerator.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; +import com.fauna.exception.CodecException; import com.fauna.types.Module; import java.io.ByteArrayOutputStream; @@ -31,16 +32,28 @@ public UTF8FaunaGenerator() throws IOException { this.jsonGenerator = factory.createGenerator(this.output); } + public static UTF8FaunaGenerator create() throws CodecException { + try { + return new UTF8FaunaGenerator(); + } catch (IOException exc) { + throw CodecException.encodingIOException(exc); + } + } + /** * Flushes the written data to the underlying buffer or stream. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void flush() throws IOException { - jsonGenerator.flush(); + public void flush() throws CodecException { + try { + jsonGenerator.flush(); + } catch (IOException exc) { + throw CodecException.encodingIOException(exc); + } } - public String serialize() throws IOException { + public String serialize() throws CodecException { this.flush(); return this.output.toString(UTF_8); @@ -51,27 +64,35 @@ public String serialize() throws IOException { /** * Writes the beginning of an object. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeStartObject() throws IOException { - jsonGenerator.writeStartObject(); + public void writeStartObject() throws CodecException { + try { + jsonGenerator.writeStartObject(); + } catch (IOException exc) { + CodecException.encodingIOException(exc); + } } /** * Writes the end of an object. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeEndObject() throws IOException { - jsonGenerator.writeEndObject(); + public void writeEndObject() throws CodecException { + try { + jsonGenerator.writeEndObject(); + } catch (IOException exc) { + CodecException.encodingIOException(exc); + } } /** * Writes the beginning of a specially tagged object. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeStartEscapedObject() throws IOException { + public void writeStartEscapedObject() throws CodecException { writeStartObject(); writeFieldName("@object"); writeStartObject(); @@ -80,9 +101,9 @@ public void writeStartEscapedObject() throws IOException { /** * Writes the end of a specially tagged object. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeEndEscapedObject() throws IOException { + public void writeEndEscapedObject() throws CodecException { writeEndObject(); writeEndObject(); } @@ -90,27 +111,35 @@ public void writeEndEscapedObject() throws IOException { /** * Writes the beginning of an array. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeStartArray() throws IOException { - jsonGenerator.writeStartArray(); + public void writeStartArray() throws CodecException { + try { + jsonGenerator.writeStartArray(); + } catch (IOException e) { + throw new RuntimeException(e); + } } /** * Writes the end of an array. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeEndArray() throws IOException { - jsonGenerator.writeEndArray(); + public void writeEndArray() throws CodecException { + try { + jsonGenerator.writeEndArray(); + } catch (IOException e) { + throw new RuntimeException(e); + } } /** * Writes the beginning of a reference object. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeStartRef() throws IOException { + public void writeStartRef() throws CodecException { writeStartObject(); writeFieldName("@ref"); writeStartObject(); @@ -119,9 +148,9 @@ public void writeStartRef() throws IOException { /** * Writes the end of a reference object. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an error occurs. */ - public void writeEndRef() throws IOException { + public void writeEndRef() throws CodecException { writeEndObject(); writeEndObject(); } @@ -131,9 +160,9 @@ public void writeEndRef() throws IOException { * * @param fieldName The name of the field. * @param value The double value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeDouble(String fieldName, double value) throws IOException { + public void writeDouble(String fieldName, double value) throws CodecException { writeFieldName(fieldName); writeDoubleValue(value); } @@ -143,9 +172,9 @@ public void writeDouble(String fieldName, double value) throws IOException { * * @param fieldName The name of the field. * @param value The integer value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeInt(String fieldName, int value) throws IOException { + public void writeInt(String fieldName, int value) throws CodecException { writeFieldName(fieldName); writeIntValue(value); } @@ -155,9 +184,9 @@ public void writeInt(String fieldName, int value) throws IOException { * * @param fieldName The name of the field. * @param value The long integer value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeLong(String fieldName, long value) throws IOException { + public void writeLong(String fieldName, long value) throws CodecException { writeFieldName(fieldName); writeLongValue(value); } @@ -167,9 +196,9 @@ public void writeLong(String fieldName, long value) throws IOException { * * @param fieldName The name of the field. * @param value The string value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeString(String fieldName, String value) throws IOException { + public void writeString(String fieldName, String value) throws CodecException { writeFieldName(fieldName); writeStringValue(value); } @@ -179,9 +208,9 @@ public void writeString(String fieldName, String value) throws IOException { * * @param fieldName The name of the field. * @param value The LocalDateTime value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeDate(String fieldName, LocalDate value) throws IOException { + public void writeDate(String fieldName, LocalDate value) throws CodecException { writeFieldName(fieldName); writeDateValue(value); } @@ -191,9 +220,9 @@ public void writeDate(String fieldName, LocalDate value) throws IOException { * * @param fieldName The name of the field. * @param value The LocalDateTime value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeTime(String fieldName, Instant value) throws IOException { + public void writeTime(String fieldName, Instant value) throws CodecException { writeFieldName(fieldName); writeTimeValue(value); } @@ -203,9 +232,9 @@ public void writeTime(String fieldName, Instant value) throws IOException { * * @param fieldName The name of the field. * @param value The boolean value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeBoolean(String fieldName, boolean value) throws IOException { + public void writeBoolean(String fieldName, boolean value) throws CodecException { writeFieldName(fieldName); writeBooleanValue(value); } @@ -214,9 +243,9 @@ public void writeBoolean(String fieldName, boolean value) throws IOException { * Writes a null value with a specific field name. * * @param fieldName The name of the field. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeNull(String fieldName) throws IOException { + public void writeNull(String fieldName) throws CodecException { writeFieldName(fieldName); writeNullValue(); } @@ -226,9 +255,9 @@ public void writeNull(String fieldName) throws IOException { * * @param fieldName The name of the field. * @param value The Module value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeModule(String fieldName, Module value) throws IOException { + public void writeModule(String fieldName, Module value) throws CodecException { writeFieldName(fieldName); writeModuleValue(value); } @@ -237,10 +266,14 @@ public void writeModule(String fieldName, Module value) throws IOException { * Writes a field name for the next value. * * @param value The name of the field. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeFieldName(String value) throws IOException { - jsonGenerator.writeFieldName(value); + public void writeFieldName(String value) throws CodecException { + try { + jsonGenerator.writeFieldName(value); + } catch (IOException e) { + throw new RuntimeException(e); + } } /** @@ -248,24 +281,24 @@ public void writeFieldName(String value) throws IOException { * * @param tag The tag to use for the value. * @param value The value associated with the tag. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeTaggedValue(String tag, String value) throws IOException { + public void writeTaggedValue(String tag, String value) throws CodecException { writeStartObject(); writeString(tag, value); writeEndObject(); } - public void writeByteArray(byte[] bytes) throws IOException { + public void writeByteArray(byte[] bytes) throws CodecException { writeTaggedValue("@bytes", Base64.getEncoder().encodeToString(bytes)); } /** * Writes a double value as a tagged element. * * @param value The double value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeDoubleValue(double value) throws IOException { + public void writeDoubleValue(double value) throws CodecException { writeTaggedValue("@double", Double.toString(value)); } @@ -273,9 +306,9 @@ public void writeDoubleValue(double value) throws IOException { * Writes a float value as a tagged element (@double). * * @param value The float value to write as a double. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeDoubleValue(float value) throws IOException { + public void writeDoubleValue(float value) throws CodecException { writeTaggedValue("@double", Float.toString(value)); } @@ -284,9 +317,9 @@ public void writeDoubleValue(float value) throws IOException { * Writes an integer value as a tagged element. * * @param value The integer value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeIntValue(int value) throws IOException { + public void writeIntValue(int value) throws CodecException { writeTaggedValue("@int", Integer.toString(value)); } @@ -294,9 +327,9 @@ public void writeIntValue(int value) throws IOException { * Writes a long integer value as a tagged element. * * @param value The long integer value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeLongValue(long value) throws IOException { + public void writeLongValue(long value) throws CodecException { writeTaggedValue("@long", Long.toString(value)); } @@ -304,19 +337,23 @@ public void writeLongValue(long value) throws IOException { * Writes a string value as a tagged element. * * @param value The string value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeStringValue(String value) throws IOException { - jsonGenerator.writeString(value); + public void writeStringValue(String value) throws CodecException { + try { + jsonGenerator.writeString(value); + } catch (IOException exc) { + throw CodecException.encodingIOException(exc); + } } /** * Writes a date value as a tagged element. * * @param value The date value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeDateValue(LocalDate value) throws IOException { + public void writeDateValue(LocalDate value) throws CodecException { String str = value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); writeTaggedValue("@date", str); } @@ -325,9 +362,9 @@ public void writeDateValue(LocalDate value) throws IOException { * Writes a time value as a tagged element. * * @param value The time value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeTimeValue(Instant value) throws IOException { + public void writeTimeValue(Instant value) throws CodecException { Instant instant = value.atZone(ZoneOffset.UTC).toInstant(); String formattedTime = instant.toString(); writeTaggedValue("@time", formattedTime); @@ -337,32 +374,40 @@ public void writeTimeValue(Instant value) throws IOException { * Writes a boolean value to the stream. * * @param value The boolean value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeBooleanValue(boolean value) throws IOException { - jsonGenerator.writeBoolean(value); + public void writeBooleanValue(boolean value) throws CodecException { + try { + jsonGenerator.writeBoolean(value); + } catch (IOException exc) { + throw CodecException.encodingIOException(exc); + } } - public void writeCharValue(Character value) throws IOException { + public void writeCharValue(Character value) throws CodecException { writeIntValue(value); } /** * Writes a null value to the stream. * - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeNullValue() throws IOException { - jsonGenerator.writeNull(); + public void writeNullValue() throws CodecException { + try { + jsonGenerator.writeNull(); + } catch (IOException exc) { + throw CodecException.encodingIOException(exc); + } } /** * Writes a module value as a tagged element. * * @param value The module value to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeModuleValue(Module value) throws IOException { + public void writeModuleValue(Module value) throws CodecException { writeTaggedValue("@mod", value.getName()); } @@ -370,15 +415,24 @@ public void writeModuleValue(Module value) throws IOException { * Writes a byte array encoded as a base64 string as a tagged element. * * @param value The byte array to write. - * @throws IOException If an I/O error occurs. + * @throws CodecException If an I/O error occurs. */ - public void writeBytesValue(byte[] value) throws IOException { + public void writeBytesValue(byte[] value) throws CodecException { writeTaggedValue("@bytes", Base64.getEncoder().encodeToString(value)); } @Override - public void close() throws IOException { - jsonGenerator.close(); - output.close(); + public void close() throws CodecException { + try { + jsonGenerator.close(); + } catch (IOException exc) { + throw CodecException.encodingIOException(exc); + } finally { + try { + output.close(); + } catch (IOException e) { + throw CodecException.encodingIOException(e); + } + } } } diff --git a/src/main/java/com/fauna/codec/UTF8FaunaParser.java b/src/main/java/com/fauna/codec/UTF8FaunaParser.java index 38c00468..4267b6be 100644 --- a/src/main/java/com/fauna/codec/UTF8FaunaParser.java +++ b/src/main/java/com/fauna/codec/UTF8FaunaParser.java @@ -3,7 +3,8 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; -import com.fauna.exception.WireParseException; +import com.fauna.exception.CodecException; +import com.fauna.exception.CodecException; import com.fauna.types.Module; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -45,29 +46,31 @@ public class UTF8FaunaParser { private FaunaTokenType bufferedFaunaTokenType; private String taggedTokenValue; - public UTF8FaunaParser(String str) throws IOException { - InputStream inputStream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)); - JsonFactory factory = new JsonFactory(); - this.jsonParser = factory.createParser(inputStream); - if (getCurrentTokenType() == FaunaTokenType.NONE) { - read(); - } - } private enum InternalTokenType { START_ESCAPED_OBJECT } - public UTF8FaunaParser(InputStream body) throws IOException { + public UTF8FaunaParser(JsonParser jsonParser) { + this.jsonParser = jsonParser; + } + + public static UTF8FaunaParser fromInputStream(InputStream body) throws CodecException { JsonFactory factory = new JsonFactory(); - this.jsonParser = factory.createParser(body); - if (getCurrentTokenType() == FaunaTokenType.NONE) { - read(); + try { + JsonParser jsonParser = factory.createParser(body); + UTF8FaunaParser faunaParser = new UTF8FaunaParser(jsonParser); + if (faunaParser.getCurrentTokenType() == FaunaTokenType.NONE) { + faunaParser.read(); + } + return faunaParser; + } catch (IOException exc) { + throw CodecException.decodingIOException(exc); } } - public UTF8FaunaParser(JsonParser jsonParser) { - this.jsonParser = jsonParser; + public static UTF8FaunaParser fromString(String str) { + return UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))); } public FaunaTokenType getCurrentTokenType() { @@ -103,7 +106,7 @@ private void skipInternal() throws IOException { } } - public boolean read() throws IOException { + public boolean read() throws CodecException { taggedTokenValue = null; if (bufferedFaunaTokenType != null) { @@ -152,7 +155,7 @@ public boolean read() throws IOException { currentFaunaTokenType = FaunaTokenType.NULL; break; default: - throw new WireParseException( + throw new CodecException( "Unhandled JSON token type " + currentToken + "."); } } else { @@ -162,12 +165,12 @@ public boolean read() throws IOException { return true; } - private void handleStartObject() throws IOException { + private void handleStartObject() throws CodecException { advanceTrue(); switch (jsonParser.currentToken()) { case FIELD_NAME: - switch (jsonParser.getText()) { + switch (getText()) { case BYTES_TAG: handleTaggedString(FaunaTokenType.BYTES); break; @@ -225,7 +228,7 @@ private void handleStartObject() throws IOException { currentFaunaTokenType = FaunaTokenType.START_OBJECT; break; default: - throw new WireParseException( + throw new CodecException( "Unexpected token following StartObject: " + jsonParser.currentToken()); } } @@ -247,21 +250,33 @@ private void handleEndObject() { } else if (startToken.equals(FaunaTokenType.START_OBJECT)) { currentFaunaTokenType = END_OBJECT; } else { - throw new WireParseException( + throw new CodecException( "Unexpected token " + startToken + ". This might be a bug."); } } - private void handleTaggedString(FaunaTokenType token) throws IOException { - advanceTrue(); - currentFaunaTokenType = token; - taggedTokenValue = jsonParser.getText(); - advance(); + private void handleTaggedString(FaunaTokenType token) throws CodecException { + try { + advanceTrue(); + currentFaunaTokenType = token; + taggedTokenValue = jsonParser.getText(); + advance(); + } catch (IOException exc) { + throw CodecException.decodingIOException(exc); + } + } + + private String getText() throws CodecException { + try { + return jsonParser.getText(); + } catch (IOException exc) { + throw CodecException.decodingIOException(exc); + } } private void advanceTrue() { if (!advance()) { - throw new WireParseException("Unexpected end of underlying JSON reader."); + throw new CodecException("Unexpected end of underlying JSON reader."); } } @@ -269,7 +284,7 @@ private boolean advance() { try { return Objects.nonNull(jsonParser.nextToken()); } catch (IOException e) { - throw new WireParseException("Failed to advance underlying JSON reader.", e); + throw new CodecException("Failed to advance underlying JSON reader.", e); } } @@ -297,7 +312,7 @@ public String getValueAsString() { try { return jsonParser.getValueAsString(); } catch (IOException e) { - throw new WireParseException("Error getting the current token as String", e); + throw new CodecException("Error getting the current token as String", e); } } @@ -315,7 +330,7 @@ public Byte getValueAsByte() { try { return Byte.parseByte(taggedTokenValue); } catch (NumberFormatException e) { - throw new WireParseException("Error getting the current token as Byte", e); + throw new CodecException("Error getting the current token as Byte", e); } } @@ -324,7 +339,7 @@ public Short getValueAsShort() { try { return Short.parseShort(taggedTokenValue); } catch (NumberFormatException e) { - throw new WireParseException("Error getting the current token as Short", e); + throw new CodecException("Error getting the current token as Short", e); } } @@ -334,7 +349,7 @@ public Integer getValueAsInt() { try { return Integer.parseInt(taggedTokenValue); } catch (NumberFormatException e) { - throw new WireParseException("Error getting the current token as Integer", e); + throw new CodecException("Error getting the current token as Integer", e); } } @@ -342,7 +357,7 @@ public Boolean getValueAsBoolean() { try { return jsonParser.getValueAsBoolean(); } catch (IOException e) { - throw new WireParseException("Error getting the current token as Boolean", e); + throw new CodecException("Error getting the current token as Boolean", e); } } @@ -351,7 +366,7 @@ public LocalDate getValueAsLocalDate() { try { return LocalDate.parse(taggedTokenValue); } catch (DateTimeParseException e) { - throw new WireParseException("Error getting the current token as LocalDate", e); + throw new CodecException("Error getting the current token as LocalDate", e); } } @@ -360,7 +375,7 @@ public Instant getValueAsTime() { try { return Instant.parse(taggedTokenValue); } catch (DateTimeParseException e) { - throw new WireParseException("Error getting the current token as LocalDateTime", e); + throw new CodecException("Error getting the current token as LocalDateTime", e); } } @@ -369,7 +384,7 @@ public Float getValueAsFloat() { try { return Float.parseFloat(taggedTokenValue); } catch (NumberFormatException e) { - throw new WireParseException("Error getting the current token as Float", e); + throw new CodecException("Error getting the current token as Float", e); } } @@ -378,7 +393,7 @@ public Double getValueAsDouble() { try { return Double.parseDouble(taggedTokenValue); } catch (NumberFormatException e) { - throw new WireParseException("Error getting the current token as Double", e); + throw new CodecException("Error getting the current token as Double", e); } } @@ -387,7 +402,7 @@ public Long getValueAsLong() { try { return Long.parseLong(taggedTokenValue); } catch (NumberFormatException e) { - throw new WireParseException("Error getting the current token as Long", e); + throw new CodecException("Error getting the current token as Long", e); } } @@ -395,7 +410,7 @@ public Module getValueAsModule() { try { return new Module(taggedTokenValue); } catch (Exception e) { - throw new WireParseException("Error getting the current token as Module", e); + throw new CodecException("Error getting the current token as Module", e); } } } \ No newline at end of file diff --git a/src/main/java/com/fauna/codec/codecs/BaseDocumentCodec.java b/src/main/java/com/fauna/codec/codecs/BaseDocumentCodec.java index 46719f27..f878b942 100644 --- a/src/main/java/com/fauna/codec/codecs/BaseDocumentCodec.java +++ b/src/main/java/com/fauna/codec/codecs/BaseDocumentCodec.java @@ -21,7 +21,7 @@ public BaseDocumentCodec(CodecProvider provider) { } @Override - public BaseDocument decode(UTF8FaunaParser parser) throws IOException { + public BaseDocument decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -36,7 +36,7 @@ public BaseDocument decode(UTF8FaunaParser parser) throws IOException { } } - private Object decodeInternal(UTF8FaunaParser parser) throws IOException { + private Object decodeInternal(UTF8FaunaParser parser) throws CodecException { var builder = new InternalDocument.Builder(); var valueCodec = provider.get(Object.class); @@ -67,7 +67,7 @@ private Object decodeInternal(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, BaseDocument obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, BaseDocument obj) throws CodecException { gen.writeStartRef(); if (obj instanceof Document) { diff --git a/src/main/java/com/fauna/codec/codecs/BaseRefCodec.java b/src/main/java/com/fauna/codec/codecs/BaseRefCodec.java index 25641204..b868b240 100644 --- a/src/main/java/com/fauna/codec/codecs/BaseRefCodec.java +++ b/src/main/java/com/fauna/codec/codecs/BaseRefCodec.java @@ -14,7 +14,7 @@ public class BaseRefCodec extends BaseCodec { public static final BaseRefCodec SINGLETON = new BaseRefCodec(); @Override - public BaseRef decode(UTF8FaunaParser parser) throws IOException { + public BaseRef decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -25,7 +25,7 @@ public BaseRef decode(UTF8FaunaParser parser) throws IOException { } } - private Object decodeInternal(UTF8FaunaParser parser) throws IOException { + private Object decodeInternal(UTF8FaunaParser parser) throws CodecException { var builder = new InternalDocument.Builder(); while (parser.read() && parser.getCurrentTokenType() != FaunaTokenType.END_REF) { @@ -50,7 +50,7 @@ private Object decodeInternal(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, BaseRef obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, BaseRef obj) throws CodecException { gen.writeStartRef(); if (obj instanceof DocumentRef) { diff --git a/src/main/java/com/fauna/codec/codecs/BoolCodec.java b/src/main/java/com/fauna/codec/codecs/BoolCodec.java index e1aa3e1d..4be548f9 100644 --- a/src/main/java/com/fauna/codec/codecs/BoolCodec.java +++ b/src/main/java/com/fauna/codec/codecs/BoolCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class BoolCodec extends BaseCodec { public static final BoolCodec singleton = new BoolCodec(); @Override - public Boolean decode(UTF8FaunaParser parser) throws IOException { + public Boolean decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -25,7 +23,7 @@ public Boolean decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Boolean obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Boolean obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/ByteArrayCodec.java b/src/main/java/com/fauna/codec/codecs/ByteArrayCodec.java index cecf463a..2a600c11 100644 --- a/src/main/java/com/fauna/codec/codecs/ByteArrayCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ByteArrayCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class ByteArrayCodec extends BaseCodec { public static final ByteArrayCodec singleton = new ByteArrayCodec(); @Override - public byte[] decode(UTF8FaunaParser parser) throws IOException { + public byte[] decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -24,7 +22,7 @@ public byte[] decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, byte[] obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, byte[] obj) throws CodecException { if (obj == null) { gen.writeNullValue(); return; diff --git a/src/main/java/com/fauna/codec/codecs/ByteCodec.java b/src/main/java/com/fauna/codec/codecs/ByteCodec.java index aa4d3a50..9497053e 100644 --- a/src/main/java/com/fauna/codec/codecs/ByteCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ByteCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class ByteCodec extends BaseCodec { public static final ByteCodec singleton = new ByteCodec(); @Override - public Byte decode(UTF8FaunaParser parser) throws IOException { + public Byte decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -24,7 +22,7 @@ public Byte decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Byte obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Byte obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/CharCodec.java b/src/main/java/com/fauna/codec/codecs/CharCodec.java index b528f3be..41f34e14 100644 --- a/src/main/java/com/fauna/codec/codecs/CharCodec.java +++ b/src/main/java/com/fauna/codec/codecs/CharCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class CharCodec extends BaseCodec { public static final CharCodec singleton = new CharCodec(); @Override - public Character decode(UTF8FaunaParser parser) throws IOException { + public Character decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -24,7 +22,7 @@ public Character decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Character obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Character obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/ClassCodec.java b/src/main/java/com/fauna/codec/codecs/ClassCodec.java index 3429680b..437c5cad 100644 --- a/src/main/java/com/fauna/codec/codecs/ClassCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ClassCodec.java @@ -100,7 +100,7 @@ private FieldType getFieldType(Field field) { } @Override - public T decode(UTF8FaunaParser parser) { + public T decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -124,7 +124,7 @@ public T decode(UTF8FaunaParser parser) { } @Override - public void encode(UTF8FaunaGenerator gen, T obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, T obj) throws CodecException { if (shouldEscapeObject) { gen.writeStartEscapedObject(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/DoubleCodec.java b/src/main/java/com/fauna/codec/codecs/DoubleCodec.java index 2ab4b2e0..b8b68168 100644 --- a/src/main/java/com/fauna/codec/codecs/DoubleCodec.java +++ b/src/main/java/com/fauna/codec/codecs/DoubleCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class DoubleCodec extends BaseCodec { public static final DoubleCodec singleton = new DoubleCodec(); @Override - public Double decode(UTF8FaunaParser parser) throws IOException { + public Double decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -26,7 +24,7 @@ public Double decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Double obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Double obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/DynamicCodec.java b/src/main/java/com/fauna/codec/codecs/DynamicCodec.java index e9c769a8..0733c51d 100644 --- a/src/main/java/com/fauna/codec/codecs/DynamicCodec.java +++ b/src/main/java/com/fauna/codec/codecs/DynamicCodec.java @@ -9,7 +9,6 @@ import com.fauna.query.StreamTokenResponse; import com.fauna.types.*; -import java.io.IOException; import java.util.List; import java.util.Map; @@ -25,7 +24,7 @@ public DynamicCodec(CodecProvider provider) { } @Override - public Object decode(UTF8FaunaParser parser) throws IOException { + public Object decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -67,7 +66,7 @@ public Object decode(UTF8FaunaParser parser) throws IOException { @Override @SuppressWarnings("unchecked") - public void encode(UTF8FaunaGenerator gen, Object obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Object obj) throws CodecException { // TODO: deal with Object.class loop @SuppressWarnings("rawtypes") diff --git a/src/main/java/com/fauna/codec/codecs/EnumCodec.java b/src/main/java/com/fauna/codec/codecs/EnumCodec.java index 7d4a3721..7bf826f1 100644 --- a/src/main/java/com/fauna/codec/codecs/EnumCodec.java +++ b/src/main/java/com/fauna/codec/codecs/EnumCodec.java @@ -5,8 +5,6 @@ import com.fauna.codec.UTF8FaunaParser; import com.fauna.exception.CodecException; -import java.io.IOException; - public class EnumCodec extends BaseCodec { private final Class enumType; @@ -15,7 +13,7 @@ public EnumCodec(Class enumType) { } @Override - public T decode(UTF8FaunaParser parser) throws IOException { + public T decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -28,7 +26,7 @@ public T decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, T obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, T obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/FloatCodec.java b/src/main/java/com/fauna/codec/codecs/FloatCodec.java index 54c2b6c1..ac910619 100644 --- a/src/main/java/com/fauna/codec/codecs/FloatCodec.java +++ b/src/main/java/com/fauna/codec/codecs/FloatCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class FloatCodec extends BaseCodec { public static final FloatCodec singleton = new FloatCodec(); @Override - public Float decode(UTF8FaunaParser parser) throws IOException { + public Float decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -26,7 +24,7 @@ public Float decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Float obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Float obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/InstantCodec.java b/src/main/java/com/fauna/codec/codecs/InstantCodec.java index 8d2d7b89..e9c24a3e 100644 --- a/src/main/java/com/fauna/codec/codecs/InstantCodec.java +++ b/src/main/java/com/fauna/codec/codecs/InstantCodec.java @@ -5,7 +5,6 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; import java.time.Instant; public class InstantCodec extends BaseCodec { @@ -13,7 +12,7 @@ public class InstantCodec extends BaseCodec { public static final InstantCodec SINGLETON = new InstantCodec(); @Override - public Instant decode(UTF8FaunaParser parser) throws IOException { + public Instant decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -25,7 +24,7 @@ public Instant decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Instant obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Instant obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/IntCodec.java b/src/main/java/com/fauna/codec/codecs/IntCodec.java index dc3377b5..4e8a3368 100644 --- a/src/main/java/com/fauna/codec/codecs/IntCodec.java +++ b/src/main/java/com/fauna/codec/codecs/IntCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class IntCodec extends BaseCodec { public static final IntCodec singleton = new IntCodec(); @Override - public Integer decode(UTF8FaunaParser parser) throws IOException { + public Integer decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -25,7 +23,7 @@ public Integer decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Integer obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Integer obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/ListCodec.java b/src/main/java/com/fauna/codec/codecs/ListCodec.java index b2403dc4..719ba476 100644 --- a/src/main/java/com/fauna/codec/codecs/ListCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ListCodec.java @@ -7,7 +7,6 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -20,7 +19,7 @@ public ListCodec(Codec elementCodec) { } @Override - public L decode(UTF8FaunaParser parser) throws IOException { + public L decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -40,7 +39,7 @@ public L decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, L obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, L obj) throws CodecException { if (obj == null) { gen.writeNullValue(); return; diff --git a/src/main/java/com/fauna/codec/codecs/LocalDateCodec.java b/src/main/java/com/fauna/codec/codecs/LocalDateCodec.java index 28274f86..e500107a 100644 --- a/src/main/java/com/fauna/codec/codecs/LocalDateCodec.java +++ b/src/main/java/com/fauna/codec/codecs/LocalDateCodec.java @@ -5,7 +5,6 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; import java.time.LocalDate; public class LocalDateCodec extends BaseCodec { @@ -13,7 +12,7 @@ public class LocalDateCodec extends BaseCodec { public static final LocalDateCodec SINGLETON = new LocalDateCodec(); @Override - public LocalDate decode(UTF8FaunaParser parser) throws IOException { + public LocalDate decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -25,7 +24,7 @@ public LocalDate decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, LocalDate obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, LocalDate obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/LongCodec.java b/src/main/java/com/fauna/codec/codecs/LongCodec.java index 1445f6a7..57d347b1 100644 --- a/src/main/java/com/fauna/codec/codecs/LongCodec.java +++ b/src/main/java/com/fauna/codec/codecs/LongCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class LongCodec extends BaseCodec { public static final LongCodec singleton = new LongCodec(); @Override - public Long decode(UTF8FaunaParser parser) throws IOException { + public Long decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -25,7 +23,7 @@ public Long decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Long obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Long obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/MapCodec.java b/src/main/java/com/fauna/codec/codecs/MapCodec.java index a3d81060..4cf913a3 100644 --- a/src/main/java/com/fauna/codec/codecs/MapCodec.java +++ b/src/main/java/com/fauna/codec/codecs/MapCodec.java @@ -7,7 +7,6 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -20,7 +19,7 @@ public MapCodec(Codec valueCodec) { } @Override - public L decode(UTF8FaunaParser parser) throws IOException { + public L decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -47,7 +46,7 @@ public L decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, L obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, L obj) throws CodecException { if (obj == null) { gen.writeNullValue(); return; diff --git a/src/main/java/com/fauna/codec/codecs/ModuleCodec.java b/src/main/java/com/fauna/codec/codecs/ModuleCodec.java index 0ee12ba7..86262728 100644 --- a/src/main/java/com/fauna/codec/codecs/ModuleCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ModuleCodec.java @@ -6,14 +6,12 @@ import com.fauna.codec.UTF8FaunaParser; import com.fauna.types.Module; -import java.io.IOException; - public class ModuleCodec extends BaseCodec { public static final ModuleCodec SINGLETON = new ModuleCodec(); @Override - public Module decode(UTF8FaunaParser parser) throws IOException { + public Module decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -25,7 +23,7 @@ public Module decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Module obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Module obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/NullableCodec.java b/src/main/java/com/fauna/codec/codecs/NullableCodec.java index d51e8b3b..e91485ba 100644 --- a/src/main/java/com/fauna/codec/codecs/NullableCodec.java +++ b/src/main/java/com/fauna/codec/codecs/NullableCodec.java @@ -11,8 +11,6 @@ import com.fauna.types.NullDoc; import com.fauna.types.Nullable; -import java.io.IOException; - public class NullableCodec> extends BaseCodec { private final Codec valueCodec; @@ -23,7 +21,7 @@ public NullableCodec(Codec valueCodec) { @Override @SuppressWarnings("unchecked") - public L decode(UTF8FaunaParser parser) throws IOException { + public L decode(UTF8FaunaParser parser) throws CodecException { if (parser.getCurrentTokenType() == FaunaTokenType.NULL) { return null; } @@ -40,7 +38,7 @@ public L decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, L obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, L obj) throws CodecException { if (obj instanceof NonNull) { @SuppressWarnings("unchecked") NonNull nn = (NonNull) obj; diff --git a/src/main/java/com/fauna/codec/codecs/OptionalCodec.java b/src/main/java/com/fauna/codec/codecs/OptionalCodec.java index 42517ad6..4a2c9b3e 100644 --- a/src/main/java/com/fauna/codec/codecs/OptionalCodec.java +++ b/src/main/java/com/fauna/codec/codecs/OptionalCodec.java @@ -5,8 +5,8 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; +import com.fauna.exception.CodecException; -import java.io.IOException; import java.util.Optional; public class OptionalCodec> extends BaseCodec { @@ -18,7 +18,7 @@ public OptionalCodec(Codec valueCodec) { } @Override - public L decode(UTF8FaunaParser parser) throws IOException { + public L decode(UTF8FaunaParser parser) throws CodecException { if (parser.getCurrentTokenType() == FaunaTokenType.NULL) { @SuppressWarnings("unchecked") L res = (L) Optional.empty(); @@ -31,7 +31,7 @@ public L decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, L obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, L obj) throws CodecException { if (obj == null || obj.isEmpty()) { gen.writeNullValue(); return; diff --git a/src/main/java/com/fauna/codec/codecs/PageCodec.java b/src/main/java/com/fauna/codec/codecs/PageCodec.java index 0e508a0e..818e3f83 100644 --- a/src/main/java/com/fauna/codec/codecs/PageCodec.java +++ b/src/main/java/com/fauna/codec/codecs/PageCodec.java @@ -22,7 +22,7 @@ public PageCodec(Codec elementCodec) { } @Override - public L decode(UTF8FaunaParser parser) throws IOException { + public L decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -56,7 +56,7 @@ public L decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, L obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, L obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { @@ -70,7 +70,7 @@ public Class getCodecClass() { } - private L decodePage(UTF8FaunaParser parser, FaunaTokenType endToken) throws IOException { + private L decodePage(UTF8FaunaParser parser, FaunaTokenType endToken) throws CodecException { List data = null; String after = null; @@ -97,7 +97,7 @@ private L decodePage(UTF8FaunaParser parser, FaunaTokenType endToken) throws IOE return res; } - private L wrapInPage(UTF8FaunaParser parser) throws IOException { + private L wrapInPage(UTF8FaunaParser parser) throws CodecException { E elem = this.elementCodec.decode(parser); @SuppressWarnings("unchecked") L res = (L) new Page<>(List.of(elem), null); diff --git a/src/main/java/com/fauna/codec/codecs/QueryArrCodec.java b/src/main/java/com/fauna/codec/codecs/QueryArrCodec.java index 02118f82..4e3280f4 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryArrCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryArrCodec.java @@ -7,9 +7,6 @@ import com.fauna.codec.UTF8FaunaParser; import com.fauna.exception.CodecException; import com.fauna.query.builder.QueryArr; -import com.fauna.query.builder.QueryFragment; - -import java.io.IOException; public class QueryArrCodec extends BaseCodec { @@ -21,12 +18,12 @@ public QueryArrCodec(CodecProvider provider) { } @Override - public QueryArr decode(UTF8FaunaParser parser) throws IOException { + public QueryArr decode(UTF8FaunaParser parser) throws CodecException { throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override - public void encode(UTF8FaunaGenerator gen, QueryArr obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, QueryArr obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/QueryCodec.java b/src/main/java/com/fauna/codec/codecs/QueryCodec.java index 63f5a0af..d9bd43c4 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryCodec.java @@ -9,8 +9,6 @@ import com.fauna.query.builder.Query; import com.fauna.query.builder.QueryFragment; -import java.io.IOException; - public class QueryCodec extends BaseCodec { private final CodecProvider provider; @@ -19,12 +17,12 @@ public QueryCodec(CodecProvider provider) { } @Override - public Query decode(UTF8FaunaParser parser) throws IOException { + public Query decode(UTF8FaunaParser parser) throws CodecException { throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override - public void encode(UTF8FaunaGenerator gen, Query obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Query obj) throws CodecException { gen.writeStartObject(); gen.writeFieldName("fql"); gen.writeStartArray(); diff --git a/src/main/java/com/fauna/codec/codecs/QueryLiteralCodec.java b/src/main/java/com/fauna/codec/codecs/QueryLiteralCodec.java index cf7778f3..0ffe67b7 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryLiteralCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryLiteralCodec.java @@ -6,17 +6,15 @@ import com.fauna.exception.CodecException; import com.fauna.query.builder.QueryLiteral; -import java.io.IOException; - public class QueryLiteralCodec extends BaseCodec { @Override - public QueryLiteral decode(UTF8FaunaParser parser) throws IOException { + public QueryLiteral decode(UTF8FaunaParser parser) throws CodecException { throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override - public void encode(UTF8FaunaGenerator gen, QueryLiteral obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, QueryLiteral obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/QueryObjCodec.java b/src/main/java/com/fauna/codec/codecs/QueryObjCodec.java index f7246ae7..89e9f9da 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryObjCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryObjCodec.java @@ -6,11 +6,8 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; import com.fauna.exception.CodecException; -import com.fauna.query.builder.QueryFragment; import com.fauna.query.builder.QueryObj; -import java.io.IOException; - public class QueryObjCodec extends BaseCodec { private final CodecProvider provider; @@ -21,12 +18,12 @@ public QueryObjCodec(CodecProvider provider) { } @Override - public QueryObj decode(UTF8FaunaParser parser) throws IOException { + public QueryObj decode(UTF8FaunaParser parser) throws CodecException { throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override - public void encode(UTF8FaunaGenerator gen, QueryObj obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, QueryObj obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/QueryValCodec.java b/src/main/java/com/fauna/codec/codecs/QueryValCodec.java index 326261ce..9ae241c7 100644 --- a/src/main/java/com/fauna/codec/codecs/QueryValCodec.java +++ b/src/main/java/com/fauna/codec/codecs/QueryValCodec.java @@ -6,11 +6,8 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; import com.fauna.exception.CodecException; -import com.fauna.query.builder.QueryArr; import com.fauna.query.builder.QueryVal; -import java.io.IOException; - public class QueryValCodec extends BaseCodec { private final CodecProvider provider; @@ -21,12 +18,12 @@ public QueryValCodec(CodecProvider provider) { } @Override - public QueryVal decode(UTF8FaunaParser parser) throws IOException { + public QueryVal decode(UTF8FaunaParser parser) throws CodecException { throw new CodecException("Decoding into a QueryFragment is not supported"); } @Override - public void encode(UTF8FaunaGenerator gen, QueryVal obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, QueryVal obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/ShortCodec.java b/src/main/java/com/fauna/codec/codecs/ShortCodec.java index f38761c1..f9052b18 100644 --- a/src/main/java/com/fauna/codec/codecs/ShortCodec.java +++ b/src/main/java/com/fauna/codec/codecs/ShortCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class ShortCodec extends BaseCodec { public static final ShortCodec singleton = new ShortCodec(); @Override - public Short decode(UTF8FaunaParser parser) throws IOException { + public Short decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -24,7 +22,7 @@ public Short decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, Short obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, Short obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/codec/codecs/StreamTokenResponseCodec.java b/src/main/java/com/fauna/codec/codecs/StreamTokenResponseCodec.java index 09298479..5ac0425f 100644 --- a/src/main/java/com/fauna/codec/codecs/StreamTokenResponseCodec.java +++ b/src/main/java/com/fauna/codec/codecs/StreamTokenResponseCodec.java @@ -7,12 +7,10 @@ import com.fauna.exception.CodecException; import com.fauna.query.StreamTokenResponse; -import java.io.IOException; - public class StreamTokenResponseCodec extends BaseCodec { @Override - public StreamTokenResponse decode(UTF8FaunaParser parser) throws IOException { + public StreamTokenResponse decode(UTF8FaunaParser parser) throws CodecException { if (parser.getCurrentTokenType() == FaunaTokenType.STREAM) { return new StreamTokenResponse(parser.getTaggedValueAsString()); } else { @@ -21,7 +19,7 @@ public StreamTokenResponse decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, StreamTokenResponse obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, StreamTokenResponse obj) throws CodecException { throw new CodecException("Cannot encode StreamTokenResponse"); } diff --git a/src/main/java/com/fauna/codec/codecs/StringCodec.java b/src/main/java/com/fauna/codec/codecs/StringCodec.java index e937ffe8..91704bf7 100644 --- a/src/main/java/com/fauna/codec/codecs/StringCodec.java +++ b/src/main/java/com/fauna/codec/codecs/StringCodec.java @@ -5,14 +5,12 @@ import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import java.io.IOException; - public class StringCodec extends BaseCodec { public static final StringCodec singleton = new StringCodec(); @Override - public String decode(UTF8FaunaParser parser) throws IOException { + public String decode(UTF8FaunaParser parser) throws CodecException { switch (parser.getCurrentTokenType()) { case NULL: return null; @@ -26,7 +24,7 @@ public String decode(UTF8FaunaParser parser) throws IOException { } @Override - public void encode(UTF8FaunaGenerator gen, String obj) throws IOException { + public void encode(UTF8FaunaGenerator gen, String obj) throws CodecException { if (obj == null) { gen.writeNullValue(); } else { diff --git a/src/main/java/com/fauna/exception/AbortException.java b/src/main/java/com/fauna/exception/AbortException.java index 190e83b6..11a82f57 100644 --- a/src/main/java/com/fauna/exception/AbortException.java +++ b/src/main/java/com/fauna/exception/AbortException.java @@ -27,7 +27,7 @@ public T getAbort(Class clazz) throws IOException { var abStr = this.getResponse().getAbortString(); if (abStr.isPresent()) { var codec = provider.get(clazz); - var parser = new UTF8FaunaParser(abStr.get()); + var parser = UTF8FaunaParser.fromString(abStr.get()); abort = codec.decode(parser); return (T) abort; } else { diff --git a/src/main/java/com/fauna/exception/CodecException.java b/src/main/java/com/fauna/exception/CodecException.java index 46d22a4f..d17616c3 100644 --- a/src/main/java/com/fauna/exception/CodecException.java +++ b/src/main/java/com/fauna/exception/CodecException.java @@ -1,5 +1,8 @@ package com.fauna.exception; +import java.io.IOException; +import java.util.concurrent.Callable; + public class CodecException extends FaunaException { public CodecException(String message) { @@ -9,4 +12,12 @@ public CodecException(String message) { public CodecException(String message, Throwable cause) { super(message, cause); } + + public static CodecException decodingIOException(IOException exc) { + return new CodecException("IOException while decoding.", exc); + } + + public static CodecException encodingIOException(IOException exc) { + return new CodecException("IOException while encoding.", exc); + } } \ No newline at end of file diff --git a/src/main/java/com/fauna/exception/ErrorHandler.java b/src/main/java/com/fauna/exception/ErrorHandler.java index fa9a9e40..6bb59f2d 100644 --- a/src/main/java/com/fauna/exception/ErrorHandler.java +++ b/src/main/java/com/fauna/exception/ErrorHandler.java @@ -44,7 +44,7 @@ public class ErrorHandler { * @throws ThrottlingException * */ - public static void handleErrorResponse(int statusCode, QueryResponseWire response, String body) throws IOException { + public static void handleErrorResponse(int statusCode, QueryResponseWire response, String body) { QueryFailure failure = new QueryFailure(statusCode, response); handleQueryFailure(statusCode, failure); throw new ProtocolException(statusCode, body); diff --git a/src/main/java/com/fauna/response/QueryFailure.java b/src/main/java/com/fauna/response/QueryFailure.java index 6a3b204a..c9209bfa 100644 --- a/src/main/java/com/fauna/response/QueryFailure.java +++ b/src/main/java/com/fauna/response/QueryFailure.java @@ -2,6 +2,9 @@ import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.UTF8FaunaParser; +import com.fauna.exception.ClientException; +import com.fauna.exception.ClientResponseException; +import com.fauna.exception.CodecException; import com.fauna.response.wire.ConstraintFailureWire; import com.fauna.response.wire.QueryResponseWire; @@ -28,7 +31,7 @@ public final class QueryFailure extends QueryResponse { * @param statusCode The HTTP status code. * @param response The parsed response. */ - public QueryFailure(int statusCode, QueryResponseWire response) throws IOException { + public QueryFailure(int statusCode, QueryResponseWire response) { super(response); this.statusCode = statusCode; @@ -42,9 +45,13 @@ public QueryFailure(int statusCode, QueryResponseWire response) throws IOExcepti var cf = new ArrayList(); var codec = DefaultCodecProvider.SINGLETON.get(Object.class); for (ConstraintFailureWire cfw : err.getConstraintFailures().get()) { - var parser = new UTF8FaunaParser(cfw.getPaths()); - var paths = codec.decode(parser); - cf.add(new ConstraintFailure(cfw.getMessage(), cfw.getName(), (List>) paths)); + try { + var parser = UTF8FaunaParser.fromString(cfw.getPaths()); + var paths = codec.decode(parser); + cf.add(new ConstraintFailure(cfw.getMessage(), cfw.getName(), (List>) paths)); + } catch (CodecException exc) { + throw new ClientResponseException("Failed to parse constraint failure.", exc); + } } constraintFailures = cf; } diff --git a/src/main/java/com/fauna/response/QueryResponse.java b/src/main/java/com/fauna/response/QueryResponse.java index e7eb921f..4aa52988 100644 --- a/src/main/java/com/fauna/response/QueryResponse.java +++ b/src/main/java/com/fauna/response/QueryResponse.java @@ -1,5 +1,6 @@ package com.fauna.response; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fauna.codec.Codec; import com.fauna.exception.ClientResponseException; @@ -45,8 +46,8 @@ public static QuerySuccess handleResponse(HttpResponse response, ErrorHandler.handleErrorResponse(response.statusCode(), responseInternal, body); } return new QuerySuccess<>(codec, responseInternal); - } catch (IOException exc) { // Jackson JsonProcessingException subclasses IOException - throw new ProtocolException(exc, response.statusCode(), body); + } catch (JsonProcessingException exc) { // Jackson JsonProcessingException subclasses IOException + throw new ClientResponseException("Failed to handle error response.", exc, response.statusCode()); } } diff --git a/src/main/java/com/fauna/response/QuerySuccess.java b/src/main/java/com/fauna/response/QuerySuccess.java index d3c9c6b3..831c80c6 100644 --- a/src/main/java/com/fauna/response/QuerySuccess.java +++ b/src/main/java/com/fauna/response/QuerySuccess.java @@ -3,6 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.UTF8FaunaParser; import com.fauna.exception.ClientResponseException; +import com.fauna.exception.CodecException; import com.fauna.response.wire.QueryResponseWire; import java.io.IOException; @@ -20,13 +21,15 @@ public final class QuerySuccess extends QueryResponse { * @param codec A codec for the response data type. * @param response The parsed response. */ - public QuerySuccess(Codec codec, QueryResponseWire response) - throws IOException { + public QuerySuccess(Codec codec, QueryResponseWire response) { super(response); - UTF8FaunaParser reader = new UTF8FaunaParser(response.getData()); - this.data = codec.decode(reader); - + try { + UTF8FaunaParser reader = UTF8FaunaParser.fromString(response.getData()); + this.data = codec.decode(reader); + } catch (CodecException exc) { + throw new ClientResponseException("Failed to decode response.", exc); + } this.staticType = response.getStaticType(); } diff --git a/src/test/java/com/fauna/codec/Helpers.java b/src/test/java/com/fauna/codec/Helpers.java index 4953d50d..bfd37832 100644 --- a/src/test/java/com/fauna/codec/Helpers.java +++ b/src/test/java/com/fauna/codec/Helpers.java @@ -5,7 +5,7 @@ public class Helpers { public static T decode(Codec codec, String val) throws IOException { - var parser = new UTF8FaunaParser(val); + var parser = UTF8FaunaParser.fromString(val); return codec.decode(parser); } diff --git a/src/test/java/com/fauna/codec/UTF8FaunaGeneratorTest.java b/src/test/java/com/fauna/codec/UTF8FaunaGeneratorTest.java index 41233373..8d464097 100644 --- a/src/test/java/com/fauna/codec/UTF8FaunaGeneratorTest.java +++ b/src/test/java/com/fauna/codec/UTF8FaunaGeneratorTest.java @@ -77,7 +77,7 @@ public void writeTime() throws IOException { @Test - public void writeObject() throws IOException { + public void writeObject() { writer.writeStartObject(); writer.writeInt("anInt", 42); writer.writeLong("aLong", 42L); @@ -175,7 +175,7 @@ public void writeNonUTCTime() throws IOException { assertWriter("{\"@time\":\"2024-01-23T20:33:10.300001Z\"}"); } - private void assertWriter(String expected) throws IOException { + private void assertWriter(String expected) { writer.flush(); String actual = writer.serialize(); assertEquals(expected, actual); diff --git a/src/test/java/com/fauna/codec/UTF8FaunaParserTest.java b/src/test/java/com/fauna/codec/UTF8FaunaParserTest.java index e44f4f94..bf9291a5 100644 --- a/src/test/java/com/fauna/codec/UTF8FaunaParserTest.java +++ b/src/test/java/com/fauna/codec/UTF8FaunaParserTest.java @@ -4,8 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import com.fauna.exception.CodecException; import com.fauna.types.Module; -import com.fauna.exception.ClientException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -21,9 +21,9 @@ class UTF8FaunaParserTest { @Test - public void testGetValueAsString() throws IOException { + public void testGetValueAsString() { InputStream inputStream = new ByteArrayInputStream("\"hello\"".getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.STRING, "hello") @@ -36,7 +36,7 @@ public void testGetValueAsString() throws IOException { public void testGetValueAsByteArray() throws IOException { String s = "{\"@bytes\": \"RmF1bmE=\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.BYTES, "Fauna".getBytes()) @@ -49,7 +49,7 @@ public void testGetValueAsByteArray() throws IOException { public void testGetValueAsInt() throws IOException { String s = "{\"@int\": \"123\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.INT, 123) @@ -62,13 +62,13 @@ public void testGetValueAsInt() throws IOException { public void testGetValueAsIntFail() throws IOException { String invalidJson = "{\"@int\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); - UTF8FaunaParser invalidReader = new UTF8FaunaParser(invalidInputStream); + UTF8FaunaParser invalidReader = UTF8FaunaParser.fromInputStream(invalidInputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.INT, "abc") ); - Exception ex = assertThrows(ClientException.class, + Exception ex = assertThrows(CodecException.class, () -> assertReader(invalidReader, expectedTokens)); assertEquals("Error getting the current token as Integer", ex.getMessage()); @@ -79,8 +79,8 @@ public void testUnexpectedEndDuringAdvance() throws IOException { String json = "{\"@int\": \"123\""; InputStream inputStream = new ByteArrayInputStream(json.getBytes()); - Exception ex = assertThrows(ClientException.class, - () -> new UTF8FaunaParser(inputStream)); + Exception ex = assertThrows(CodecException.class, + () -> UTF8FaunaParser.fromInputStream(inputStream)); assertEquals("Failed to advance underlying JSON reader.", ex.getMessage()); } @@ -88,7 +88,7 @@ public void testUnexpectedEndDuringAdvance() throws IOException { @Test public void testGetValueAsBooleanTrue() throws IOException { InputStream inputStream = new ByteArrayInputStream("true".getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.TRUE, true) @@ -100,7 +100,7 @@ public void testGetValueAsBooleanTrue() throws IOException { @Test public void testGetValueAsBooleanFalse() throws IOException { InputStream inputStream = new ByteArrayInputStream("false".getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.FALSE, false) @@ -113,7 +113,7 @@ public void testGetValueAsBooleanFalse() throws IOException { public void testGetValueAsLocalDate() throws IOException { String s = "{\"@date\":\"2024-01-23\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.DATE, LocalDate.of(2024, 1, 23)) @@ -126,13 +126,13 @@ public void testGetValueAsLocalDate() throws IOException { public void testGetValueAsLocalDateFail() throws IOException { String invalidJson = "{\"@date\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); - UTF8FaunaParser invalidReader = new UTF8FaunaParser(invalidInputStream); + UTF8FaunaParser invalidReader = UTF8FaunaParser.fromInputStream(invalidInputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.DATE, "abc") ); - Exception ex = assertThrows(ClientException.class, + Exception ex = assertThrows(CodecException.class, () -> assertReader(invalidReader, expectedTokens)); assertEquals("Error getting the current token as LocalDate", ex.getMessage()); @@ -142,7 +142,7 @@ public void testGetValueAsLocalDateFail() throws IOException { public void testGetValueAsTime() throws IOException { String s = "{\"@time\":\"2024-01-23T13:33:10.300Z\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); Instant instant = Instant.parse("2024-01-23T13:33:10.300Z"); @@ -158,7 +158,7 @@ public void testGetValueAsStreamToken() throws IOException { String s = "{\"@stream\":\"0123456789abcdefABCDEF\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.STREAM, "0123456789abcdefABCDEF") @@ -171,13 +171,13 @@ public void testGetValueAsStreamToken() throws IOException { public void testGetValueAsTimeFail() throws IOException { String invalidJson = "{\"@time\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); - UTF8FaunaParser invalidReader = new UTF8FaunaParser(invalidInputStream); + UTF8FaunaParser invalidReader = UTF8FaunaParser.fromInputStream(invalidInputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.TIME, "abc") ); - Exception ex = assertThrows(ClientException.class, + Exception ex = assertThrows(CodecException.class, () -> assertReader(invalidReader, expectedTokens)); assertEquals("Error getting the current token as LocalDateTime", ex.getMessage()); @@ -187,7 +187,7 @@ public void testGetValueAsTimeFail() throws IOException { public void testeGetValueAsTimeNonUTC() throws IOException { String s = "{\"@time\":\"2023-12-03T05:52:10.000001-09:00\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); Instant instant = Instant.parse("2023-12-03T05:52:10.000001-09:00"); @@ -203,7 +203,7 @@ public void testeGetValueAsTimeNonUTC() throws IOException { public void testGetValueAsDouble() throws IOException { String s = "{\"@double\": \"1.23\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.DOUBLE, 1.23D) @@ -216,13 +216,13 @@ public void testGetValueAsDouble() throws IOException { public void testGetValueAsDoubleFail() throws IOException { String invalidJson = "{\"@double\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); - UTF8FaunaParser invalidReader = new UTF8FaunaParser(invalidInputStream); + UTF8FaunaParser invalidReader = UTF8FaunaParser.fromInputStream(invalidInputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.DOUBLE, "abc") ); - Exception ex = assertThrows(ClientException.class, + Exception ex = assertThrows(CodecException.class, () -> assertReader(invalidReader, expectedTokens)); assertEquals("Error getting the current token as Double", ex.getMessage()); @@ -232,7 +232,7 @@ public void testGetValueAsDoubleFail() throws IOException { public void testGetValueAsLong() throws IOException { String s = "{\"@long\": \"123\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.LONG, 123L) @@ -245,13 +245,13 @@ public void testGetValueAsLong() throws IOException { public void testGetValueAsLongFail() throws IOException { String invalidJson = "{\"@long\": \"abc\"}"; InputStream invalidInputStream = new ByteArrayInputStream(invalidJson.getBytes()); - UTF8FaunaParser invalidReader = new UTF8FaunaParser(invalidInputStream); + UTF8FaunaParser invalidReader = UTF8FaunaParser.fromInputStream(invalidInputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.LONG, "abc") ); - Exception ex = assertThrows(ClientException.class, + Exception ex = assertThrows(CodecException.class, () -> assertReader(invalidReader, expectedTokens)); assertEquals("Error getting the current token as Long", ex.getMessage()); @@ -261,7 +261,7 @@ public void testGetValueAsLongFail() throws IOException { public void testGetValueAsModule() throws IOException { String s = "{\"@mod\": \"MyModule\"}"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( Map.entry(FaunaTokenType.MODULE, new Module("MyModule")) @@ -274,7 +274,7 @@ public void testGetValueAsModule() throws IOException { public void readArrayWithEmptyObject() throws IOException { String s = "[{}]"; InputStream inputStream = new ByteArrayInputStream(s.getBytes()); - UTF8FaunaParser reader = new UTF8FaunaParser(inputStream); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(inputStream); List> expectedTokens = List.of( new AbstractMap.SimpleEntry<>(FaunaTokenType.START_ARRAY, null), @@ -296,7 +296,7 @@ public void testReadEscapedObject() throws IOException { " \"anEscapedObject\": { \"@object\": { \"@long\": \"notalong\" } }\n" + " }\n" + "}"; - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(s.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(s.getBytes())); List> expectedTokens = List.of( new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null), @@ -327,7 +327,7 @@ public void testReadDocumentTokens() throws IOException { " \"data\": { \"foo\": \"bar\" }\n" + " }\n" + "}"; - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(s.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(s.getBytes())); List> expectedTokens = List.of( new AbstractMap.SimpleEntry<>(FaunaTokenType.START_DOCUMENT, null), @@ -356,7 +356,7 @@ public void testReadSet() throws IOException { " \"after\": \"afterme\"\n" + " }\n" + "}"; - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(s.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(s.getBytes())); List> expectedTokens = List.of( new AbstractMap.SimpleEntry<>(FaunaTokenType.START_PAGE, null), @@ -376,7 +376,7 @@ public void testReadSet() throws IOException { public void testReadRef() throws IOException { String s = "{\"@ref\": {\"id\": \"123\", \"coll\": {\"@mod\": \"Col\"}}}"; - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(s.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(s.getBytes())); List> expectedTokens = List.of( new AbstractMap.SimpleEntry<>(FaunaTokenType.START_REF, null), @@ -407,7 +407,7 @@ public void testReadObjectTokens() throws IOException { " \"false\": false,\n" + " \"null\": null\n" + "}"; - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(s.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(s.getBytes())); List> expectedTokens = List.of( new AbstractMap.SimpleEntry<>(FaunaTokenType.START_OBJECT, null), @@ -481,7 +481,7 @@ public void testReadArray() throws IOException { " false,\n" + " null\n" + "]"; - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(s.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(s.getBytes())); List> expectedTokens = List.of( new AbstractMap.SimpleEntry<>(FaunaTokenType.START_ARRAY, null), @@ -528,8 +528,8 @@ public void testReadArray() throws IOException { @Test public void throwsOnMalformedJson() { String s = "{"; - assertThrows(ClientException.class, () -> { - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(s.getBytes())); + assertThrows(CodecException.class, () -> { + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(s.getBytes())); reader.read(); reader.read(); }, "Failed to advance underlying JSON reader."); @@ -547,7 +547,7 @@ public void skipValues() throws IOException { ); for (String test : tests) { - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(test.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(test.getBytes())); reader.skip(); assertFalse(reader.read()); } @@ -556,7 +556,7 @@ public void skipValues() throws IOException { @Test public void skipNestedEscapedObject() throws IOException { String test = "{\"@object\": {\"inner\": {\"@object\": {\"foo\": \"bar\"}}, \"k2\": {}}}"; - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(test.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(test.getBytes())); assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType()); reader.read(); // inner assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType()); @@ -572,7 +572,7 @@ public void skipNestedEscapedObject() throws IOException { @Test public void skipNestedObject() throws IOException { String test = "{\"k\":{\"inner\":{}},\"k2\":{}}"; - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(test.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(test.getBytes())); assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType()); reader.read(); // k assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType()); @@ -588,7 +588,7 @@ public void skipNestedObject() throws IOException { @Test public void skipNestedArrays() throws IOException { String test = "{\"k\":[\"1\",\"2\"],\"k2\":{}}"; - UTF8FaunaParser reader = new UTF8FaunaParser(new ByteArrayInputStream(test.getBytes())); + UTF8FaunaParser reader = UTF8FaunaParser.fromInputStream(new ByteArrayInputStream(test.getBytes())); assertEquals(FaunaTokenType.START_OBJECT, reader.getCurrentTokenType()); reader.read(); // k assertEquals(FaunaTokenType.FIELD_NAME, reader.getCurrentTokenType()); @@ -602,7 +602,7 @@ public void skipNestedArrays() throws IOException { } private static void assertReader(UTF8FaunaParser reader, - List> tokens) throws IOException { + List> tokens) { for (Map.Entry entry : tokens) { assertNotNull(entry.getKey()); assertNotNull(reader.getCurrentTokenType()); diff --git a/src/test/java/com/fauna/codec/codecs/BaseDocumentCodecTest.java b/src/test/java/com/fauna/codec/codecs/BaseDocumentCodecTest.java index 490c7ffe..e620a8b7 100644 --- a/src/test/java/com/fauna/codec/codecs/BaseDocumentCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/BaseDocumentCodecTest.java @@ -4,11 +4,12 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.exception.NullDocumentException; -import com.fauna.types.*; +import com.fauna.types.BaseDocument; +import com.fauna.types.Document; import com.fauna.types.Module; -import org.junit.jupiter.api.Test; +import com.fauna.types.NamedDocument; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -56,7 +57,7 @@ public static Stream testCases() { Arguments.of(TestType.Decode, BASE_DOCUMENT_CODEC, DOCUMENT_WIRE, DOCUMENT, null), Arguments.of(TestType.Decode, BASE_DOCUMENT_CODEC, NAMED_DOCUMENT_WIRE, NAMED_DOCUMENT, null), Arguments.of(TestType.Decode, BASE_DOCUMENT_CODEC, NULL_DOC_WIRE, null, NULL_DOC_EXCEPTION), - Arguments.of(TestType.Decode, BASE_DOCUMENT_CODEC, DOCUMENT_REF_WIRE, null, new ClientException("Unexpected type `class com.fauna.types.DocumentRef` decoding with `BaseDocumentCodec`")), + Arguments.of(TestType.Decode, BASE_DOCUMENT_CODEC, DOCUMENT_REF_WIRE, null, new CodecException("Unexpected type `class com.fauna.types.DocumentRef` decoding with `BaseDocumentCodec`")), Arguments.of(TestType.Encode, BASE_DOCUMENT_CODEC, DOCUMENT_REF_WIRE, DOCUMENT, null), Arguments.of(TestType.Encode, BASE_DOCUMENT_CODEC, NAMED_DOCUMENT_REF_WIRE, NAMED_DOCUMENT, null) ); @@ -76,6 +77,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void baseDoc_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `BaseDocumentCodec`. Supported types for codec are [Document, Null, Ref].", type); - runCase(TestType.Decode, BASE_DOCUMENT_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, BASE_DOCUMENT_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/BaseRefCodecTest.java b/src/test/java/com/fauna/codec/codecs/BaseRefCodecTest.java index bf463ec1..7ba3d049 100644 --- a/src/test/java/com/fauna/codec/codecs/BaseRefCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/BaseRefCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.exception.NullDocumentException; import com.fauna.types.BaseRef; import com.fauna.types.DocumentRef; @@ -56,6 +56,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void baseRef_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `BaseRefCodec`. Supported types for codec are [Null, Ref].", type); - runCase(TestType.Decode, BASE_REF_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, BASE_REF_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/BoolCodecTest.java b/src/test/java/com/fauna/codec/codecs/BoolCodecTest.java index 2ec65ce6..11ef220f 100644 --- a/src/test/java/com/fauna/codec/codecs/BoolCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/BoolCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -38,6 +38,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void bool_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `BoolCodec`. Supported types for codec are [Boolean, Null].", type); - runCase(TestType.Decode, BOOL_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, BOOL_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/ByteArrayCodecTest.java b/src/test/java/com/fauna/codec/codecs/ByteArrayCodecTest.java index 2dd228e5..8a45b340 100644 --- a/src/test/java/com/fauna/codec/codecs/ByteArrayCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/ByteArrayCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -36,6 +36,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void byteArray_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `ByteArrayCodec`. Supported types for codec are [Bytes, Null].", type); - runCase(TestType.Decode, BYTE_ARRAY_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, BYTE_ARRAY_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/ByteCodecTest.java b/src/test/java/com/fauna/codec/codecs/ByteCodecTest.java index 09185f47..133be5a2 100644 --- a/src/test/java/com/fauna/codec/codecs/ByteCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/ByteCodecTest.java @@ -4,7 +4,7 @@ import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; import com.fauna.codec.Helpers; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -42,6 +42,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void byte_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `ByteCodec`. Supported types for codec are [Int, Null].", type); - runCase(TestType.Decode, BYTE_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, BYTE_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/CharCodecTest.java b/src/test/java/com/fauna/codec/codecs/CharCodecTest.java index 5712f56d..ef033939 100644 --- a/src/test/java/com/fauna/codec/codecs/CharCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/CharCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -39,6 +39,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void char_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `CharCodec`. Supported types for codec are [Int, Null].", type); - runCase(TestType.Decode, CHAR_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, CHAR_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/ClassCodecTest.java b/src/test/java/com/fauna/codec/codecs/ClassCodecTest.java index 80f92ebb..b774baf0 100644 --- a/src/test/java/com/fauna/codec/codecs/ClassCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/ClassCodecTest.java @@ -10,7 +10,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.exception.NullDocumentException; import com.fauna.types.Module; import org.junit.jupiter.api.Test; @@ -104,6 +104,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void class_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `ClassCodec`. Supported types for codec are [Document, Null, Object, Ref].", type); - runCase(TestType.Decode, CLASS_WITH_ATTRIBUTES_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, CLASS_WITH_ATTRIBUTES_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/DoubleCodecTest.java b/src/test/java/com/fauna/codec/codecs/DoubleCodecTest.java index e9a9343c..6bebb3aa 100644 --- a/src/test/java/com/fauna/codec/codecs/DoubleCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/DoubleCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -39,6 +39,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void double_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `DoubleCodec`. Supported types for codec are [Double, Int, Long, Null].", type); - runCase(TestType.Decode, DOUBLE_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, DOUBLE_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/EnumCodecTest.java b/src/test/java/com/fauna/codec/codecs/EnumCodecTest.java index 4d854356..e903390a 100644 --- a/src/test/java/com/fauna/codec/codecs/EnumCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/EnumCodecTest.java @@ -2,7 +2,7 @@ import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -36,6 +36,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void enum_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `EnumCodec`. Supported types for codec are [Null, String].", type); - runCase(TestType.Decode, DefaultCodecProvider.SINGLETON.get(TestEnum.class), wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, DefaultCodecProvider.SINGLETON.get(TestEnum.class), wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/FloatCodecTest.java b/src/test/java/com/fauna/codec/codecs/FloatCodecTest.java index e83d5113..25f6c288 100644 --- a/src/test/java/com/fauna/codec/codecs/FloatCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/FloatCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -39,6 +39,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void float_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `FloatCodec`. Supported types for codec are [Double, Int, Long, Null].", type); - runCase(TestType.Decode, FLOAT_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, FLOAT_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/InstantCodecTest.java b/src/test/java/com/fauna/codec/codecs/InstantCodecTest.java index d45fb734..9c5246df 100644 --- a/src/test/java/com/fauna/codec/codecs/InstantCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/InstantCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -45,6 +45,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void instant_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `InstantCodec`. Supported types for codec are [Null, Time].", type); - runCase(TestType.Decode, INSTANT_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, INSTANT_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/IntCodecTest.java b/src/test/java/com/fauna/codec/codecs/IntCodecTest.java index 8e807032..611e18d1 100644 --- a/src/test/java/com/fauna/codec/codecs/IntCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/IntCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -38,6 +38,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void int_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `IntCodec`. Supported types for codec are [Int, Long, Null].", type); - runCase(TestType.Decode, INT_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, INT_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/ListCodecTest.java b/src/test/java/com/fauna/codec/codecs/ListCodecTest.java index 32c7cf5c..acce9aac 100644 --- a/src/test/java/com/fauna/codec/codecs/ListCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/ListCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.types.Document; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -41,6 +41,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void list_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `ListCodec`. Supported types for codec are [Array, Null].", type); - runCase(TestType.Decode, LIST_INT_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, LIST_INT_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/LocalDateCodecTest.java b/src/test/java/com/fauna/codec/codecs/LocalDateCodecTest.java index cc8d4702..2143784d 100644 --- a/src/test/java/com/fauna/codec/codecs/LocalDateCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/LocalDateCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -37,6 +37,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void localDate_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `LocalDateCodec`. Supported types for codec are [Date, Null].", type); - runCase(TestType.Decode, LOCAL_DATE_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, LOCAL_DATE_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/LongCodecTest.java b/src/test/java/com/fauna/codec/codecs/LongCodecTest.java index d9eb8d27..c1933cc5 100644 --- a/src/test/java/com/fauna/codec/codecs/LongCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/LongCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -39,6 +39,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void long_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `LongCodec`. Supported types for codec are [Int, Long, Null].", type); - runCase(TestType.Decode, LONG_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, LONG_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/MapCodecTest.java b/src/test/java/com/fauna/codec/codecs/MapCodecTest.java index 02dace8f..cb03cad6 100644 --- a/src/test/java/com/fauna/codec/codecs/MapCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/MapCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -48,6 +48,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void map_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `MapCodec`. Supported types for codec are [Null, Object].", type); - runCase(TestType.Decode, MAP_STRING_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, MAP_STRING_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/ModuleCodecTest.java b/src/test/java/com/fauna/codec/codecs/ModuleCodecTest.java index 63eebb3b..5b2e1ab6 100644 --- a/src/test/java/com/fauna/codec/codecs/ModuleCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/ModuleCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.types.Module; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -37,6 +37,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void module_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `ModuleCodec`. Supported types for codec are [Module, Null].", type); - runCase(TestType.Decode, MODULE_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, MODULE_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/NullableCodecTest.java b/src/test/java/com/fauna/codec/codecs/NullableCodecTest.java index 4ea0b873..d2d9cdb6 100644 --- a/src/test/java/com/fauna/codec/codecs/NullableCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/NullableCodecTest.java @@ -4,7 +4,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.types.*; import com.fauna.types.Module; import org.junit.jupiter.params.ParameterizedTest; @@ -78,6 +78,6 @@ public static Stream unsupportedTypeCases() { public void nullable_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { // This codec will pass through the supported types of the underlying codec var exMsg = MessageFormat.format("Unable to decode `{0}` with `BaseDocumentCodec`. Supported types for codec are [Document, Null, Ref].", type); - runCase(TestType.Decode, NULLABLE_DOC_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, NULLABLE_DOC_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/OptionalCodecTest.java b/src/test/java/com/fauna/codec/codecs/OptionalCodecTest.java index b5927bf6..64a6c7db 100644 --- a/src/test/java/com/fauna/codec/codecs/OptionalCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/OptionalCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -40,6 +40,6 @@ public static Stream unsupportedTypeCases() { public void optional_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { // This codec will pass through the supported types of the underlying codec var exMsg = MessageFormat.format("Unable to decode `{0}` with `StringCodec`. Supported types for codec are [Bytes, Null, String].", type); - runCase(TestType.Decode, OPTIONAL_STRING_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, OPTIONAL_STRING_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/PageCodecTest.java b/src/test/java/com/fauna/codec/codecs/PageCodecTest.java index 056682d5..3f179e3e 100644 --- a/src/test/java/com/fauna/codec/codecs/PageCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/PageCodecTest.java @@ -4,7 +4,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import com.fauna.types.Document; import com.fauna.types.Page; import org.junit.jupiter.params.ParameterizedTest; @@ -56,6 +56,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void page_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `PageCodec`. Supported types for codec are [Array, Boolean, Bytes, Date, Double, Document, Int, Long, Module, Null, Object, Ref, Set, String, Time].", type); - runCase(TestType.Decode, pageCodecOf(Object.class), wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, pageCodecOf(Object.class), wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/ShortCodecTest.java b/src/test/java/com/fauna/codec/codecs/ShortCodecTest.java index 08a0f2bb..cb924b1c 100644 --- a/src/test/java/com/fauna/codec/codecs/ShortCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/ShortCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -39,6 +39,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void short_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `ShortCodec`. Supported types for codec are [Int, Null].", type); - runCase(TestType.Decode, SHORT_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, SHORT_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/codec/codecs/StringCodecTest.java b/src/test/java/com/fauna/codec/codecs/StringCodecTest.java index 466f815d..92e7f5dd 100644 --- a/src/test/java/com/fauna/codec/codecs/StringCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/StringCodecTest.java @@ -3,7 +3,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.exception.ClientException; +import com.fauna.exception.CodecException; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -37,6 +37,6 @@ public static Stream unsupportedTypeCases() { @MethodSource("unsupportedTypeCases") public void string_runUnsupportedTypeTestCases(String wire, FaunaType type) throws IOException { var exMsg = MessageFormat.format("Unable to decode `{0}` with `StringCodec`. Supported types for codec are [Bytes, Null, String].", type); - runCase(TestType.Decode, STRING_CODEC, wire, null, new ClientException(exMsg)); + runCase(TestType.Decode, STRING_CODEC, wire, null, new CodecException(exMsg)); } } diff --git a/src/test/java/com/fauna/response/QueryResponseTest.java b/src/test/java/com/fauna/response/QueryResponseTest.java index 8b8a146a..109607b5 100644 --- a/src/test/java/com/fauna/response/QueryResponseTest.java +++ b/src/test/java/com/fauna/response/QueryResponseTest.java @@ -11,7 +11,8 @@ import com.fauna.beans.ClassWithAttributes; import com.fauna.codec.*; import com.fauna.constants.ResponseFields; -import com.fauna.exception.ClientException; +import com.fauna.exception.ClientResponseException; +import com.fauna.exception.CodecException; import java.io.IOException; import java.net.http.HttpResponse; @@ -83,17 +84,15 @@ public void handleResponseWithInvalidJsonThrowsProtocolException() { when(resp.statusCode()).thenReturn(400); when(resp.body()).thenReturn(body); - ProtocolException exc = assertThrows(ProtocolException.class, () -> QueryResponse.handleResponse(resp, codecProvider.get(Object.class))); - assertEquals("ProtocolException HTTP 400 with body: " + body, exc.getMessage()); - assertEquals(400, exc.getStatusCode()); - assertEquals(body, exc.getBody()); + ClientResponseException exc = assertThrows(ClientResponseException.class, () -> QueryResponse.handleResponse(resp, codecProvider.get(Object.class))); + assertEquals("ClientResponseException HTTP 400: Failed to handle error response.", exc.getMessage()); } @Test public void handleResponseWithMissingStatsThrowsProtocolException() { HttpResponse resp = mock(HttpResponse.class); when(resp.body()).thenReturn("{\"not valid json\""); - assertThrows(ProtocolException.class, () -> QueryResponse.handleResponse(resp, codecProvider.get(Object.class))); + assertThrows(ClientResponseException.class, () -> QueryResponse.handleResponse(resp, codecProvider.get(Object.class))); } @Test @@ -101,8 +100,8 @@ void getFromResponseBody_Exception() { String body = "Invalid JSON"; // TODO call FaunaClient.handleResponse here. - ClientException exception = assertThrows(ClientException.class, () -> { - throw new ClientException("Error occurred while parsing the response body"); + CodecException exception = assertThrows(CodecException.class, () -> { + throw new CodecException("Error occurred while parsing the response body"); }); assertEquals("Error occurred while parsing the response body", exception.getMessage()); From 6830ab6b15eaf59a5c49603950dd07e4edc72a55 Mon Sep 17 00:00:00 2001 From: David Griffin Date: Tue, 10 Sep 2024 12:23:07 -0700 Subject: [PATCH 3/4] Remove WireParseException. --- .../java/com/fauna/exception/WireParseException.java | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 src/main/java/com/fauna/exception/WireParseException.java diff --git a/src/main/java/com/fauna/exception/WireParseException.java b/src/main/java/com/fauna/exception/WireParseException.java deleted file mode 100644 index 78e3d03a..00000000 --- a/src/main/java/com/fauna/exception/WireParseException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.fauna.exception; - -public class WireParseException extends FaunaException { - - public WireParseException(String message) { - super(message); - } - - public WireParseException(String message, Throwable cause) { - super(message, cause); - } -} \ No newline at end of file From 87d801997d61261c2ff47d7a7799ac5ac4b15b09 Mon Sep 17 00:00:00 2001 From: David Griffin Date: Wed, 11 Sep 2024 09:41:47 -0700 Subject: [PATCH 4/4] Update src/main/java/com/fauna/codec/UTF8FaunaParser.java Co-authored-by: Lucas Pedroza <40873230+pnwpedro@users.noreply.github.com> --- src/main/java/com/fauna/codec/UTF8FaunaParser.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/fauna/codec/UTF8FaunaParser.java b/src/main/java/com/fauna/codec/UTF8FaunaParser.java index 4267b6be..cae4486c 100644 --- a/src/main/java/com/fauna/codec/UTF8FaunaParser.java +++ b/src/main/java/com/fauna/codec/UTF8FaunaParser.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fauna.exception.CodecException; -import com.fauna.exception.CodecException; import com.fauna.types.Module; import java.io.ByteArrayInputStream; import java.io.IOException;