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
HandleError(napi_env env,napi_value & errorObj)41 void NapiError::HandleError(napi_env env, napi_value &errorObj)
42 {
43 // deal with context->error
44 MediaLibraryNapiUtils::HandleError(env, error, errorObj, apiName);
45 }
46
ThrowError(napi_env env,int32_t err,const std::string & errMsg)47 void NapiError::ThrowError(napi_env env, int32_t err, const std::string &errMsg)
48 {
49 string message = errMsg;
50 if (message.empty()) {
51 message = "operation not support";
52 if (jsErrMap.count(err) > 0) {
53 message = jsErrMap.at(err);
54 }
55 }
56
57 NAPI_ERR_LOG("ThrowError errCode:%{public}d errMsg:%{public}s", err, message.c_str());
58 NAPI_CALL_RETURN_VOID(env, napi_throw_error(env, to_string(err).c_str(), message.c_str()));
59 }
60
ThrowError(napi_env env,int32_t err,const char * funcName,int32_t line,const std::string & errMsg)61 void NapiError::ThrowError(napi_env env, int32_t err, const char *funcName, int32_t line, const std::string &errMsg)
62 {
63 string message = errMsg;
64 if (message.empty()) {
65 message = "operation not support";
66 if (jsErrMap.count(err) > 0) {
67 message = jsErrMap.at(err);
68 }
69 }
70
71 NAPI_ERR_LOG("{%{public}s:%d} ThrowError errCode:%{public}d errMsg:%{public}s", funcName, line,
72 err, message.c_str());
73 NAPI_CALL_RETURN_VOID(env, napi_throw_error(env, to_string(err).c_str(), message.c_str()));
74 }
75
76 } // namespace Media
77 } // namespace OHOS