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

Nicer exception messages #303

Merged
merged 4 commits into from
Nov 2, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ public class BleAlreadyConnectedException extends BleException {
private final String macAddress;

public BleAlreadyConnectedException(String macAddress) {
super("Already connected to device with MAC address " + macAddress);
this.macAddress = macAddress;
}

@Override
public String toString() {
return "BleAlreadyConnectedException{macAddress=" + macAddress + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ public class BleCannotSetCharacteristicNotificationException extends BleExceptio
// TODO [DS] 08.07.2017 Remove in 2.0.0
@Deprecated
public BleCannotSetCharacteristicNotificationException(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
super(createMessage(bluetoothGattCharacteristic, UNKNOWN));
this.bluetoothGattCharacteristic = bluetoothGattCharacteristic;
this.reason = UNKNOWN;
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public BleCannotSetCharacteristicNotificationException(BluetoothGattCharacteristic bluetoothGattCharacteristic, @Reason int reason,
Throwable cause) {
super(cause);
super(createMessage(bluetoothGattCharacteristic, reason), cause);
this.bluetoothGattCharacteristic = bluetoothGattCharacteristic;
this.reason = reason;
}
Expand All @@ -90,28 +91,23 @@ public int getReason() {
return reason;
}

@Override
public String toString() {
return "BleCannotSetCharacteristicNotificationException{"
+ "bluetoothGattCharacteristic=" + bluetoothGattCharacteristic.getUuid()
+ ", reason=" + reasonDescription()
+ " (see Javadoc for more comment)"
+ toStringCauseIfExists()
+ '}';
private static String createMessage(BluetoothGattCharacteristic bluetoothGattCharacteristic, @Reason int reason) {
return reasonDescription(reason) + " (code "
+ reason + ") with characteristic UUID " + bluetoothGattCharacteristic.getUuid();
}

private String reasonDescription() {
private static String reasonDescription(int reason) {
switch (reason) {

case CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR:
return "CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR";
return "Cannot find client characteristic config descriptor";
case CANNOT_WRITE_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR:
return "CANNOT_WRITE_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR";
return "Cannot write client characteristic config descriptor";
case CANNOT_SET_LOCAL_NOTIFICATION:
return "CANNOT_SET_LOCAL_NOTIFICATION";
return "Cannot set local notification";
case UNKNOWN:
default:
return "UNKNOWN";
return "Unknown error";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ public class BleCharacteristicNotFoundException extends BleException {
private final UUID charactersisticUUID;

public BleCharacteristicNotFoundException(UUID charactersisticUUID) {
super("Characteristic not found with UUID " + charactersisticUUID);
this.charactersisticUUID = charactersisticUUID;
}

public UUID getCharactersisticUUID() {
return charactersisticUUID;
}

@Override
public String toString() {
return "BleCharacteristicNotFoundException{" + "charactersisticUUID=" + charactersisticUUID + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class BleConflictingNotificationAlreadySetException extends BleException
private final boolean alreadySetIsIndication;

public BleConflictingNotificationAlreadySetException(UUID characteristicUuid, boolean alreadySetIsIndication) {
super("Characteristic " + characteristicUuid
+ " notification already set to " + (alreadySetIsIndication ? "indication" : "notification"));
this.characteristicUuid = characteristicUuid;
this.alreadySetIsIndication = alreadySetIsIndication;
}
Expand All @@ -25,11 +27,4 @@ public boolean notificationAlreadySet() {
return !alreadySetIsIndication;
}

@Override
public String toString() {
return "BleCharacteristicNotificationOfOtherTypeAlreadySetException{"
+ "characteristicUuid=" + characteristicUuid.toString()
+ ", typeAlreadySet=" + (alreadySetIsIndication ? "indication" : "notification")
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.polidea.rxandroidble.exceptions;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
* Exception emitted when the BLE link has been disconnected either when the connection was already established
Expand All @@ -17,24 +18,20 @@ public class BleDisconnectedException extends BleException {

@Deprecated
public BleDisconnectedException() {
super();
bluetoothDeviceAddress = "";
}

public BleDisconnectedException(Throwable throwable, @NonNull String bluetoothDeviceAddress) {
super(throwable);
super(createMessage(bluetoothDeviceAddress), throwable);
this.bluetoothDeviceAddress = bluetoothDeviceAddress;
}

public BleDisconnectedException(@NonNull String bluetoothDeviceAddress) {
super(createMessage(bluetoothDeviceAddress));
this.bluetoothDeviceAddress = bluetoothDeviceAddress;
}

@Override
public String toString() {
return "BleDisconnectedException{"
+ "bluetoothDeviceAddress='" + bluetoothDeviceAddress + '\''
+ toStringCauseIfExists()
+ '}';
private static String createMessage(@Nullable String bluetoothDeviceAddress) {
return "Disconnected from " + bluetoothDeviceAddress;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.polidea.rxandroidble.exceptions;

/**
* Base class of exceptions in this project.
*/
public class BleException extends RuntimeException {

public BleException() {
Expand All @@ -14,8 +17,7 @@ public BleException(Throwable throwable) {
super(throwable);
}

String toStringCauseIfExists() {
Throwable throwableCause = getCause();
return (throwableCause != null ? ", cause=" + throwableCause.toString() : "");
public BleException(String message, Throwable throwable) {
super(message, throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public class BleGattException extends BleException {

@Deprecated
public BleGattException(int status, BleGattOperationType bleGattOperationType) {
super(createMessage(null, status, bleGattOperationType));
this.gatt = null;
this.status = status;
this.bleGattOperationType = bleGattOperationType;
}

public BleGattException(@NonNull BluetoothGatt gatt, int status, BleGattOperationType bleGattOperationType) {
super(createMessage(gatt, status, bleGattOperationType));
this.gatt = gatt;
this.status = status;
this.bleGattOperationType = bleGattOperationType;
Expand All @@ -41,7 +43,7 @@ public BleGattException(BluetoothGatt gatt, BleGattOperationType bleGattOperatio
}

public String getMacAddress() {
return gatt != null ? gatt.getDevice().getAddress() : null;
return getMacAddress(gatt);
}

public BleGattOperationType getBleGattOperationType() {
Expand All @@ -52,17 +54,20 @@ public int getStatus() {
return status;
}

private static String getMacAddress(@Nullable BluetoothGatt gatt) {
return (gatt != null && gatt.getDevice() != null) ? gatt.getDevice().getAddress() : null;
}

@SuppressLint("DefaultLocale")
@Override
public String toString() {
private static String createMessage(@Nullable BluetoothGatt gatt, int status, BleGattOperationType bleGattOperationType) {
if (status == UNKNOWN_STATUS) {
return String.format("%s{macAddress=%s, bleGattOperationType=%s}",
getClass().getSimpleName(), getMacAddress(), bleGattOperationType);
return String.format("GATT exception from MAC address %s, with type %s",
getMacAddress(gatt), bleGattOperationType);
}

final String link
= "https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h";
return String.format("%s{macAddress=%s, status=%d (0x%02x -> %s), bleGattOperationType=%s}",
getClass().getSimpleName(), getMacAddress(), status, status, link, bleGattOperationType);
return String.format("GATT exception from MAC address %s, status %d, type %s. (Look up status 0x%02x here %s)",
getMacAddress(gatt), status, bleGattOperationType, status, link);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,19 @@ public class BleScanException extends BleException {
private final Date retryDateSuggestion;

public BleScanException(@Reason int reason) {
super(createMessage(reason, null));
this.reason = reason;
this.retryDateSuggestion = null;
}

public BleScanException(@Reason int reason, @NonNull Date retryDateSuggestion) {
super(createMessage(reason, retryDateSuggestion));
this.reason = reason;
this.retryDateSuggestion = retryDateSuggestion;
}

public BleScanException(@Reason int reason, Throwable causeException) {
super(causeException);
super(createMessage(reason, null), causeException);
this.reason = reason;
this.retryDateSuggestion = null;
}
Expand All @@ -129,51 +131,46 @@ public Date getRetryDateSuggestion() {
return retryDateSuggestion;
}

@Override
public String toString() {
return "BleScanException{"
+ "reason=" + reasonDescription()
+ retryDateSuggestionIfExists()
+ toStringCauseIfExists()
+ '}';
private static String createMessage(int reason, Date retryDateSuggestion) {
return reasonDescription(reason) + " (code " + reason + ")" + retryDateSuggestionIfExists(retryDateSuggestion);
}

private String reasonDescription() {
private static String reasonDescription(int reason) {
switch (reason) {
case BLUETOOTH_CANNOT_START:
return "BLUETOOTH_CANNOT_START";
return "Bluetooth cannot start";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good piece of code. I like it :-)

case BLUETOOTH_DISABLED:
return "BLUETOOTH_DISABLED";
return "Bluetooth disabled";
case BLUETOOTH_NOT_AVAILABLE:
return "BLUETOOTH_NOT_AVAILABLE";
return "Bluetooth not available";
case LOCATION_PERMISSION_MISSING:
return "LOCATION_PERMISSION_MISSING";
return "Location Permission missing";
case LOCATION_SERVICES_DISABLED:
return "LOCATION_SERVICES_DISABLED";
return "Location Services disabled";
case SCAN_FAILED_ALREADY_STARTED:
return "SCAN_FAILED_ALREADY_STARTED";
return "Scan failed because it has already started";
case SCAN_FAILED_APPLICATION_REGISTRATION_FAILED:
return "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED";
return "Scan failed because application registration failed";
case SCAN_FAILED_INTERNAL_ERROR:
return "SCAN_FAILED_INTERNAL_ERROR";
return "Scan failed because of an internal error";
case SCAN_FAILED_FEATURE_UNSUPPORTED:
return "SCAN_FAILED_FEATURE_UNSUPPORTED";
return "Scan failed because feature unsupported";
case SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES:
return "SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES";
return "Scan failed because out of hardware resources";
case UNDOCUMENTED_SCAN_THROTTLE:
return "UNDOCUMENTED_SCAN_THROTTLE";
return "Undocumented scan throttle";
case UNKNOWN_ERROR_CODE:
// fallthrough
default:
return "UNKNOWN";
return "Unknown error";
}
}

private String retryDateSuggestionIfExists() {
private static String retryDateSuggestionIfExists(Date retryDateSuggestion) {
if (retryDateSuggestion == null) {
return "";
} else {
return ", retryDateSuggestion=" + retryDateSuggestion;
return ", suggested retry date is " + retryDateSuggestion;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ public class BleServiceNotFoundException extends BleException {
private final UUID serviceUUID;

public BleServiceNotFoundException(UUID serviceUUID) {
super("BLE Service not found with uuid " + serviceUUID);
Copy link
Contributor

@piotrek1543 piotrek1543 Nov 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that uuid should be used here with uppercase (I mean: UUID instead of uuid), as it is more frequent used version, you used it already in another class and it relates to used there class name.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I'll update in another commit.

this.serviceUUID = serviceUUID;
}

public UUID getServiceUUID() {
return serviceUUID;
}

@Override
public String toString() {
return "BleServiceNotFoundException{serviceUUID=" + serviceUUID + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,5 @@ class RxBleClientTest extends Specification {
Thread.sleep(200) // Nasty :<
true
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary formatting :-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I'll update in another commit.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.polidea.rxandroidble.exceptions

import android.bluetooth.BluetoothGattCharacteristic
import spock.lang.Specification

/**
* Tests BleCannotSetCharacteristicNotificationException
*/
class BleCannotSetCharacteristicNotificationExceptionTest extends Specification {

BleCannotSetCharacteristicNotificationException objectUnderTest

BluetoothGattCharacteristic mockCharacteristic = Mock BluetoothGattCharacteristic

def "toString should include message"() {

given:
mockCharacteristic.uuid >> new UUID(1, 2)
when:
objectUnderTest = new BleCannotSetCharacteristicNotificationException(mockCharacteristic,
BleCannotSetCharacteristicNotificationException.CANNOT_SET_LOCAL_NOTIFICATION, new Exception("because"))

then:
assert objectUnderTest.toString() ==
"com.polidea.rxandroidble.exceptions.BleCannotSetCharacteristicNotificationException: " +
"Cannot set local notification (code 1) with characteristic UUID 00000000-0000-0001-0000-000000000002"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.polidea.rxandroidble.exceptions

import spock.lang.Specification

/**
* Created by jallen on 2017-11-01.
*/
class BleDisconnectedExceptionTest extends Specification {

BleDisconnectedException objectUnderTest

def "toString should include message"() {

when:
objectUnderTest = new BleDisconnectedException("myBluetoothAddress")

then:
assert objectUnderTest.toString() ==
"com.polidea.rxandroidble.exceptions.BleDisconnectedException: Disconnected from myBluetoothAddress"
}
}
Loading