summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/BluetoothDevice.cpp8
-rw-r--r--src/BluetoothGattCharacteristic.cpp17
-rw-r--r--src/BluetoothGattDescriptor.cpp14
-rw-r--r--src/BluetoothGattService.cpp8
-rw-r--r--src/tinyb_utils.cpp10
5 files changed, 35 insertions, 22 deletions
diff --git a/src/BluetoothDevice.cpp b/src/BluetoothDevice.cpp
index 3d37f862..93fc86ed 100644
--- a/src/BluetoothDevice.cpp
+++ b/src/BluetoothDevice.cpp
@@ -311,10 +311,10 @@ BluetoothAdapter BluetoothDevice::get_adapter ()
&error);
if (adapter == NULL) {
- g_printerr("Error instantiating adapter: %s",
- error->message);
- g_error_free(error);
- throw std::exception();
+ std::string error_msg("Error occured while instantiating adapter: ");
+ error_msg += error->message;
+ g_error_free(error);
+ throw std::runtime_error(error_msg);
}
return BluetoothAdapter(adapter);
diff --git a/src/BluetoothGattCharacteristic.cpp b/src/BluetoothGattCharacteristic.cpp
index 9f0867dc..9fb4ca96 100644
--- a/src/BluetoothGattCharacteristic.cpp
+++ b/src/BluetoothGattCharacteristic.cpp
@@ -184,11 +184,11 @@ BluetoothGattService BluetoothGattCharacteristic::get_service ()
NULL,
&error);
- if (service == NULL) {
- g_printerr("Error instantiating: %s",
- error->message);
+ if (service == nullptr) {
+ std::string error_msg("Error occured while instantiating service: ");
+ error_msg += error->message;
g_error_free(error);
- throw std::exception();
+ throw std::runtime_error(error_msg);
}
return BluetoothGattService(service);
@@ -197,7 +197,14 @@ BluetoothGattService BluetoothGattCharacteristic::get_service ()
std::vector<unsigned char> BluetoothGattCharacteristic::get_value ()
{
GBytes *value_gbytes = const_cast<GBytes *>(gatt_characteristic1_get_value (object));
- std::vector<unsigned char> result = from_gbytes_to_vector(value_gbytes);
+ std::vector<unsigned char> result;
+
+ try {
+ result = from_gbytes_to_vector(value_gbytes);
+ } catch (std::exception &e) {
+ g_bytes_unref(value_gbytes);
+ throw e;
+ }
g_bytes_unref(value_gbytes);
diff --git a/src/BluetoothGattDescriptor.cpp b/src/BluetoothGattDescriptor.cpp
index b0edfcba..969097a2 100644
--- a/src/BluetoothGattDescriptor.cpp
+++ b/src/BluetoothGattDescriptor.cpp
@@ -158,10 +158,10 @@ BluetoothGattCharacteristic BluetoothGattDescriptor::get_characteristic ()
&error);
if (characteristic == NULL) {
- g_printerr("Error instantiating: %s",
- error->message);
+ std::string error_msg("Error occured while instantiating characteristic: ");
+ error_msg += error->message;
g_error_free(error);
- throw std::exception();
+ throw std::runtime_error(error_msg);
}
return BluetoothGattCharacteristic(characteristic);
@@ -170,7 +170,13 @@ BluetoothGattCharacteristic BluetoothGattDescriptor::get_characteristic ()
std::vector<unsigned char> BluetoothGattDescriptor::get_value ()
{
GBytes *value_gbytes = const_cast<GBytes *>(gatt_descriptor1_get_value (object));
- std::vector<unsigned char> result = from_gbytes_to_vector(value_gbytes);
+ std::vector<unsigned char> result;
+ try {
+ result = from_gbytes_to_vector(value_gbytes);
+ } catch (std::exception &e) {
+ g_bytes_unref(value_gbytes);
+ throw e;
+ }
g_bytes_unref(value_gbytes);
diff --git a/src/BluetoothGattService.cpp b/src/BluetoothGattService.cpp
index c1d35be8..31757ac1 100644
--- a/src/BluetoothGattService.cpp
+++ b/src/BluetoothGattService.cpp
@@ -109,11 +109,11 @@ BluetoothDevice BluetoothGattService::get_device ()
NULL,
&error);
- if (device == NULL) {
- g_printerr("Error instantiating: %s",
- error->message);
+ if (device == nullptr) {
+ std::string error_msg("Error occured while instantiating device: ");
+ error_msg += error->message;
g_error_free(error);
- throw std::exception();
+ throw std::runtime_error(error_msg);
}
return BluetoothDevice(device);
diff --git a/src/tinyb_utils.cpp b/src/tinyb_utils.cpp
index a5853abc..a1f2a5d6 100644
--- a/src/tinyb_utils.cpp
+++ b/src/tinyb_utils.cpp
@@ -29,6 +29,9 @@ std::vector<unsigned char> tinyb::from_gbytes_to_vector(const GBytes *bytes)
gsize result_size;
const unsigned char *aux_array = (const unsigned char *)g_bytes_get_data(const_cast<GBytes *>(bytes), &result_size);
+ if (aux_array == nullptr || result_size == 0)
+ throw std::runtime_error("Trying to read empty value");
+
std::vector<unsigned char> result(result_size);
std::copy(aux_array, aux_array + result_size, result.begin());
@@ -42,11 +45,8 @@ GBytes *tinyb::from_vector_to_gbytes(const std::vector<unsigned char>& vector)
const unsigned char *vector_content = vector.data();
GBytes *result = g_bytes_new(vector_content, vector_size);
- if (!result)
- {
- g_printerr("Error: cannot allocate\n");
- throw std::exception();
- }
+ if (result == nullptr)
+ throw std::bad_alloc();
return result;
}