1 /*
2 * Copyright (C) 2025 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 "ani_error.h"
17
18 #include "medialibrary_client_errno.h"
19 #include "medialibrary_ani_utils.h"
20 using namespace std;
21
22 namespace OHOS {
23 namespace Media {
SetApiName(const std::string & Name)24 void AniError::SetApiName(const std::string &Name)
25 {
26 apiName = Name;
27 }
28
SaveError(const std::shared_ptr<DataShare::DataShareResultSet> & resultSet)29 void AniError::SaveError(const std::shared_ptr<DataShare::DataShareResultSet> &resultSet)
30 {
31 error = MediaLibraryAniUtils::TransErrorCode(apiName, resultSet);
32 }
33
SaveError(int32_t ret)34 void AniError::SaveError(int32_t ret)
35 {
36 if (ret < 0) {
37 error = MediaLibraryAniUtils::TransErrorCode(apiName, ret);
38 }
39 }
40
HandleError(ani_env * env,ani_object & errorObj)41 void AniError::HandleError(ani_env *env, ani_object &errorObj)
42 {
43 // deal with context->error
44 MediaLibraryAniUtils::HandleError(env, error, errorObj, apiName);
45 }
46
ThrowError(ani_env * env,int32_t err,const std::string & errMsg)47 void AniError::ThrowError(ani_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 ANI_ERR_LOG("ThrowError errCode:%{public}d errMsg:%{public}s", err, message.c_str());
58 ani_object aniError;
59 MediaLibraryAniUtils::CreateAniErrorObject(env, aniError, err, errMsg);
60 env->ThrowError(static_cast<ani_error>(aniError));
61 }
62
ThrowError(ani_env * env,int32_t err,const char * funcName,int32_t line,const std::string & errMsg)63 void AniError::ThrowError(ani_env *env, int32_t err, const char *funcName, int32_t line, const std::string &errMsg)
64 {
65 string message = errMsg;
66 if (message.empty()) {
67 message = "operation not support";
68 if (jsErrMap.count(err) > 0) {
69 message = jsErrMap.at(err);
70 }
71 }
72
73 ANI_ERR_LOG("{%{public}s:%d} ThrowError errCode:%{public}d errMsg:%{public}s", funcName, line,
74 err, message.c_str());
75 ani_object aniError;
76 MediaLibraryAniUtils::CreateAniErrorObject(env, aniError, err, errMsg);
77 env->ThrowError(static_cast<ani_error>(aniError));
78 }
79
80 } // namespace Media
81 } // namespace OHOS