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#include <string> 18#include <unordered_map> 19#include <cstdarg> 20#include "securec.h" 21 22namespace { 23// error message 24const std::string PARAM_CHECK_ERROR_MSG = "Invalid input parameter"; 25const std::string INIT_ERROR_MSG = "Init error. The WebviewController must be associated with a Web component"; 26const std::string INVALID_URL_MSG = "Invalid url"; 27const std::string INVALID_RESOURCE_MSG = "Invalid resource path or file type"; 28const std::string FUNCTION_NOT_ENABLE_MSG = "Function not enabled."; 29const std::string INVALID_COOKIE_VALUE_MSG = "Invalid cookie value"; 30const std::string CAN_NOT_REGISTER_MESSAGE_EVENT_MSG = "Fail to register a message event for the port."; 31const std::string CANNOT_DEL_JAVA_SCRIPT_PROXY_MSG = "Failed to delete JavaScriptProxy because it does not exist."; 32const std::string CAN_NOT_POST_MESSAGE_MSG = "Failed to post messages through the port."; 33const std::string INVALID_ORIGIN_MSG = "Invalid origin"; 34const std::string NO_WEBSTORAGE_ORIGIN_MSG = "Invalid web storage origin"; 35const std::string INVALID_SOCKET_NUMBER_MSG = "The number of sockets to be preconnected is invalid."; 36const std::string TYPE_NOT_MATCH_WITCH_VALUE_MSG = "The type and value of the message do not match."; 37const std::string NEW_OOM_MSG = "Memory allocation failed."; 38const std::string DOWNLOAD_NOT_PAUSED_MSG = "The download task is not paused."; 39const std::string NO_VALID_CONTROLLER_FOR_DOWNLOAD_MSG = "No valid WebviewController is associated."; 40const std::string NO_DOWNLOAD_DELEGATE_SET_MSG = "No WebDownloadDelegate has been set yet."; 41const std::string DOWNLOAD_NOT_START_MSG = "The download task is not started yet."; 42const std::string REGISTER_CUSTOM_SCHEME_FAILED_MSG = "Failed to register custom schemes."; 43const std::string HTTP_BODY_STREAN_INIT_FAILED_MSG = "Failed to initialize the HTTP body stream."; 44const std::string RESOURCE_HANDLER_INVALID_MSG = "The resource handler is invalid."; 45const std::string UNKNOWN_ERROR_MSG = "Unknown error message."; 46} 47 48namespace OHOS { 49namespace ParamCheckErrorMsgTemplate { 50 const char* TYPE_ERROR = "BusinessError 401: Parameter error. The type of '%s' must be %s."; 51 const char* TYPE_ALL_STRING = "BusinessError 401: Parameter error. The type of params must be string."; 52 const char* TYPE_ALL_INT = "BusinessError 401: Parameter error. The type of params must be int."; 53 const char* PARAM_TYEPS_ERROR = "BusinessError 401: Parameter error. The type of params is error."; 54 const char* PARAM_NUMBERS_ERROR_ONE = "BusinessError 401: Parameter error. The number of params must be %s."; 55 const char* PARAM_NUMBERS_ERROR_TWO = "BusinessError 401: Parameter error. The number of params must be %s or %s."; 56 const char* PARAM_NUMBERS_ERROR_THREE = 57 "BusinessError 401: Parameter error. The number of params must be %s or %s or %s."; 58 const char* PARAM_NOT_NULL = "BusinessError 401: Parameter error. The type of '%s' can not be ignored."; 59 const char* PARAM_NOT_NULL_TWO = 60 "BusinessError 401: Parameter error. The type of '%s' and '%s' can not be ignored."; 61 const char* PARAM_TYPE_INVALID = "BusinessError 401: Parameter error. The type of '%s' is invalid."; 62 const char* PARAM_DETAIL_ERROR_MSG = "BusinessError 401: Parameter error. detail: %s."; 63} 64namespace NWebError { 65std::unordered_map<ErrCode, std::string> g_errCodeMsgMap = { 66 {PARAM_CHECK_ERROR, PARAM_CHECK_ERROR_MSG}, 67 {INIT_ERROR, INIT_ERROR_MSG}, 68 {INVALID_URL, INVALID_URL_MSG}, 69 {INVALID_RESOURCE, INVALID_RESOURCE_MSG}, 70 {FUNCTION_NOT_ENABLE, FUNCTION_NOT_ENABLE_MSG}, 71 {INVALID_COOKIE_VALUE, INVALID_COOKIE_VALUE_MSG}, 72 {CAN_NOT_REGISTER_MESSAGE_EVENT, CAN_NOT_REGISTER_MESSAGE_EVENT_MSG}, 73 {CANNOT_DEL_JAVA_SCRIPT_PROXY, CANNOT_DEL_JAVA_SCRIPT_PROXY_MSG}, 74 {CAN_NOT_POST_MESSAGE, CAN_NOT_POST_MESSAGE_MSG}, 75 {INVALID_ORIGIN, INVALID_ORIGIN_MSG}, 76 {NO_WEBSTORAGE_ORIGIN, NO_WEBSTORAGE_ORIGIN_MSG}, 77 {INVALID_SOCKET_NUMBER, INVALID_SOCKET_NUMBER_MSG}, 78 {TYPE_NOT_MATCH_WITCH_VALUE, TYPE_NOT_MATCH_WITCH_VALUE_MSG}, 79 {NEW_OOM, NEW_OOM_MSG}, 80 {DOWNLOAD_NOT_PAUSED, DOWNLOAD_NOT_PAUSED_MSG}, 81 {NO_VALID_CONTROLLER_FOR_DOWNLOAD, NO_VALID_CONTROLLER_FOR_DOWNLOAD_MSG}, 82 {NO_DOWNLOAD_DELEGATE_SET, NO_DOWNLOAD_DELEGATE_SET_MSG}, 83 {DOWNLOAD_NOT_START, DOWNLOAD_NOT_START_MSG}, 84 {REGISTER_CUSTOM_SCHEME_FAILED, REGISTER_CUSTOM_SCHEME_FAILED_MSG}, 85 {HTTP_BODY_STREAN_INIT_FAILED, HTTP_BODY_STREAN_INIT_FAILED_MSG}, 86 {RESOURCE_HANDLER_INVALID, RESOURCE_HANDLER_INVALID_MSG}, 87}; 88 89std::string GetErrMsgByErrCode(ErrCode code) 90{ 91 auto it = g_errCodeMsgMap.find(code); 92 if (it != g_errCodeMsgMap.end()) { 93 return it->second; 94 } 95 return UNKNOWN_ERROR_MSG; 96} 97std::string FormatString(const char *errorMsgTemplate, ...) 98{ 99 char sbuf[256]; 100 va_list args; 101 va_start(args, errorMsgTemplate); 102 if (vsnprintf_s(sbuf, sizeof(sbuf), sizeof(sbuf) - 1, errorMsgTemplate, args) < 0) { 103 va_end(args); 104 return ""; 105 } 106 va_end(args); 107 return sbuf; 108} 109} // namespace NWebError 110} // namespace OHOS 111