From 27ad1437a1af0b784a0c6dde91a936e6f55d0782 Mon Sep 17 00:00:00 2001 From: zakorgy Date: Wed, 6 Apr 2016 16:31:29 +0200 Subject: [PATCH] Error handling --- .../dom/bluetoothremotegattcharacteristic.rs | 15 ++---- .../dom/bluetoothremotegattdescriptor.rs | 17 +++--- .../script/dom/bluetoothremotegattserver.rs | 52 ++++++++----------- .../script/dom/bluetoothremotegattservice.rs | 37 ++++++------- .../BluetoothRemoteGATTCharacteristic.webidl | 1 + .../BluetoothRemoteGATTDescriptor.webidl | 1 + .../webidls/BluetoothRemoteGATTServer.webidl | 8 ++- .../webidls/BluetoothRemoteGATTService.webidl | 6 ++- 8 files changed, 61 insertions(+), 76 deletions(-) diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index db487bb70bc3..6d3bc5fe4f04 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -188,10 +188,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris }, BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - None - }, + } => return Err(Type(error)), _ => unreachable!() }; *self.value.borrow_mut() = value; @@ -200,19 +197,17 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue - fn WriteValue(&self, value: Vec) { + fn WriteValue(&self, value: Vec) -> Fallible<()> { let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); let result = receiver.recv().unwrap(); match result { - BluetoothObjectMsg::BluetoothWriteValue => (), + BluetoothObjectMsg::BluetoothWriteValue => Ok(()), BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - }, + } => Err(Type(error)), _ => unreachable!() - }; + } } } diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index 7b55e1d962e9..9a48ee8395ff 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -10,7 +10,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding::BluetoothRemoteGATTDescriptorMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; -use dom::bindings::error::Error::Network; +use dom::bindings::error::Error::{Type, Network}; use dom::bindings::error::Fallible; use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; @@ -103,10 +103,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { }, BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - None - }, + } => return Err(Type(error)), _ => unreachable!() }; *self.value.borrow_mut() = value; @@ -115,19 +112,17 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue - fn WriteValue(&self, value: Vec) { + fn WriteValue(&self, value: Vec) -> Fallible<()> { let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); let result = receiver.recv().unwrap(); match result { - BluetoothObjectMsg::BluetoothWriteValue => (), + BluetoothObjectMsg::BluetoothWriteValue => Ok(()), BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - }, + } => Err(Type(error)), _ => unreachable!() - }; + } } } diff --git a/components/script/dom/bluetoothremotegattserver.rs b/components/script/dom/bluetoothremotegattserver.rs index e3b0e5bd8c91..a52482f2106c 100644 --- a/components/script/dom/bluetoothremotegattserver.rs +++ b/components/script/dom/bluetoothremotegattserver.rs @@ -6,6 +6,8 @@ use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMet use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong; +use dom::bindings::error::Error::Type; +use dom::bindings::error::Fallible; use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; @@ -60,29 +62,28 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect - fn Connect(&self) -> Root { + fn Connect(&self) -> Fallible> { let (sender, receiver) = ipc::channel().unwrap(); - self.get_bluetooth_thread().send( - BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap(); + self.get_bluetooth_thread() + .send(BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)) + .unwrap(); let server = receiver.recv().unwrap(); match server { BluetoothObjectMsg::BluetoothServer { connected } => { self.connected.set(connected); + Ok(Root::from_ref(self)) }, BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - }, + } => Err(Type(error)), _ => unreachable!() } - Root::from_ref(self) } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect - fn Disconnect(&self) { + fn Disconnect(&self) -> Fallible<()>{ let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap(); @@ -92,24 +93,20 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { connected } => { self.connected.set(connected); + Ok(()) }, BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - }, + } => Err(Type(error)), _ => unreachable!() } } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice - fn GetPrimaryService(&self, service: StringOrUnsignedLong) -> Option> { + fn GetPrimaryService(&self, service: StringOrUnsignedLong) -> Fallible> { let uuid: String = match BluetoothUUID::GetService(self.global().r(), service.clone()) { Ok(domstring) => domstring.to_string(), - Err(_) => { - println!("No UUID provided!"); - return None; - }, + Err(error) => return Err(error), }; let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( @@ -121,7 +118,7 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { is_primary, instance_id, } => { - Some(BluetoothRemoteGATTService::new(self.global().r(), + Ok(BluetoothRemoteGATTService::new(self.global().r(), &self.device.get(), DOMString::from(uuid), is_primary, @@ -129,28 +126,26 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { }, BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - None - }, + } => Err(Type(error)), _ => unreachable!(), } } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices fn GetPrimaryServices(&self, service: Option) - -> Option>> { + -> Fallible>> { let uuid: Option = match service { Some(s) => match BluetoothUUID::GetService(self.global().r(), s.clone()) { Ok(domstring) => Some(domstring.to_string()), - Err(_) => None, + Err(error) => return Err(error), }, None => None, }; let mut services: Vec> = vec!(); let (sender, receiver) = ipc::channel().unwrap(); - self.get_bluetooth_thread().send( - BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap(); + self.get_bluetooth_thread() + .send(BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)) + .unwrap(); let services_vec = receiver.recv().unwrap(); match services_vec { BluetoothObjectMsg::BluetoothServices { @@ -172,14 +167,11 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer { _ => unreachable!(), } } - Some(services) + Ok(services) }, BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - None - }, + } => Err(Type(error)), _ => unreachable!(), } } diff --git a/components/script/dom/bluetoothremotegattservice.rs b/components/script/dom/bluetoothremotegattservice.rs index 8d4afe48ed3f..de98f887f0c9 100644 --- a/components/script/dom/bluetoothremotegattservice.rs +++ b/components/script/dom/bluetoothremotegattservice.rs @@ -5,6 +5,8 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong; +use dom::bindings::error::Error::Type; +use dom::bindings::error::Fallible; use dom::bindings::global::GlobalRef; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; @@ -84,18 +86,16 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic fn GetCharacteristic(&self, characteristic: StringOrUnsignedLong) - -> Option> { + -> Fallible> { let uuid: String = match BluetoothUUID::GetCharacteristic(self.global().r(), characteristic.clone()) { Ok(domstring) => domstring.to_string(), - Err(_) => { - println!("No UUID provided!"); - return None; - }, + Err(error) => return Err(error), }; let (sender, receiver) = ipc::channel().unwrap(); - self.get_bluetooth_thread().send( - BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap(); + self.get_bluetooth_thread() + .send(BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)) + .unwrap(); let characteristic = receiver.recv().unwrap(); match characteristic { BluetoothObjectMsg::BluetoothCharacteristic { @@ -121,7 +121,7 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { authenticated_signed_writes, reliable_write, writable_auxiliaries); - Some(BluetoothRemoteGATTCharacteristic::new(self.global().r(), + Ok(BluetoothRemoteGATTCharacteristic::new(self.global().r(), &self, DOMString::from(uuid), properties, @@ -129,28 +129,26 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { }, BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - None - }, + } => return Err(Type(error)), _ => unreachable!(), } } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics fn GetCharacteristics(&self, characteristic: Option) - -> Option>> { + -> Fallible>> { let uuid: Option = match characteristic { Some(c) => match BluetoothUUID::GetCharacteristic(self.global().r(), c.clone()) { Ok(domstring) => Some(domstring.to_string()), - Err(_) => None, + Err(error) => return Err(error), }, None => None, }; let mut characteristics: Vec> = vec!(); let (sender, receiver) = ipc::channel().unwrap(); - self.get_bluetooth_thread().send( - BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)).unwrap(); + self.get_bluetooth_thread() + .send(BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)) + .unwrap(); let characteristics_vec = receiver.recv().unwrap(); match characteristics_vec { BluetoothObjectMsg::BluetoothCharacteristics { @@ -192,14 +190,11 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService { _ => unreachable!(), } } - Some(characteristics) + Ok(characteristics) }, BluetoothObjectMsg::Error { error - } => { - println!("{}", error); - None - }, + } => return Err(Type(error)), _ => unreachable!(), } } diff --git a/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl b/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl index 3024b3bc7f22..294e00e1b854 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTCharacteristic.webidl @@ -20,6 +20,7 @@ interface BluetoothRemoteGATTCharacteristic { [Throws] ByteString readValue(); //Promise readValue(); + [Throws] void writeValue(sequence value); //Promise writeValue(BufferSource value); //Promise startNotifications(); diff --git a/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl b/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl index 77becb830bff..853054f6e3a4 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTDescriptor.webidl @@ -12,6 +12,7 @@ interface BluetoothRemoteGATTDescriptor { [Throws] ByteString readValue(); //Promise readValue(); + [Throws] void writeValue(sequence value); //Promise writeValue(BufferSource value); }; diff --git a/components/script/dom/webidls/BluetoothRemoteGATTServer.webidl b/components/script/dom/webidls/BluetoothRemoteGATTServer.webidl index ac6af9649b1f..835a6444103d 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTServer.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTServer.webidl @@ -8,10 +8,14 @@ interface BluetoothRemoteGATTServer { readonly attribute BluetoothDevice device; readonly attribute boolean connected; + [Throws] BluetoothRemoteGATTServer connect(); + [Throws] void disconnect(); - BluetoothRemoteGATTService? getPrimaryService((DOMString or unsigned long) service); - sequence? getPrimaryServices(optional (DOMString or unsigned long) service); + [Throws] + BluetoothRemoteGATTService getPrimaryService((DOMString or unsigned long) service); + [Throws] + sequence getPrimaryServices(optional (DOMString or unsigned long) service); //Promise getPrimaryService(BluetoothServiceUUID service); //Promise>getPrimaryServices(optional BluetoothServiceUUID service); //Promise connect(); diff --git a/components/script/dom/webidls/BluetoothRemoteGATTService.webidl b/components/script/dom/webidls/BluetoothRemoteGATTService.webidl index 1cc34ba41a3b..e6673f693977 100644 --- a/components/script/dom/webidls/BluetoothRemoteGATTService.webidl +++ b/components/script/dom/webidls/BluetoothRemoteGATTService.webidl @@ -9,8 +9,10 @@ interface BluetoothRemoteGATTService { readonly attribute BluetoothDevice device; readonly attribute DOMString uuid; readonly attribute boolean isPrimary; - BluetoothRemoteGATTCharacteristic? getCharacteristic((DOMString or unsigned long) characteristic); - sequence? getCharacteristics + [Throws] + BluetoothRemoteGATTCharacteristic getCharacteristic((DOMString or unsigned long) characteristic); + [Throws] + sequence getCharacteristics (optional (DOMString or unsigned long) characteristic); //PromisegetCharacteristic(BluetoothCharacteristicUUID characteristic); //Promise>