1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMON_ERROR_DEFINE_H 17 #define COMMON_ERROR_DEFINE_H 18 19 #include <string> 20 #include <vector> 21 22 #include "json_builder.h" 23 #include "json_utils.h" 24 25 namespace OHOS::UpdateEngine { 26 #define CAST_INT(enumClass) (static_cast<int32_t>(enumClass)) 27 #define CALL_RESULT_TO_IPC_RESULT(callResult) ((callResult) + CALL_RESULT_OFFSET) 28 constexpr int32_t COMPONENT_ERR = 11500000; 29 constexpr int CALL_RESULT_OFFSET = 2000; 30 enum class CallResult { 31 // 通用错误码 32 APP_NOT_GRANTED = 201, 33 NOT_SYSTEM_APP = 202, 34 PARAM_ERR = 401, 35 UN_SUPPORT = 801, 36 37 // 模块内错误码 38 SUCCESS = 0, 39 FAIL = 100, 40 DEV_UPG_INFO_ERR = 102, 41 FORBIDDEN = 103, 42 IPC_ERR = 104, 43 TIME_OUT = 402, 44 DB_ERROR = 501, 45 IO_ERROR = 502, 46 NET_ERROR = 503 47 }; 48 49 constexpr int32_t INT_CALL_SUCCESS = CAST_INT(CallResult::SUCCESS); 50 constexpr int32_t INT_CALL_FAIL = CAST_INT(CallResult::FAIL); 51 constexpr int32_t INT_UN_SUPPORT = CAST_INT(CallResult::UN_SUPPORT); 52 constexpr int32_t INT_FORBIDDEN = CAST_INT(CallResult::FORBIDDEN); 53 constexpr int32_t INT_CALL_IPC_ERR = CAST_INT(CallResult::IPC_ERR); 54 constexpr int32_t INT_APP_NOT_GRANTED = CAST_INT(CallResult::APP_NOT_GRANTED); 55 constexpr int32_t INT_NOT_SYSTEM_APP = CAST_INT(CallResult::NOT_SYSTEM_APP); 56 constexpr int32_t INT_PARAM_ERR = CAST_INT(CallResult::PARAM_ERR); 57 constexpr int32_t INT_DEV_UPG_INFO_ERR = CAST_INT(CallResult::DEV_UPG_INFO_ERR); 58 constexpr int32_t INT_TIME_OUT = CAST_INT(CallResult::TIME_OUT); 59 constexpr int32_t INT_DB_ERROR = CAST_INT(CallResult::DB_ERROR); 60 constexpr int32_t INT_IO_ERROR = CAST_INT(CallResult::IO_ERROR); 61 constexpr int32_t INT_NET_ERROR = CAST_INT(CallResult::NET_ERROR); 62 63 struct ErrorMessage { 64 int32_t errorCode = 0; 65 std::string errorMessage; 66 to_jsonErrorMessage67 friend void to_json(nlohmann::json &jsonObj, const ErrorMessage &message) 68 { 69 jsonObj["errorCode"] = message.errorCode; 70 jsonObj["errorMessage"] = message.errorMessage; 71 } 72 from_jsonErrorMessage73 friend void from_json(const nlohmann::json &jsonObj, ErrorMessage &message) 74 { 75 JsonUtils::GetValueAndSetTo(jsonObj, "errorCode", message.errorCode); 76 JsonUtils::GetValueAndSetTo(jsonObj, "errorMessage", message.errorMessage); 77 } 78 GetJsonBuilderErrorMessage79 JsonBuilder GetJsonBuilder() 80 { 81 return JsonBuilder() 82 .Append("{") 83 .Append("errorCode", errorCode) 84 .Append("errorMessage", errorMessage) 85 .Append("}"); 86 } 87 }; 88 89 struct BusinessError { 90 std::string message; 91 CallResult errorNum = CallResult::SUCCESS; 92 std::vector<ErrorMessage> data; 93 BuildBusinessError94 BusinessError &Build(CallResult callResult, const std::string &msg) 95 { 96 errorNum = callResult; 97 message = msg; 98 return *this; 99 } 100 AddErrorMessageBusinessError101 BusinessError &AddErrorMessage(int32_t errorCode, const std::string &errorMessage) 102 { 103 ErrorMessage errMsg; 104 errMsg.errorCode = errorCode; 105 errMsg.errorMessage = errorMessage; 106 data.push_back(errMsg); 107 return *this; 108 } 109 to_jsonBusinessError110 friend void to_json(nlohmann::json &jsonObj, const BusinessError &businessError) 111 { 112 jsonObj["message"] = businessError.message; 113 jsonObj["errorNum"] = businessError.errorNum; 114 jsonObj["data"] = businessError.data; 115 } 116 from_jsonBusinessError117 friend void from_json(const nlohmann::json &jsonObj, BusinessError &businessError) 118 { 119 JsonUtils::GetValueAndSetTo(jsonObj, "message", businessError.message); 120 JsonUtils::GetValueAndSetTo(jsonObj, "data", businessError.data); 121 int32_t errorNumber = static_cast<int32_t>(CallResult::SUCCESS); 122 JsonUtils::GetValueAndSetTo(jsonObj, "errorNum", errorNumber); 123 businessError.errorNum = static_cast<CallResult>(errorNumber); 124 } 125 }; 126 } // namespace OHOS::UpdateEngine 127 #endif // COMMON_ERROR_DEFINE_H