Skip to content

Commit e550880

Browse files
committed
Error refactor
1 parent 8121d3a commit e550880

File tree

8 files changed

+221
-442
lines changed

8 files changed

+221
-442
lines changed

components/net/bluetooth_thread.rs

Lines changed: 79 additions & 233 deletions
Large diffs are not rendered by default.

components/net_traits/bluetooth_thread.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,47 +64,49 @@ pub type BluetoothDescriptorsMsg = Vec<BluetoothDescriptorMsg>;
6464

6565
pub type BluetoothResult<T> = Result<T, BluetoothError>;
6666

67+
pub type BluetoothResponseResult = Result<BluetoothResponse, BluetoothError>;
68+
6769
#[derive(Deserialize, Serialize)]
6870
pub enum BluetoothRequest {
69-
RequestDevice(RequestDeviceoptions, IpcSender<BluetoothResponse>),
70-
GATTServerConnect(String, IpcSender<BluetoothResponse>),
71+
RequestDevice(RequestDeviceoptions, IpcSender<BluetoothResponseResult>),
72+
GATTServerConnect(String, IpcSender<BluetoothResponseResult>),
7173
GATTServerDisconnect(String, IpcSender<BluetoothResult<bool>>),
72-
GetPrimaryService(String, String, IpcSender<BluetoothResponse>),
73-
GetPrimaryServices(String, Option<String>, IpcSender<BluetoothResponse>),
74-
GetIncludedService(String, String, IpcSender<BluetoothResponse>),
75-
GetIncludedServices(String, Option<String>, IpcSender<BluetoothResponse>),
76-
GetCharacteristic(String, String, IpcSender<BluetoothResponse>),
77-
GetCharacteristics(String, Option<String>, IpcSender<BluetoothResponse>),
78-
GetDescriptor(String, String, IpcSender<BluetoothResponse>),
79-
GetDescriptors(String, Option<String>, IpcSender<BluetoothResponse>),
80-
ReadValue(String, IpcSender<BluetoothResponse>),
81-
WriteValue(String, Vec<u8>, IpcSender<BluetoothResponse>),
74+
GetPrimaryService(String, String, IpcSender<BluetoothResponseResult>),
75+
GetPrimaryServices(String, Option<String>, IpcSender<BluetoothResponseResult>),
76+
GetIncludedService(String, String, IpcSender<BluetoothResponseResult>),
77+
GetIncludedServices(String, Option<String>, IpcSender<BluetoothResponseResult>),
78+
GetCharacteristic(String, String, IpcSender<BluetoothResponseResult>),
79+
GetCharacteristics(String, Option<String>, IpcSender<BluetoothResponseResult>),
80+
GetDescriptor(String, String, IpcSender<BluetoothResponseResult>),
81+
GetDescriptors(String, Option<String>, IpcSender<BluetoothResponseResult>),
82+
ReadValue(String, IpcSender<BluetoothResponseResult>),
83+
WriteValue(String, Vec<u8>, IpcSender<BluetoothResponseResult>),
8284
Test(String, IpcSender<BluetoothResult<()>>),
8385
Exit,
8486
}
8587

