Skip to content

Commit

Permalink
Auto merge of #12538 - szeged:error-refactor, r=nox
Browse files Browse the repository at this point in the history
Refactor Bluetooth error handling

<!-- Please describe your changes on the following line: -->
Replace the error messages with an enum in `net/bluetooth_thread.rs`. Rename `bluetooth_blacklist.rs` to `bluetooth_utils.rs` and put the error conversion in it.
With this the returned errors in DOM classes follow the specification.
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes do not require tests because  there is no Web Bluetooth test API implementation yet.

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12538)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Jul 28, 2016
2 parents abdf01e + b4db144 commit 05fdc62
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 62 deletions.
72 changes: 32 additions & 40 deletions components/net/bluetooth_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence, RequestDeviceoptions};
use net_traits::bluetooth_thread::{BluetoothCharacteristicMsg, BluetoothCharacteristicsMsg};
use net_traits::bluetooth_thread::{BluetoothDescriptorMsg, BluetoothDescriptorsMsg};
use net_traits::bluetooth_thread::{BluetoothDeviceMsg, BluetoothMethodMsg};
use net_traits::bluetooth_thread::{BluetoothDeviceMsg, BluetoothError, BluetoothMethodMsg};
use net_traits::bluetooth_thread::{BluetoothResult, BluetoothServiceMsg, BluetoothServicesMsg};
use rand::{self, Rng};
use std::borrow::ToOwned;
Expand All @@ -25,15 +25,7 @@ use tinyfiledialogs;
use util::thread::spawn_named;

const ADAPTER_ERROR: &'static str = "No adapter found";
const DEVICE_ERROR: &'static str = "No device found";
const DEVICE_MATCH_ERROR: &'static str = "No device found, that matches the given options";
const PRIMARY_SERVICE_ERROR: &'static str = "No primary service found";
const INCLUDED_SERVICE_ERROR: &'static str = "No included service found";
const CHARACTERISTIC_ERROR: &'static str = "No characteristic found";
const DESCRIPTOR_ERROR: &'static str = "No descriptor found";
const VALUE_ERROR: &'static str = "No characteristic or descriptor found with that id";
const SECURITY_ERROR: &'static str = "The operation is insecure";
const NETWORK_ERROR: &'static str = "A network error occurred";

// A transaction not completed within 30 seconds shall time out. Such a transaction shall be considered to have failed.
// https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=286439 (Vol. 3, page 480)
const MAXIMUM_TRANSACTION_TIME: u8 = 30;
Expand Down Expand Up @@ -73,7 +65,7 @@ macro_rules! get_adapter_or_return_error(
($bl_manager:expr, $sender:expr) => (
match $bl_manager.get_or_create_adapter() {
Some(adapter) => adapter,
None => return drop($sender.send(Err(String::from(ADAPTER_ERROR)))),
None => return drop($sender.send(Err(BluetoothError::Type(ADAPTER_ERROR.to_string())))),
}
);
);
Expand Down Expand Up @@ -453,7 +445,7 @@ impl BluetoothManager {
if let Some(address) = self.select_device(matched_devices) {
let device_id = match self.address_to_id.get(&address) {
Some(id) => id.clone(),
None => return drop(sender.send(Err(String::from(DEVICE_MATCH_ERROR)))),
None => return drop(sender.send(Err(BluetoothError::NotFound))),
};
let mut services = options.get_services_set();
if let Some(services_set) = self.allowed_services.get(&device_id) {
Expand All @@ -471,7 +463,7 @@ impl BluetoothManager {
return drop(sender.send(message));
}
}
return drop(sender.send(Err(String::from(DEVICE_MATCH_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}

fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender<BluetoothResult<bool>>) {
Expand All @@ -489,9 +481,9 @@ impl BluetoothManager {
false => thread::sleep(Duration::from_millis(CONNECTION_TIMEOUT_MS)),
}
}
return drop(sender.send(Err(String::from(NETWORK_ERROR))));
return drop(sender.send(Err(BluetoothError::Network)));
},
None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))),
None => return drop(sender.send(Err(BluetoothError::NotFound))),
}
}

Expand All @@ -510,9 +502,9 @@ impl BluetoothManager {
false => return drop(sender.send(Ok(false))),
}
}
return drop(sender.send(Err(String::from(NETWORK_ERROR))));
return drop(sender.send(Err(BluetoothError::Network)));
},
None => return drop(sender.send(Err(String::from(DEVICE_ERROR)))),
None => return drop(sender.send(Err(BluetoothError::NotFound))),
}
}

Expand All @@ -522,11 +514,11 @@ impl BluetoothManager {
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
let mut adapter = get_adapter_or_return_error!(self, sender);
if !self.allowed_services.get(&device_id).map_or(false, |s| s.contains(&uuid)) {
return drop(sender.send(Err(String::from(SECURITY_ERROR))));
return drop(sender.send(Err(BluetoothError::Security)));
}
let services = self.get_gatt_services_by_uuid(&mut adapter, &device_id, &uuid);
if services.is_empty() {
return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}
for service in services {
if service.is_primary().unwrap_or(false) {
Expand All @@ -539,7 +531,7 @@ impl BluetoothManager {
}
}
}
return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}

