• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "web_errors.h"
17 
18 #include <string>
19 #include <unordered_map>
20 
21 namespace {
22 // error message
23 const std::string PARAM_CHECK_ERROR_MSG = "Invalid input parameter";
24 const std::string INIT_ERROR_MSG = "Init error. The WebviewController must be associated with a Web component";
25 const std::string INVALID_URL_MSG = "URL error. Possible causes:1.No valid cookie found for the specified URL. "
26     "2.The webpage corresponding to the URL is invalid, or the URL length exceeds 2048.";
27 const std::string INVALID_RESOURCE_MSG = "Invalid resource path or file type";
28 const std::string FUNCTION_NOT_ENABLE_MSG = "Function not enabled.";
29 const std::string INVALID_COOKIE_VALUE_MSG = "The provided cookie value is invalid. It must follow the format "
30     "specified in RFC 6265.";
31 const std::string CAN_NOT_REGISTER_MESSAGE_EVENT_MSG = "Fail to register a message event for the port.";
32 const std::string CANNOT_DEL_JAVA_SCRIPT_PROXY_MSG = "Failed to delete JavaScriptProxy because it does not exist.";
33 const std::string CAN_NOT_POST_MESSAGE_MSG = "Failed to post messages through the port.";
34 const std::string INVALID_ORIGIN_MSG = "Invalid origin.The origin format must follow defined in RFC 6454.";
35 const std::string NO_WEBSTORAGE_ORIGIN_MSG = "Invalid web storage origin.The web storage origin is empty.";
36 const std::string INVALID_SOCKET_NUMBER_MSG = "The number of sockets to be preconnected is invalid.";
37 const std::string TYPE_NOT_MATCH_WITCH_VALUE_MSG = "The type and value of the message do not match.";
38 const std::string NEW_OOM_MSG = "Memory allocation failed.";
39 const std::string DOWNLOAD_NOT_PAUSED_MSG = "The download task is not paused.";
40 const std::string NO_VALID_CONTROLLER_FOR_DOWNLOAD_MSG = "No valid WebviewController is associated.";
41 const std::string NO_DOWNLOAD_DELEGATE_SET_MSG = "No WebDownloadDelegate has been set yet.";
42 const std::string DOWNLOAD_NOT_START_MSG = "The download task is not started yet.";
43 const std::string REGISTER_CUSTOM_SCHEME_FAILED_MSG = "Failed to register custom schemes.";
44 const std::string HTTP_BODY_STREAN_INIT_FAILED_MSG = "Failed to initialize the HTTP body stream.";
45 const std::string RESOURCE_HANDLER_INVALID_MSG = "The resource handler is invalid.";
46 const std::string HTTP_AUTH_MALLOC_FAILED_MSG = "Failed to malloc string memory to get HttpAuth.";
47 const std::string UNKNOWN_ERROR_MSG = "Unknown error message.";
48 }
49 
50 namespace OHOS {
51 namespace NWebError {
52 std::unordered_map<ErrCode, std::string> g_errCodeMsgMap = {
53     {PARAM_CHECK_ERROR, PARAM_CHECK_ERROR_MSG},
54     {INIT_ERROR, INIT_ERROR_MSG},
55     {INVALID_URL, INVALID_URL_MSG},
56     {INVALID_RESOURCE, INVALID_RESOURCE_MSG},
57     {FUNCTION_NOT_ENABLE, FUNCTION_NOT_ENABLE_MSG},
58     {INVALID_COOKIE_VALUE, INVALID_COOKIE_VALUE_MSG},
59     {CAN_NOT_REGISTER_MESSAGE_EVENT, CAN_NOT_REGISTER_MESSAGE_EVENT_MSG},
60     {CANNOT_DEL_JAVA_SCRIPT_PROXY, CANNOT_DEL_JAVA_SCRIPT_PROXY_MSG},
61     {CAN_NOT_POST_MESSAGE, CAN_NOT_POST_MESSAGE_MSG},
62     {INVALID_ORIGIN, INVALID_ORIGIN_MSG},
63     {NO_WEBSTORAGE_ORIGIN, NO_WEBSTORAGE_ORIGIN_MSG},
64     {INVALID_SOCKET_NUMBER, INVALID_SOCKET_NUMBER_MSG},
65     {TYPE_NOT_MATCH_WITCH_VALUE, TYPE_NOT_MATCH_WITCH_VALUE_MSG},
66     {NEW_OOM, NEW_OOM_MSG},
67     {DOWNLOAD_NOT_PAUSED, DOWNLOAD_NOT_PAUSED_MSG},
68     {NO_VALID_CONTROLLER_FOR_DOWNLOAD, NO_VALID_CONTROLLER_FOR_DOWNLOAD_MSG},
69     {NO_DOWNLOAD_DELEGATE_SET, NO_DOWNLOAD_DELEGATE_SET_MSG},
70     {DOWNLOAD_NOT_START, DOWNLOAD_NOT_START_MSG},
71     {REGISTER_CUSTOM_SCHEME_FAILED, REGISTER_CUSTOM_SCHEME_FAILED_MSG},
72     {HTTP_BODY_STREAN_INIT_FAILED, HTTP_BODY_STREAN_INIT_FAILED_MSG},
73     {RESOURCE_HANDLER_INVALID, RESOURCE_HANDLER_INVALID_MSG},
74     {HTTP_AUTH_MALLOC_FAILED, HTTP_AUTH_MALLOC_FAILED_MSG},
75 };
76 
GetErrMsgByErrCode(ErrCode code)77 std::string GetErrMsgByErrCode(ErrCode code)
78 {
79     auto it = g_errCodeMsgMap.find(code);
80     if (it != g_errCodeMsgMap.end()) {
81         return it->second;
82     }
83     return UNKNOWN_ERROR_MSG;
84 }
85 } // namespace NWebError
86 } // namespace OHOS
87