1 /*
2 * Copyright (c) 2023 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 "drm_error_code.h"
17 #include "common_napi.h"
18 #include "napi_param_utils.h"
19
20 namespace OHOS {
21 namespace DrmStandard {
ThrowError(napi_env env,const char * napiMessage,int32_t napiCode)22 napi_status NapiDrmError::ThrowError(napi_env env, const char *napiMessage, int32_t napiCode)
23 {
24 napi_value message = nullptr;
25 napi_value code = nullptr;
26 napi_value result = nullptr;
27 napi_status status = napi_create_string_utf8(env, napiMessage, NAPI_AUTO_LENGTH, &message);
28 DRM_NAPI_CHECK_AND_RETURN_LOG(status == napi_ok, status, "ThrowError napi_create_string_utf8 failed.");
29 status = napi_create_error(env, nullptr, message, &result);
30 DRM_NAPI_CHECK_AND_RETURN_LOG(status == napi_ok, status, "ThrowError napi_create_error failed.");
31 status = napi_create_int32(env, napiCode, &code);
32 DRM_NAPI_CHECK_AND_RETURN_LOG(status == napi_ok, status, "ThrowError napi_create_int32 failed.");
33 status = napi_set_named_property(env, result, "code", code);
34 DRM_NAPI_CHECK_AND_RETURN_LOG(status == napi_ok, status, "ThrowError napi_set_named_property failed.");
35 status = napi_throw(env, result);
36 DRM_NAPI_CHECK_AND_RETURN_LOG(status == napi_ok, status, "ThrowError napi_throw failed.");
37 return napi_ok;
38 }
39
ThrowError(napi_env env,int32_t code)40 void NapiDrmError::ThrowError(napi_env env, int32_t code)
41 {
42 std::string messageValue = GetMessageByCode(code);
43 napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
44 }
45
GetMessageByCode(int32_t & code)46 std::string NapiDrmError::GetMessageByCode(int32_t &code)
47 {
48 std::string errMessage;
49 switch (code) {
50 case DRM_INVALID_PARAM:
51 errMessage = DRM_INVALID_PARAM_INFO;
52 break;
53 case DRM_SERVICE_FATAL_ERROR:
54 errMessage = DRM_SERVICE_FATAL_ERRO_INFO;
55 break;
56 case DRM_UNKNOWN_ERROR:
57 errMessage = DRM_UNKNOWN_ERROR_INFO;
58 break;
59 case DRM_MAX_SYSTEM_NUM_REACHED:
60 errMessage = DRM_MAX_SYSTEM_NUM_REACHED_INFO;
61 break;
62 case DRM_MAX_SESSION_NUM_REACHED:
63 errMessage = DRM_MAX_SESSION_NUM_REACHED_INFO;
64 break;
65 default:
66 errMessage = DRM_UNKNOWN_ERROR_INFO;
67 code = DRM_UNKNOWN_ERROR;
68 break;
69 }
70 return errMessage;
71 }
72 } // namespace DrmStandard
73 } // namespace OHOS