• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 "napi_error.h"
17 
18 #include "medialibrary_client_errno.h"
19 #include "medialibrary_napi_utils.h"
20 using namespace std;
21 
22 namespace OHOS {
23 namespace Media {
SetApiName(const std::string & Name)24 void NapiError::SetApiName(const std::string &Name)
25 {
26     apiName = Name;
27 }
28 
SaveError(const std::shared_ptr<DataShare::DataShareResultSet> & resultSet)29 void NapiError::SaveError(const std::shared_ptr<DataShare::DataShareResultSet> &resultSet)
30 {
31     error = MediaLibraryNapiUtils::TransErrorCode(apiName, resultSet);
32 }
33 
SaveError(int32_t ret)34 void NapiError::SaveError(int32_t ret)
35 {
36     if (ret < 0) {
37         error = MediaLibraryNapiUtils::TransErrorCode(apiName, ret);
38     }
39 }
40 
SaveRealErr(int32_t ret)41 void NapiError::SaveRealErr(int32_t ret)
42 {
43     if (ret < 0) {
44         realErr = MediaLibraryNapiUtils::TransErrorCode(apiName, ret);
45     }
46     NAPI_ERR_LOG("SaveRealErr errCode:%{public}d realErr:%{public}d", ret, realErr);
47 }
48 
HandleError(napi_env env,napi_value & errorObj)49 void NapiError::HandleError(napi_env env, napi_value &errorObj)
50 {
51     // deal with context->error
52     MediaLibraryNapiUtils::HandleError(env, error, errorObj, apiName, realErr);
53 }
54 
ThrowError(napi_env env,int32_t err,const std::string & errMsg)55 void NapiError::ThrowError(napi_env env, int32_t err, const std::string &errMsg)
56 {
57     string message = errMsg;
58     if (message.empty()) {
59         message = "operation not support";
60         if (jsErrMap.count(err) > 0) {
61             message = jsErrMap.at(err);
62         }
63     }
64 
65     NAPI_DEBUG_LOG("ThrowError errCode:%{public}d errMsg:%{public}s", err, message.c_str());
66     NAPI_CALL_RETURN_VOID(env, napi_throw_error(env, to_string(err).c_str(), message.c_str()));
67 }
68 
ThrowError(napi_env env,int32_t err,const char * funcName,int32_t line,const std::string & errMsg)69 void NapiError::ThrowError(napi_env env, int32_t err, const char *funcName, int32_t line, const std::string &errMsg)
70 {
71     string message = errMsg;
72     if (message.empty()) {
73         message = "operation not support";
74         if (jsErrMap.count(err) > 0) {
75             message = jsErrMap.at(err);
76         }
77     }
78 
79     NAPI_ERR_LOG("{%{public}s:%d} ThrowError errCode:%{public}d errMsg:%{public}s", funcName, line,
80         err, message.c_str());
81     NAPI_CALL_RETURN_VOID(env, napi_throw_error(env, to_string(err).c_str(), message.c_str()));
82 }
83 
84 } // namespace Media
85 } // namespace OHOS