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
Changes from 1 commit
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
Next Next commit
Exception stack traces always had null for message. You should never …
…override toString() of Exceptions, instead use the message
connected-jallen committed Oct 30, 2017
commit 973678c97253e413abbda1250067c1a0dd05f631
Original file line number Diff line number Diff line change
@@ -63,16 +63,18 @@ public class BleCannotSetCharacteristicNotificationException extends BleExceptio
// TODO [DS] 08.07.2017 Remove in 2.0.0
@Deprecated
public BleCannotSetCharacteristicNotificationException(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
super(createMessage(bluetoothGattCharacteristic, UNKNOWN, null));
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;
initCause(cause);
}

public BluetoothGattCharacteristic getBluetoothGattCharacteristic() {
@@ -90,17 +92,17 @@ public int getReason() {
return reason;
}

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

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

case CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR:
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
@@ -17,24 +18,25 @@ public class BleDisconnectedException extends BleException {

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

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

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

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

import android.support.annotation.Nullable;

public class BleException extends RuntimeException {

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

String toStringCauseIfExists() {
Throwable throwableCause = getCause();
static String toStringCauseIfExists(@Nullable Throwable throwableCause) {
return (throwableCause != null ? ", cause=" + throwableCause.toString() : "");
}
}
Original file line number Diff line number Diff line change
@@ -94,19 +94,22 @@ public class BleScanException extends BleException {
private final Date retryDateSuggestion;

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

public BleScanException(@Reason int reason, @NonNull Date retryDateSuggestion) {
super(createMessage(reason, retryDateSuggestion, null));
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;
initCause(causeException);
}

/**
@@ -129,16 +132,15 @@ public Date getRetryDateSuggestion() {
return retryDateSuggestion;
}

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

private String reasonDescription() {
private static String reasonDescription(int reason) {
switch (reason) {
case BLUETOOTH_CANNOT_START:
return "BLUETOOTH_CANNOT_START";
@@ -169,7 +171,7 @@ private String reasonDescription() {
}
}

private String retryDateSuggestionIfExists() {
private static String retryDateSuggestionIfExists(Date retryDateSuggestion) {
if (retryDateSuggestion == null) {
return "";
} else {