Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 17 | //! Helper for converting Error types to what Binder expects |
| 18 | |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 19 | use binder::{Result as BinderResult, Status}; |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 20 | use log::warn; |
Victor Hsieh | 72c774c | 2021-11-18 15:52:28 -0800 | [diff] [blame] | 21 | use std::fmt::Debug; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 22 | |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 23 | /// Convert a Result<T, E> to BinderResult<T> to allow it to be returned from a binder RPC, |
| 24 | /// preserving the content as far as possible. |
| 25 | /// Also log the error if there is one. |
Victor Hsieh | 72c774c | 2021-11-18 15:52:28 -0800 | [diff] [blame] | 26 | pub fn to_binder_result<T, E: Debug>(result: Result<T, E>) -> BinderResult<T> { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 27 | result.map_err(|e| { |
| 28 | let message = format!("{:?}", e); |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 29 | warn!("Returning binder error: {}", &message); |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 30 | Status::new_service_specific_error_str(-1, Some(message)) |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 31 | }) |
| 32 | } |