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)47 void NapiError::ThrowError(napi_env env, int32_t err)
48 {
49 string errMessage = "operation not support";
50 string errCode = std::to_string(err);
51 if (jsErrMap.count(err) > 0) {
52 errMessage = jsErrMap.at(err);
53 }
54 NAPI_ERR_LOG("ThrowError errCode:%{public}d errMessage:%{public}s", err, errMessage.c_str());
55 napi_throw_error(env, errCode.c_str(), errMessage.c_str());
56 }
57 } // namespace Media
58 } // namespace OHOS