8688
#[derive(Deserialize, Serialize)]
8789
pub enum BluetoothResponse {
88-
RequestDevice(BluetoothResult<BluetoothDeviceMsg>),
89-
GATTServerConnect(BluetoothResult<bool>),
90+
RequestDevice(BluetoothDeviceMsg),
91+
GATTServerConnect(bool),
9092
//GATTServerDisconnect(bool),
91-
GetPrimaryService(BluetoothResult<BluetoothServiceMsg>),
92-
GetPrimaryServices(BluetoothResult<BluetoothServicesMsg>),
93-
GetIncludedService(BluetoothResult<BluetoothServiceMsg>),
94-
GetIncludedServices(BluetoothResult<BluetoothServicesMsg>),
95-
GetCharacteristic(BluetoothResult<BluetoothCharacteristicMsg>),
96-
GetCharacteristics(BluetoothResult<BluetoothCharacteristicsMsg>),
97-
GetDescriptor(BluetoothResult<BluetoothDescriptorMsg>),
98-
GetDescriptors(BluetoothResult<BluetoothDescriptorsMsg>),
99-
ReadValue(BluetoothResult<Vec<u8>>),
100-
WriteValue(BluetoothResult<()>),
93+
GetPrimaryService(BluetoothServiceMsg),
94+
GetPrimaryServices(BluetoothServicesMsg),
95+
GetIncludedService(BluetoothServiceMsg),
96+
GetIncludedServices(BluetoothServicesMsg),
97+
GetCharacteristic(BluetoothCharacteristicMsg),
98+
GetCharacteristics(BluetoothCharacteristicsMsg),
99+
GetDescriptor(BluetoothDescriptorMsg),
100+
GetDescriptors(BluetoothDescriptorsMsg),
101+
ReadValue(Vec<u8>),
102+
WriteValue(()),
101103
}
102104

103105
pub trait BluetoothResponseListener {
104-
fn response(&mut self, response: BluetoothResponse);
106+
fn response(&mut self, response: BluetoothResponseResult);
105107
}
106108

