1 /*
2  * Copyright (C) 2024 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 #pragma once
17 
18 #include <binder/Status.h>
19 #include <error/expected_utils.h>
20 #include <utils/Errors.h>
21 
22 namespace android {
23 namespace error {
24 
25 /**
26  * A convenience short-hand for base::expected, where the error type is a binder::Status, for use
27  * when implementing binder services.
28  * Clients need to link against libbinder, since this library is header only.
29  */
30 template <typename T>
31 using BinderResult = base::expected<T, binder::Status>;
32 
unexpectedExceptionCode(int32_t exceptionCode,const char * s)33 inline base::unexpected<binder::Status> unexpectedExceptionCode(int32_t exceptionCode,
34                                                                 const char* s) {
35     return base::unexpected{binder::Status::fromExceptionCode(exceptionCode, s)};
36 }
37 
unexpectedServiceException(int32_t serviceSpecificCode,const char * s)38 inline base::unexpected<binder::Status> unexpectedServiceException(int32_t serviceSpecificCode,
39                                                                    const char* s) {
40     return base::unexpected{binder::Status::fromServiceSpecificError(serviceSpecificCode, s)};
41 }
42 
43 }  // namespace error
44 }  // namespace android
45 
errorToString(const::android::binder::Status & status)46 inline std::string errorToString(const ::android::binder::Status& status) {
47     return std::string{status.toString8().c_str()};
48 }
49