pusher/pusher-websocket-java

WebSocketConnection ConnectionEventListener.onError Crash

Closed this issue · 2 comments

What is the issue?

Fatal Exception: java.lang.IllegalArgumentException
Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter code

Is it a crash report? Submit stack traces or anything that you think would help

com.pusher.client.connection.websocket.WebSocketConnection$5.run (WebSocketConnection.java:238)
com.pusher.client.util.Factory$1.run (Factory.java:119)

Any improvements you suggest

        final Object codeObject = dataMap.get("code");
        **String code = null;**  => String code = "";
        if (codeObject != null) {
            code = String.valueOf(Math.round((Double)codeObject));
        }

CC @pusher/mobile

Hi, Thanks for reporting. Can you share a little more information about the error?
Are you able to replicate this at all?
Can you share the code that triggers this error in your app?

where I use it

   fun connect() {
        val options = PusherOptions().setHost(PUSHER_HOST).setWsPort(PUSHER_PORT).setWssPort(PUSHER_PORT)
        Pusher(PUSHER_KEY, options).let {
            it.connect(object : ConnectionEventListener {
                override fun onConnectionStateChange(connectionStateChange: ConnectionStateChange) {
                }

                override fun onError(msg: String, code: String, e: Exception) {
                   //it happens here
                }
            })
        }
    }

and caused by WebSocketConnection class line-238
I think it basically caused by line-220
if codeObject == null, code is always null

    private void handleError(final String wholeMessage) {
        final Map json = GSON.fromJson(wholeMessage, Map.class);
        final Object data = json.get("data");

        Map dataMap;
        if (data instanceof String) {
            dataMap = GSON.fromJson((String)data, Map.class);
        }
        else {
            dataMap = (Map)data;
        }

        final String message = (String)dataMap.get("message");

        final Object codeObject = dataMap.get("code");
        String code = null;
        if (codeObject != null) {
            code = String.valueOf(Math.round((Double)codeObject));
        }

        sendErrorToAllListeners(message, code, null);
    }

    private void sendErrorToAllListeners(final String message, final String code, final Exception e) {
        final Set<ConnectionEventListener> allListeners = new HashSet<ConnectionEventListener>();
        for (final Set<ConnectionEventListener> listenersForState : eventListeners.values()) {
            allListeners.addAll(listenersForState);
        }

        for (final ConnectionEventListener listener : allListeners) {
            factory.queueOnEventThread(new Runnable() {
                @Override
                public void run() {
                    listener.onError(message, code, e);
                }
            });
        }
    }

and I think I fixed it by changing parameters to null-acceptable ones.

    fun connect() {
        val options = PusherOptions().setHost(PUSHER_HOST).setWsPort(PUSHER_PORT).setWssPort(PUSHER_PORT)
        Pusher(PUSHER_KEY, options).let {
            it.connect(object : ConnectionEventListener {
                override fun onConnectionStateChange(connectionStateChange: ConnectionStateChange) {
                }

                override fun onError(message: String?, code: String?, e: Exception?) {
                }
            })
        }
    }

I just thought it would be nice if you guys can change the initialization of parameter from null to empty.