Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/src/main/java/io/ably/lib/platform/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void setAndroidContext(Context context) throws AblyException {
Log.v(TAG, "setAndroidContext(): existing applicationContext is compatible with that being set");
return;
}
throw AblyException.fromErrorInfo(new ErrorInfo("Incompatible application context set", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("Incompatible application context set", 400, 40000));
} else {
Log.v(TAG, "setAndroidContext(): there was no existing applicationContext");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ AblyRest getAbly() throws AblyException {
String deviceIdentityToken = getLocalDevice().deviceIdentityToken;
if(deviceIdentityToken == null) {
Log.e(TAG, "getAbly(): unable to create Ably instance using deviceIdentityToken");
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to get Ably library instance; no device identity token", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to get Ably library instance; no device identity token", 400, 40000));
}
Log.v(TAG, "getAbly(): returning Ably instance using deviceIdentityToken");
// TODO: We need to persist Ably client options such as the environment with `deviceIdentityToken` and use these options during initialization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public void onSuccess(JsonObject response) {
JsonObject deviceIdentityTokenJson = response.getAsJsonObject("deviceIdentityToken");
if(deviceIdentityTokenJson == null) {
Log.e(TAG, "invalid device registration response (no deviceIdentityToken); deviceId = " + device.id);
machine.handleEvent(new ActivationStateMachine.GettingDeviceRegistrationFailed(new ErrorInfo("Invalid deviceIdentityToken in response", 40000, 400)));
machine.handleEvent(new ActivationStateMachine.GettingDeviceRegistrationFailed(new ErrorInfo("Invalid deviceIdentityToken in response", 400, 40000)));
return;
}
JsonPrimitive responseClientIdJson = response.getAsJsonPrimitive("clientId");
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/io/ably/lib/push/Push.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Context getApplicationContext() throws AblyException {
Context applicationContext = rest.platform.getApplicationContext();
if(applicationContext == null) {
Log.e(TAG, "getApplicationContext(): Unable to get application context; not set");
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to get application context; not set", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to get application context; not set", 400, 40000));
}
return applicationContext;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/io/ably/lib/http/HttpAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static Map<Type, String> sortAuthenticateHeaders(Collection<String> authe
Map<Type, String> sortedHeaders = new HashMap<>();
for(String header : authenticateHeaders) {
int delimiterIdx = header.indexOf(' ');
if(delimiterIdx == -1) { throw AblyException.fromErrorInfo(new ErrorInfo("Invalid authenticate header (no delimiter)", 40000, 400)); }
if(delimiterIdx == -1) { throw AblyException.fromErrorInfo(new ErrorInfo("Invalid authenticate header (no delimiter)", 400, 40000)); }
String authType = header.substring(0, delimiterIdx).trim();
String authDetails = header.substring(delimiterIdx + 1).trim();
sortedHeaders.put(Type.parse(authType), authDetails);
Expand Down Expand Up @@ -91,7 +91,7 @@ public void processAuthenticateHeaders(Map<Type, String> authenticateHeaders) th
String authDetails = authenticateHeaders.get(type = prefType);
if(authDetails == null) {
Entry<Type, String> firstEntry = authenticateHeaders.entrySet().iterator().next();
if(firstEntry == null) { throw AblyException.fromErrorInfo(new ErrorInfo("Invalid authenticate header (no entries)", 40000, 400)); }
if(firstEntry == null) { throw AblyException.fromErrorInfo(new ErrorInfo("Invalid authenticate header (no entries)", 400, 40000)); }
type = firstEntry.getKey();
authDetails = firstEntry.getValue();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/main/java/io/ably/lib/http/HttpCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ public HttpCore(ClientOptions options, Auth auth, PlatformAgentProvider platform
if (proxyOptions != null) {
String proxyHost = proxyOptions.host;
if (proxyHost == null) {
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to configure proxy without proxy host", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to configure proxy without proxy host", 400, 40000));
}
int proxyPort = proxyOptions.port;
if (proxyPort == 0) {
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to configure proxy without proxy port", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to configure proxy without proxy port", 400, 40000));
}
String proxyUser = proxyOptions.username;
if (proxyUser != null) {
String proxyPassword = proxyOptions.password;
if (proxyPassword == null) {
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to configure proxy without proxy password", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("Unable to configure proxy without proxy password", 400, 40000));
}
proxyAuth = new HttpAuth(proxyUser, proxyPassword, proxyOptions.prefAuthType);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/io/ably/lib/realtime/AblyRealtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public Channel get(final String channelName, final ChannelOptions channelOptions
if (existingChannel != null) {
if (channelOptions != null) {
if (existingChannel.shouldReattachToSetOptions(channelOptions)) {
throw AblyException.fromErrorInfo(new ErrorInfo("Channels.get() cannot be used to set channel options that would cause the channel to reattach. Please, use Channel.setOptions() instead.", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("Channels.get() cannot be used to set channel options that would cause the channel to reattach. Please, use Channel.setOptions() instead.", 400, 40000));
}
existingChannel.setOptions(channelOptions);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/io/ably/lib/realtime/ChannelBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1219,13 +1219,13 @@ static Param[] replacePlaceholderParams(Channel channel, Param[] placeholderPara
if(KEY_UNTIL_ATTACH.equals(param.key)) {
if("true".equalsIgnoreCase(param.value)) {
if (channel.state != ChannelState.attached) {
throw AblyException.fromErrorInfo(new ErrorInfo("option untilAttach requires the channel to be attached", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("option untilAttach requires the channel to be attached", 400, 40000));
}

params.add(new Param(KEY_FROM_SERIAL, channel.properties.attachSerial));
}
else if(!"false".equalsIgnoreCase(param.value)) {
throw AblyException.fromErrorInfo(new ErrorInfo("option untilAttach is invalid. \"true\" or \"false\" expected", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("option untilAttach is invalid. \"true\" or \"false\" expected", 400, 40000));
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/io/ably/lib/rest/Auth.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public AuthOptions() {}
*/
public AuthOptions(String key) throws AblyException {
if (key == null) {
throw AblyException.fromErrorInfo(new ErrorInfo("key string cannot be null", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("key string cannot be null", 400, 40000));
}
if (key.isEmpty()) {
throw new IllegalArgumentException("Key string cannot be empty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ private synchronized ConnectionStateChange setState(ITransport transport, StateI
public void ping(final CompletionListener listener) {
HeartbeatWaiter waiter = new HeartbeatWaiter(listener);
if(currentState.state != ConnectionState.connected) {
waiter.onError(new ErrorInfo("Unable to ping service; not connected", 40000, 400));
waiter.onError(new ErrorInfo("Unable to ping service; not connected", 400, 40000));
return;
}
synchronized(heartbeatWaiters) {
Expand Down Expand Up @@ -1001,7 +1001,7 @@ public void run() {
pending = clear();
}
if(pending) {
onError(new ErrorInfo("Timed out waiting for heartbeat response", 50000, 500));
onError(new ErrorInfo("Timed out waiting for heartbeat response", 500, 50000));
} else {
onSuccess();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/main/java/io/ably/lib/transport/Hosts.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public Hosts(final String primaryHost, final String defaultHost, final ClientOpt
String[] tempFallbackHosts = options.fallbackHosts;
if (options.fallbackHostsUseDefault) {
if (options.fallbackHosts != null) {
throw AblyException.fromErrorInfo(new ErrorInfo("fallbackHosts and fallbackHostsUseDefault cannot both be set", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("fallbackHosts and fallbackHostsUseDefault cannot both be set", 400, 40000));
}
if (options.port != 0 || options.tlsPort != 0) {
throw AblyException.fromErrorInfo(new ErrorInfo("fallbackHostsUseDefault cannot be set when port or tlsPort are set", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("fallbackHostsUseDefault cannot be set when port or tlsPort are set", 400, 40000));
}
tempFallbackHosts = Defaults.HOST_FALLBACKS;
}
Expand All @@ -68,7 +68,7 @@ public Hosts(final String primaryHost, final String defaultHost, final ClientOpt
if (options.environment != null) {
/* TO3k2: It is never valid to provide both a restHost and environment value
* TO3k3: It is never valid to provide both a realtimeHost and environment value */
throw AblyException.fromErrorInfo(new ErrorInfo("cannot set both restHost/realtimeHost and environment options", 40000, 400));
throw AblyException.fromErrorInfo(new ErrorInfo("cannot set both restHost/realtimeHost and environment options", 400, 40000));
}
} else {
this.primaryHost = isProduction ? defaultHost : options.environment + "-" + defaultHost;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/io/ably/lib/types/ErrorInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ else if(throwable instanceof IOException) {
errorInfo = new ErrorInfo(throwable.getLocalizedMessage(), 500, 50000);
}
else {
errorInfo = new ErrorInfo("Unexpected exception: " + throwable.getLocalizedMessage(), 50000, 500);
errorInfo = new ErrorInfo("Unexpected exception: " + throwable.getLocalizedMessage(), 500, 50000);
}

return errorInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.ably.lib.transport;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import io.ably.lib.realtime.AblyRealtime;
import io.ably.lib.realtime.CompletionListener;
import io.ably.lib.types.AblyException;
import io.ably.lib.types.ClientOptions;
import io.ably.lib.types.ErrorInfo;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;

public class ConnectionManagerPingErrorTest {

/**
* Pinging while not connected reports Ably code 40000 with HTTP status code 400.
*/
@Test
public void ping_when_not_connected_error_codes() throws AblyException {
ClientOptions options = new ClientOptions("appid.keyid:keysecret");
options.autoConnect = false;
AtomicReference<ErrorInfo> error = new AtomicReference<>();
try (AblyRealtime ably = new AblyRealtime(options)) {
ably.connection.ping(new CompletionListener() {
@Override
public void onSuccess() {}

@Override
public void onError(ErrorInfo reason) {
error.set(reason);
}
});
}
assertThat(error.get(), is(notNullValue()));
assertThat(error.get().code, is(40000));
assertThat(error.get().statusCode, is(400));
}
}
16 changes: 16 additions & 0 deletions lib/src/test/java/io/ably/lib/transport/HostsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import io.ably.lib.types.AblyException;
import io.ably.lib.types.ClientOptions;
Expand Down Expand Up @@ -46,6 +47,21 @@ public void hosts_host_and_environment() throws AblyException {
new Hosts("overridden.ably.io", Defaults.HOST_REALTIME, options);
}

/**
* Configuration errors carry Ably code 40000 and HTTP status code 400.
*/
@Test
public void hosts_invalid_options_error_codes() {
options.environment = "myenv";
try {
new Hosts("overridden.ably.io", Defaults.HOST_REALTIME, options);
fail("expected AblyException");
} catch (AblyException e) {
assertThat(e.errorInfo.code, is(40000));
assertThat(e.errorInfo.statusCode, is(400));
}
}

/**
* Expect a null, when we provide empty array of fallback hosts
*/
Expand Down