Skip to content

Commit

Permalink
Merge pull request #409 from ably/fix/java-enum-serialization-exceptions
Browse files Browse the repository at this point in the history
Replaced silent serialization errors with exceptions
  • Loading branch information
ikurek committed May 20, 2022
2 parents 9fdf4f7 + 53fb84d commit 90509e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
24 changes: 11 additions & 13 deletions android/src/main/java/io/ably/flutter/plugin/AblyMessageCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;

import io.ably.flutter.plugin.generated.PlatformConstants;
import io.ably.flutter.plugin.types.SerializationException;
import io.ably.flutter.plugin.types.PlatformClientOptions;
import io.ably.flutter.plugin.util.CipherParamsStorage;
import io.ably.flutter.plugin.util.Consumer;
Expand Down Expand Up @@ -55,7 +56,6 @@ interface CodecDecoder<T> {
}

private static class CodecPair<T> {
private static final String TAG = CodecPair.class.getName();
final CodecEncoder<T> encoder;
final CodecDecoder<T> decoder;

Expand All @@ -66,16 +66,14 @@ private static class CodecPair<T> {

Map<String, Object> encode(final Object value) {
if (this.encoder == null) {
Log.w(TAG, "Encoder is null");
return null;
throw SerializationException.forEncoder(value.getClass());
}
return this.encoder.encode((T) value);
}

T decode(Map<String, Object> jsonMap) {
if (this.decoder == null) {
Log.w(TAG, "Decoder is null");
return null;
throw SerializationException.forDecoder(jsonMap);
}
return this.decoder.decode(jsonMap);
}
Expand Down Expand Up @@ -346,7 +344,7 @@ private int decodeLogLevel(String logLevelString) {
case PlatformConstants.TxLogLevelEnum.error:
return Log.ERROR;
default:
return Log.WARN;
throw SerializationException.forEnum(logLevelString, Log.class);
}
}

Expand Down Expand Up @@ -461,7 +459,7 @@ private ChannelMode decodeChannelOptionsMode(String mode) {
case PlatformConstants.TxEnumConstants.presenceSubscribe:
return ChannelMode.presence_subscribe;
default:
return null;
throw SerializationException.forEnum(mode, ChannelMode.class);
}
}

Expand Down Expand Up @@ -644,7 +642,7 @@ private String encodeConnectionState(ConnectionState state) {
case failed:
return PlatformConstants.TxEnumConstants.failed;
default:
return null;
throw SerializationException.forEnum(state, String.class);
}
}

Expand All @@ -669,7 +667,7 @@ private String encodeConnectionEvent(ConnectionEvent event) {
case update:
return PlatformConstants.TxEnumConstants.update;
default:
return null;
throw SerializationException.forEnum(event, String.class);
}
}

Expand All @@ -690,7 +688,7 @@ private String encodeChannelState(ChannelState state) {
case suspended:
return PlatformConstants.TxEnumConstants.suspended;
default:
return null;
throw SerializationException.forEnum(state, String.class);
}
}

Expand All @@ -713,7 +711,7 @@ private String encodeChannelEvent(ChannelEvent event) {
case update:
return PlatformConstants.TxEnumConstants.update;
default:
return null;
throw SerializationException.forEnum(event, String.class);
}
}

Expand Down Expand Up @@ -801,7 +799,7 @@ private String encodeDevicePushDetailsState(DeviceDetails.Push.State state) {
case FAILED:
return PlatformConstants.TxDevicePushStateEnum.failed;
default:
return null;
throw SerializationException.forEnum(state, String.class);
}
}

Expand Down Expand Up @@ -874,7 +872,7 @@ private String encodePresenceAction(PresenceMessage.Action action) {
case update:
return PlatformConstants.TxEnumConstants.update;
default:
return null;
throw SerializationException.forEnum(action, String.class);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.ably.flutter.plugin.types;

/**
* Thrown during object serialization
*/
public class SerializationException extends RuntimeException {

public SerializationException(String message) {
super(message);
}

public static SerializationException forEnum(Object value, Class type) {
return new SerializationException(value.toString() + " can't be encoded/decoded to " + type.toString());
}

public static SerializationException forEncoder(Class type) {
return new SerializationException("No encoder found for " + type.toString());
}

public static SerializationException forDecoder(Object value) {
return new SerializationException("No decoder found for value " + value.toString());
}
}

0 comments on commit 90509e6

Please sign in to comment.