107-
impl<T: BluetoothResponseListener> Action<T> for BluetoothResponse {
109+
impl<T: BluetoothResponseListener> Action<T> for BluetoothResponseResult {
108110
/// Execute the default action on a provided listener.
109111
fn process(self, listener: &mut T) {
110112
listener.response(self)

components/script/dom/bluetooth.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use ipc_channel::router::ROUTER;
2222
use js::jsapi::{JSAutoCompartment, JSContext};
2323
use net_traits::bluetooth_scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
2424
use net_traits::bluetooth_scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
25-
use net_traits::bluetooth_thread::{BluetoothError, BluetoothRequest, BluetoothResponseListener};
26-
use net_traits::bluetooth_thread::BluetoothResponse;
25+
use net_traits::bluetooth_thread::{BluetoothError, BluetoothRequest};
26+
use net_traits::bluetooth_thread::{BluetoothResponse, BluetoothResponseListener, BluetoothResponseResult};
2727
use network_listener::{NetworkListener, PreInvoke};
2828
use std::rc::Rc;
2929
use std::sync::{Arc, Mutex};
@@ -57,14 +57,17 @@ impl<Listener: AsyncBluetoothListener + Reflectable> PreInvoke for BluetoothCont
5757

5858
impl<Listener: AsyncBluetoothListener + Reflectable> BluetoothResponseListener for BluetoothContext<Listener> {
5959
#[allow(unrooted_must_root)]
60-
fn response(&mut self, response: BluetoothResponse) {
60+
fn response(&mut self, response: BluetoothResponseResult) {
6161
let promise = self.promise.take().expect("bt promise is missing").root();
6262
let promise_cx = promise.global().r().get_cx();
6363

6464
// JSAutoCompartment needs to be manually made.
6565
// Otherwise, Servo will crash.
6666
let _ac = JSAutoCompartment::new(promise_cx, promise.reflector().get_jsobject().get());
67-
self.receiver.root().handle_response(response, promise_cx, &promise);
67+
match response {
68+
Ok(response) => self.receiver.root().handle_response(response, promise_cx, &promise),
69+
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
70+
}
6871
}
6972
}
7073

@@ -119,9 +122,8 @@ impl Bluetooth {
119122
}
120123

121124
pub fn response_async<T: AsyncBluetoothListener + Reflectable + 'static>(
122-
//chan: Box<ScriptChan + Send>,
123125
promise: &Rc<Promise>,
124-
receiver: Trusted<T>) -> IpcSender<BluetoothResponse> {
126+
receiver: Trusted<T>) -> IpcSender<BluetoothResponseResult> {
125127
let (action_sender, action_receiver) = ipc::channel().unwrap();
126128
let chan = receiver.root().global().r().networking_task_source();
127129
let context = Arc::new(Mutex::new(BluetoothContext {
@@ -130,7 +132,6 @@ pub fn response_async<T: AsyncBluetoothListener + Reflectable + 'static>(
130132
}));
131133
let listener = NetworkListener {
132134
context: context,
133-
//script_chan: receiver.root().global().r().networking_task_source(),
134135
script_chan: chan,
135136
wrapper: None,
136137
};
@@ -325,21 +326,16 @@ impl BluetoothMethods for Bluetooth {
325326
impl AsyncBluetoothListener for Bluetooth {
326327
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
327328
match response {
328-
BluetoothResponse::RequestDevice(result) => {
329-
match result {
330-
Ok(device) => {
331-
let ad_data = BluetoothAdvertisingData::new(self.global().r(),
332-
device.appearance,
333-
device.tx_power,
334-
device.rssi);
335-
let dev = BluetoothDevice::new(self.global().r(),
336-
DOMString::from(device.id),
337-
device.name.map(DOMString::from),
338-
&ad_data);
339-
promise.resolve_native(promise_cx, &dev);
340-
},
341-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
342-
}
329+
BluetoothResponse::RequestDevice(device) => {
330+
let ad_data = BluetoothAdvertisingData::new(self.global().r(),
331+
device.appearance,
332+
device.tx_power,
333+
device.rssi);
334+
let dev = BluetoothDevice::new(self.global().r(),
335+
DOMString::from(device.id),
336+
device.name.map(DOMString::from),
337+
&ad_data);
338+
promise.resolve_native(promise_cx, &dev);
343339
},
344340
_ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
345341
}

components/script/dom/bluetoothremotegattcharacteristic.rs

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -214,51 +214,31 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
214214
impl AsyncBluetoothListener for BluetoothRemoteGATTCharacteristic {
215215
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
216216
match response {
217-
BluetoothResponse::GetDescriptor(result) => {
218-
match result {
219-
Ok(descriptor) => {
220-
let d =
221-
BluetoothRemoteGATTDescriptor::new(self.global().r(),
222-
&self,
223-
DOMString::from(descriptor.uuid),
224-
descriptor.instance_id);
225-
promise.resolve_native(promise_cx, &d);
226-
},
227-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
228-
}
217+
BluetoothResponse::GetDescriptor(descriptor) => {
218+
let d =
219+
BluetoothRemoteGATTDescriptor::new(self.global().r(),
220+
&self,
221+
DOMString::from(descriptor.uuid),
222+
descriptor.instance_id);
223+
promise.resolve_native(promise_cx, &d);
229224
},
230-
BluetoothResponse::GetDescriptors(result) => {
231-
match result {
232-
Ok(descriptors_vec) => {
233-
let d: Vec<Root<BluetoothRemoteGATTDescriptor>> =
234-
descriptors_vec.into_iter()
235-
.map(|desc|
236-
BluetoothRemoteGATTDescriptor::new(self.global().r(),
237-
&self,
238-
DOMString::from(desc.uuid),
239-
desc.instance_id))
240-
.collect();
241-
promise.resolve_native(promise_cx, &d);
242-
},
243-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
244-
}
225+
BluetoothResponse::GetDescriptors(descriptors_vec) => {
226+
let d: Vec<Root<BluetoothRemoteGATTDescriptor>> =
227+
descriptors_vec.into_iter()
228+
.map(|desc|
229+
BluetoothRemoteGATTDescriptor::new(self.global().r(),
230+
&self,
231+
DOMString::from(desc.uuid),
232+
desc.instance_id))
233+
.collect();
234+
promise.resolve_native(promise_cx, &d);
245235
},
246236
BluetoothResponse::ReadValue(result) => {
247-
match result {
248-
Ok(result) => {
249-
let value = ByteString::new(result);
250-
*self.value.borrow_mut() = Some(value.clone());
251-
promise.resolve_native(promise_cx, &value);
252-
},
253-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
254-
}
255-
},
256-
BluetoothResponse::WriteValue(result) => {
257-
match result {
258-
Ok(result) => promise.resolve_native(promise_cx, &result),
259-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
260-
}
237+
let value = ByteString::new(result);
238+
*self.value.borrow_mut() = Some(value.clone());
239+
promise.resolve_native(promise_cx, &value);
261240
},
241+
BluetoothResponse::WriteValue(result) => promise.resolve_native(promise_cx, &result),
262242
_ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
263243
}
264244
}

components/script/dom/bluetoothremotegattdescriptor.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,11 @@ impl AsyncBluetoothListener for BluetoothRemoteGATTDescriptor {
135135
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
136136
match response {
137137
BluetoothResponse::ReadValue(result) => {
138-
match result {
139-
Ok(result) => {
140-
let value = ByteString::new(result);
141-
*self.value.borrow_mut() = Some(value.clone());
142-
promise.resolve_native(promise_cx, &value);
143-
},
144-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
145-
}
146-
},
147-
BluetoothResponse::WriteValue(result) => {
148-
match result {
149-
Ok(result) => promise.resolve_native(promise_cx, &result),
150-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
151-
}
138+
let value = ByteString::new(result);
139+
*self.value.borrow_mut() = Some(value.clone());
140+
promise.resolve_native(promise_cx, &value);
152141
},
142+
BluetoothResponse::WriteValue(result) => promise.resolve_native(promise_cx, &result),
153143
_ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
154144
}
155145
}

components/script/dom/bluetoothremotegattserver.rs

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -145,43 +145,28 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
145145
impl AsyncBluetoothListener for BluetoothRemoteGATTServer {
146146
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
147147
match response {
148-
BluetoothResponse::GATTServerConnect(result) => {
149-
match result {
150-
Ok(connected) => {
151-
self.connected.set(connected);
152-
promise.resolve_native(promise_cx, self);
153-
},
154-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
155-
}
148+
BluetoothResponse::GATTServerConnect(connected) => {
149+
self.connected.set(connected);
150+
promise.resolve_native(promise_cx, self);
156151
},
157-
BluetoothResponse::GetPrimaryService(result) => {
158-
match result {
159-
Ok(service) => {
160-
let s = BluetoothRemoteGATTService::new(self.global().r(),
161-
&self.device.get(),
162-
DOMString::from(service.uuid),
163-
service.is_primary,
164-
service.instance_id);
165-
promise.resolve_native(promise_cx, &s);
166-
},
167-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
168-
}
152+
BluetoothResponse::GetPrimaryService(service) => {
153+
let s = BluetoothRemoteGATTService::new(self.global().r(),
154+
&self.device.get(),
155+
DOMString::from(service.uuid),
156+
service.is_primary,
157+
service.instance_id);
158+
promise.resolve_native(promise_cx, &s);
169159
},
170-
BluetoothResponse::GetPrimaryServices(result) => {
171-
match result {
172-
Ok(services_vec) => {
173-
let s: Vec<Root<BluetoothRemoteGATTService>> =
174-
services_vec.into_iter()
175-
.map(|service| BluetoothRemoteGATTService::new(self.global().r(),
176-
&self.device.get(),
177-
DOMString::from(service.uuid),
178-
service.is_primary,
179-
service.instance_id))
180-
.collect();
181-
promise.resolve_native(promise_cx, &s);
182-
},
183-
Err(error) => promise.reject_error(promise_cx, Error::from(error)),
184-
}
160+
BluetoothResponse::GetPrimaryServices(services_vec) => {
161+
let s: Vec<Root<BluetoothRemoteGATTService>> =
162+
services_vec.into_iter()
163+
.map(|service| BluetoothRemoteGATTService::new(self.global().r(),
164+
&self.device.get(),
165+
DOMString::from(service.uuid),
166+
service.is_primary,
167+
service.instance_id))
168+
.collect();
169+
promise.resolve_native(promise_cx, &s);
185170
},
186171
_ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
187172
}

0 commit comments

Comments
 (0)