diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-09-11 17:06:22 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-09-11 17:15:48 +0200 |
| commit | c63c6d6f3be352008a149f39f1aabdbd5d88bafd (patch) | |
| tree | 3e5d216e6bdf81f81d1601cf03e27819342b8bad | |
| parent | 0a755887b9a3c26a71a19578db3a78e330874020 (diff) | |
| download | mullvadvpn-c63c6d6f3be352008a149f39f1aabdbd5d88bafd.tar.xz mullvadvpn-c63c6d6f3be352008a149f39f1aabdbd5d88bafd.zip | |
Extract error mapping code to method
| -rw-r--r-- | mullvad-daemon/src/management_interface.rs | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index ade56f3abe..bca4f68f13 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -247,6 +247,24 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterface<T> { .map_err(|_| Error::internal_error()) ) } + + /// Converts the given error to an error that can be given to the caller of the API. + /// Will let any actual RPC error through as is, any other error is changed to an internal + /// error. + fn map_rpc_error(error: jsonrpc_client_core::Error) -> Error { + match error.kind() { + &jsonrpc_client_core::ErrorKind::JsonRpcError(ref rpc_error) => { + // We have to manually copy the error since we have different + // versions of the jsonrpc_core library at the moment. + Error { + code: ErrorCode::from(rpc_error.code.code()), + message: rpc_error.message.clone(), + data: rpc_error.data.clone(), + } + } + _ => Error::internal_error(), + } + } } impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for ManagementInterface<T> { @@ -255,28 +273,21 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for Managem fn get_account_data(&self, account_token: AccountToken) -> BoxFuture<AccountData, Error> { trace!("get_account_data"); let (tx, rx) = sync::oneshot::channel(); - let future = - self.send_command_to_daemon(TunnelCommand::GetAccountData(tx, account_token)) - .and_then(|_| rx.map_err(|_| Error::internal_error())) - .and_then( - |rpc_future| { - rpc_future.map_err(|e: jsonrpc_client_core::Error| { - error!("Unable to get account data from master: {}", e.display_chain()); - match e.kind() { - &jsonrpc_client_core::ErrorKind::JsonRpcError(ref rpc_error) => { - // We have to manually copy the error since we have different - // versions of the jsonrpc_core library at the moment. - Error { - code: ErrorCode::from(rpc_error.code.code()), - message: rpc_error.message.clone(), - data: rpc_error.data.clone(), - } - } - _ => Error::internal_error(), - } - }) - }, - ); + let future = self.send_command_to_daemon(TunnelCommand::GetAccountData(tx, account_token)) + .and_then(|_| rx.map_err(|_| Error::internal_error())) + .and_then( + |rpc_future| { + rpc_future.map_err( + |error: jsonrpc_client_core::Error| { + error!( + "Unable to get account data from master: {}", + error.display_chain() + ); + Self::map_rpc_error(error) + }, + ) + }, + ); Box::new(future) } |
