|
1 | | -use std::sync::Arc; |
| 1 | +use std::{error::Error, sync::Arc}; |
2 | 2 |
|
3 | 3 | use futures_util::StreamExt; |
4 | 4 | use google_cloud_gax::conn::Environment; |
@@ -34,20 +34,38 @@ impl Backend { |
34 | 34 | back_tx: Sender<BackendMessage>, |
35 | 35 | front_rx: Receiver<FrontendMessage>, |
36 | 36 | emulator_project_id: Option<String>, |
37 | | - ) -> Self { |
| 37 | + ) -> Result<Self, Box<dyn Error>> { |
38 | 38 | let rt = Builder::new_multi_thread() |
39 | 39 | .worker_threads(4) |
40 | 40 | .enable_all() |
41 | 41 | .build() |
42 | 42 | .unwrap(); |
43 | 43 |
|
44 | | - let client = rt.block_on(async { create_client(emulator_project_id).await }); |
45 | | - |
46 | | - Self { |
47 | | - back_tx, |
48 | | - front_rx, |
49 | | - client: Arc::new(client), |
50 | | - rt, |
| 44 | + match rt.block_on(async { create_client(emulator_project_id).await }) { |
| 45 | + Ok(client) => { |
| 46 | + let back_tx_clone = back_tx.clone(); |
| 47 | + rt.spawn(async move { |
| 48 | + back_tx_clone |
| 49 | + .send(BackendMessage::ClientInitialised) |
| 50 | + .await |
| 51 | + .unwrap(); |
| 52 | + }); |
| 53 | + Ok(Self { |
| 54 | + back_tx, |
| 55 | + front_rx, |
| 56 | + client: Arc::new(client), |
| 57 | + rt, |
| 58 | + }) |
| 59 | + } |
| 60 | + Err(err) => { |
| 61 | + rt.spawn(async move { |
| 62 | + back_tx |
| 63 | + .send(BackendMessage::Error(BackendError::ClientInitFailed)) |
| 64 | + .await |
| 65 | + .unwrap(); |
| 66 | + }); |
| 67 | + Err(err) |
| 68 | + } |
51 | 69 | } |
52 | 70 | } |
53 | 71 |
|
@@ -217,14 +235,14 @@ impl Backend { |
217 | 235 | } |
218 | 236 | } |
219 | 237 |
|
220 | | -async fn create_client(emulator_project_id: Option<String>) -> Client { |
221 | | - let mut config = ClientConfig::default().with_auth().await.unwrap(); |
| 238 | +async fn create_client(emulator_project_id: Option<String>) -> Result<Client, Box<dyn Error>> { |
| 239 | + let mut config = ClientConfig::default().with_auth().await?; |
222 | 240 |
|
223 | 241 | if let (Environment::Emulator(_), Some(emulator_project_id)) = |
224 | 242 | (&config.environment, emulator_project_id) |
225 | 243 | { |
226 | 244 | config.project_id = Some(emulator_project_id); |
227 | 245 | } |
228 | 246 |
|
229 | | - Client::new(config).await.unwrap() |
| 247 | + Ok(Client::new(config).await?) |
230 | 248 | } |
0 commit comments