Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced silent serialization errors with exceptions #409

Merged
merged 3 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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());
}
}