blob: 6bd3957a2a6fc4d4f100643a9a59ba90c1a6a82e [file] [log] [blame]
Alan Stokes9ca14ca2021-10-20 14:25:57 +01001/*
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 Stokes126fd512021-12-16 15:00:01 +000017//! Helper for converting Error types to what Binder expects
18
Alan Stokes9ca14ca2021-10-20 14:25:57 +010019use anyhow::Result;
Alan Stokes126fd512021-12-16 15:00:01 +000020use binder::public_api::{ExceptionCode, Result as BinderResult};
21use binder_common::new_binder_exception;
22use log::warn;
Victor Hsieh72c774c2021-11-18 15:52:28 -080023use std::fmt::Debug;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010024
Alan Stokes126fd512021-12-16 15:00:01 +000025/// Convert a Result<T, E> to BinderResult<T> to allow it to be returned from a binder RPC,
26/// preserving the content as far as possible.
27/// Also log the error if there is one.
Victor Hsieh72c774c2021-11-18 15:52:28 -080028pub fn to_binder_result<T, E: Debug>(result: Result<T, E>) -> BinderResult<T> {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010029 result.map_err(|e| {
30 let message = format!("{:?}", e);
Alan Stokes126fd512021-12-16 15:00:01 +000031 warn!("Returning binder error: {}", &message);
32 new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, message)
Alan Stokes9ca14ca2021-10-20 14:25:57 +010033 })
34}