fn get_primary_services(&mut self,
Expand All @@ -550,14 +542,14 @@ impl BluetoothManager {
let services = match uuid {
Some(ref id) => {
if !self.allowed_services.get(&device_id).map_or(false, |s| s.contains(id)) {
return drop(sender.send(Err(String::from(SECURITY_ERROR))))
return drop(sender.send(Err(BluetoothError::Security)))
}
self.get_gatt_services_by_uuid(&mut adapter, &device_id, id)
},
None => self.get_and_cache_gatt_services(&mut adapter, &device_id),
};
if services.is_empty() {
return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}
let mut services_vec = vec!();
for service in services {
Expand All @@ -572,7 +564,7 @@ impl BluetoothManager {
}
}
if services_vec.is_empty() {
return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}

let _ = sender.send(Ok(services_vec));
Expand All @@ -584,11 +576,11 @@ impl BluetoothManager {
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
let mut adapter = match self.get_or_create_adapter() {
Some(a) => a,
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
None => return drop(sender.send(Err(BluetoothError::Type(ADAPTER_ERROR.to_string())))),
};
let primary_service = match self.get_gatt_service(&mut adapter, &service_id) {
Some(s) => s,
None => return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR)))),
None => return drop(sender.send(Err(BluetoothError::NotFound))),
};
let services = primary_service.get_includes().unwrap_or(vec!());
for service in services {
Expand All @@ -602,7 +594,7 @@ impl BluetoothManager {
}
}
}
return drop(sender.send(Err(String::from(INCLUDED_SERVICE_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}

fn get_included_services(&mut self,
Expand All @@ -611,11 +603,11 @@ impl BluetoothManager {
sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
let mut adapter = match self.get_or_create_adapter() {
Some(a) => a,
None => return drop(sender.send(Err(String::from(ADAPTER_ERROR)))),
None => return drop(sender.send(Err(BluetoothError::Type(ADAPTER_ERROR.to_string())))),
};
let primary_service = match self.get_gatt_service(&mut adapter, &service_id) {
Some(s) => s,
None => return drop(sender.send(Err(String::from(PRIMARY_SERVICE_ERROR)))),
None => return drop(sender.send(Err(BluetoothError::NotFound))),
};
let services = primary_service.get_includes().unwrap_or(vec!());
let mut services_vec = vec!();
Expand All @@ -632,7 +624,7 @@ impl BluetoothManager {
services_vec.retain(|ref s| s.uuid == uuid);
}
if services_vec.is_empty() {
return drop(sender.send(Err(String::from(INCLUDED_SERVICE_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}

let _ = sender.send(Ok(services_vec));
Expand All @@ -645,7 +637,7 @@ impl BluetoothManager {
let mut adapter = get_adapter_or_return_error!(self, sender);
let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid);
if characteristics.is_empty() {
return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}
for characteristic in characteristics {
if let Ok(uuid) = characteristic.get_uuid() {
Expand All @@ -666,7 +658,7 @@ impl BluetoothManager {
return drop(sender.send(message));
}
}
return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}

fn get_characteristics(&mut self,
Expand All @@ -679,7 +671,7 @@ impl BluetoothManager {
None => self.get_and_cache_gatt_characteristics(&mut adapter, &service_id),
};
if characteristics.is_empty() {
return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}
let mut characteristics_vec = vec!();
for characteristic in characteristics {
Expand All @@ -702,7 +694,7 @@ impl BluetoothManager {
}
}
if characteristics_vec.is_empty() {
return drop(sender.send(Err(String::from(CHARACTERISTIC_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}

let _ = sender.send(Ok(characteristics_vec));
Expand All @@ -715,7 +707,7 @@ impl BluetoothManager {
let mut adapter = get_adapter_or_return_error!(self, sender);
let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid);
if descriptors.is_empty() {
return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}
for descriptor in descriptors {
if let Ok(uuid) = descriptor.get_uuid() {
Expand All @@ -725,7 +717,7 @@ impl BluetoothManager {
})));
}
}
return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}

fn get_descriptors(&mut self,
Expand All @@ -738,7 +730,7 @@ impl BluetoothManager {
None => self.get_and_cache_gatt_descriptors(&mut adapter, &characteristic_id),
};
if descriptors.is_empty() {
return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}
let mut descriptors_vec = vec!();
for descriptor in descriptors {
Expand All @@ -750,7 +742,7 @@ impl BluetoothManager {
}
}
if descriptors_vec.is_empty() {
return drop(sender.send(Err(String::from(DESCRIPTOR_ERROR))));
return drop(sender.send(Err(BluetoothError::NotFound)));
}
let _ = sender.send(Ok(descriptors_vec));
}
Expand All @@ -763,7 +755,7 @@ impl BluetoothManager {
value = self.get_gatt_descriptor(&mut adapter, &id)
.map(|d| d.read_value().unwrap_or(vec![]));
}
let _ = sender.send(value.ok_or(String::from(VALUE_ERROR)));
let _ = sender.send(value.ok_or(BluetoothError::NotSupported));
}

fn write_value(&mut self, id: String, value: Vec<u8>, sender: IpcSender<BluetoothResult<bool>>) {
Expand All @@ -777,9 +769,9 @@ impl BluetoothManager {
let message = match result {
Some(v) => match v {
Ok(_) => Ok(true),
Err(e) => return drop(sender.send(Err(e.to_string()))),
Err(_) => return drop(sender.send(Err(BluetoothError::NotSupported))),
},
None => return drop(sender.send(Err(String::from(VALUE_ERROR)))),
None => return drop(sender.send(Err(BluetoothError::NotSupported))),
};
let _ = sender.send(message);
}
Expand Down
12 changes: 11 additions & 1 deletion components/net_traits/bluetooth_thread.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use bluetooth_scanfilter::RequestDeviceoptions;
use ipc_channel::ipc::IpcSender;

#[derive(Deserialize, Serialize)]
pub enum BluetoothError {
Type(String),
Network,
NotFound,
NotSupported,
Security,
}

#[derive(Deserialize, Serialize)]
pub struct BluetoothDeviceMsg {
// Bluetooth Device properties
Expand Down Expand Up @@ -51,7 +61,7 @@ pub type BluetoothCharacteristicsMsg = Vec<BluetoothCharacteristicMsg>;

pub type BluetoothDescriptorsMsg = Vec<BluetoothDescriptorMsg>;

pub type BluetoothResult<T> = Result<T, String>;
pub type BluetoothResult<T> = Result<T, BluetoothError>;

#[derive(Deserialize, Serialize)]
pub enum BluetoothMethodMsg {
Expand Down
18 changes: 15 additions & 3 deletions components/script/dom/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::clone::Clone;
use dom::bindings::codegen::Bindings::BluetoothBinding;
use dom::bindings::codegen::Bindings::BluetoothBinding::RequestDeviceOptions;
use dom::bindings::codegen::Bindings::BluetoothBinding::{BluetoothScanFilter, BluetoothMethods};
use dom::bindings::error::Error::{Security, Type};
use dom::bindings::error::Error::{self, Security, Type};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
Expand All @@ -19,7 +19,7 @@ use dom::bluetoothuuid::BluetoothUUID;
use ipc_channel::ipc::{self, IpcSender};
use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
use net_traits::bluetooth_scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
use net_traits::bluetooth_thread::BluetoothMethodMsg;
use net_traits::bluetooth_thread::{BluetoothError, BluetoothMethodMsg};

const FILTER_EMPTY_ERROR: &'static str = "'filters' member must be non - empty to find any devices.";
const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way.";
Expand Down Expand Up @@ -135,6 +135,18 @@ fn convert_request_device_options(options: &RequestDeviceOptions,
ServiceUUIDSequence::new(optional_services)))
}

impl From<BluetoothError> for Error {
fn from(error: BluetoothError) -> Self {
match error {
BluetoothError::Type(message) => Error::Type(message),
BluetoothError::Network => Error::Network,
BluetoothError::NotFound => Error::NotFound,
BluetoothError::NotSupported => Error::NotSupported,
BluetoothError::Security => Error::Security,
}
}
}

impl BluetoothMethods for Bluetooth {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice
fn RequestDevice(&self, option: &RequestDeviceOptions) -> Fallible<Root<BluetoothDevice>> {
Expand All @@ -154,7 +166,7 @@ impl BluetoothMethods for Bluetooth {
&ad_data))
},
Err(error) => {
Err(Type(error))
Err(Error::from(error))
},
}
}
Expand Down
10 changes: 5 additions & 5 deletions components/script/dom/bluetoothremotegattcharacteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::
BluetoothRemoteGATTCharacteristicMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::error::Error::{InvalidModification, Network, NotSupported, Security, Type};
use dom::bindings::error::Error::{self, InvalidModification, Network, NotSupported, Security};
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
Expand Down Expand Up @@ -115,7 +115,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
descriptor.instance_id))
},
Err(error) => {
Err(Type(error))
Err(Error::from(error))
},
}
}
Expand Down Expand Up @@ -147,7 +147,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
.collect())
},
Err(error) => {
Err(Type(error))
Err(Error::from(error))
},
}
}
Expand Down Expand Up @@ -177,7 +177,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
ByteString::new(val)
},
Err(error) => {
return Err(Type(error))
return Err(Error::from(error))
},
};
*self.value.borrow_mut() = Some(value.clone());
Expand All @@ -202,7 +202,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
match result {
Ok(_) => Ok(()),
Err(error) => {
Err(Type(error))
Err(Error::from(error))
},
}
}
Expand Down
Loading

0 comments on commit 05fdc62

Please sign in to comment.