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 #include <unordered_map>
17 #include "devattest_errno.h"
18 #include "devattest_napi_error.h"
19 
20 namespace OHOS {
21 namespace DevAttest {
22 static const std::unordered_map<uint32_t, std::string> g_errorStringMap = {
23     {DEVATTEST_ERR_JS_IS_NOT_SYSTEM_APP,
24         "This api is system api, Please use the system application to call this api"},
25     {DEVATTEST_ERR_JS_PARAMETER_ERROR,
26         "Input parameters wrong, the number of parameters is incorrect, or the type of parameters is incorrect"},
27     {DEVATTEST_ERR_JS_SYSTEM_SERVICE_EXCEPTION,
28         "System service exception, please try again or reboot your device"},
29 };
30 
ConvertToJsErrCode(int32_t errCode)31 int32_t ConvertToJsErrCode(int32_t errCode)
32 {
33     int32_t jsErrCode = errCode;
34     if (jsErrCode == DEVATTEST_FAIL) {
35         jsErrCode = DEVATTEST_ERR_JS_SYSTEM_SERVICE_EXCEPTION;
36     }
37     return jsErrCode;
38 }
39 
ConvertToJsErrMsg(int32_t jsErrCode)40 std::string ConvertToJsErrMsg(int32_t jsErrCode)
41 {
42     auto iter = g_errorStringMap.find(jsErrCode);
43     if (iter != g_errorStringMap.end()) {
44         return iter->second;
45     } else {
46         return "Unknown error, please reboot your device and try again";
47     }
48 }
49 } // namespace DevAttest
50 } // namespace OHOS
51