• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_rdb_error.h"
17 
18 #include <algorithm>
19 
20 namespace OHOS {
21 namespace RelationalStoreJsKit {
22 using JsErrorCode = OHOS::RelationalStoreJsKit::JsErrorCode;
23 static constexpr JsErrorCode JS_ERROR_CODE_MSGS[] = {
24     { E_PARAM_ERROR, 401, "Invalid args."},
25     { E_NOT_STAGE_MODE, 14801001, "The operation is supported in the stage model only." },
26     { E_DATA_GROUP_ID_INVALID, 14801002, "Invalid data ground ID." },
27     { NativeRdb::E_NOT_SELECT, 14800019, "The SQL must be a query statement." },
28     { NativeRdb::E_COLUMN_OUT_RANGE, 14800013, "Resultset is empty or column index is out of bounds." },
29     { NativeRdb::E_INVALID_FILE_PATH, 14800010, "Failed to open or delete the database by an invalid database path." },
30     { NativeRdb::E_ROW_OUT_RANGE, 14800012, "ResultSet is empty or pointer index is out of bounds." },
31     { NativeRdb::E_NO_ROW_IN_QUERY, 14800018, "No data meets the condition." },
32     { NativeRdb::E_ALREADY_CLOSED, 14800014, "The RdbStore or ResultSet is already closed." },
33     { NativeRdb::E_DATABASE_BUSY, 14800015, "The database does not respond." },
34     { NativeRdb::E_WAL_SIZE_OVER_LIMIT, 14800047, "The WAL file size over default limit." },
35     { NativeRdb::E_GET_DATAOBSMGRCLIENT_FAIL, 14801050, "Failed to get DataObsMgrClient." },
36     { NativeRdb::E_TYPE_MISMATCH, 14800051, "The type of the distributed table does not match." },
37     { NativeRdb::E_SQLITE_FULL, 14800029, "SQLite: The database is full." },
38     { NativeRdb::E_NOT_SUPPORT_THE_SQL, 14800021, "SQLite: Generic error. Executed SQL statement is not supported." },
39     { NativeRdb::E_ATTACHED_DATABASE_EXIST, 14800016, "The database alias already exists." },
40     { NativeRdb::E_SQLITE_ERROR, 14800021,
41         "SQLite: Generic error. Possible causes: Insert failed or the updated data does not exist." },
42     { NativeRdb::E_SQLITE_CORRUPT, 14800011, "Failed to open the database because it is corrupted." },
43     { NativeRdb::E_SQLITE_ABORT, 14800022, "SQLite: Callback routine requested an abort." },
44     { NativeRdb::E_SQLITE_PERM, 14800023, "SQLite: Access permission denied." },
45     { NativeRdb::E_SQLITE_BUSY, 14800024, "SQLite: The database file is locked." },
46     { NativeRdb::E_SQLITE_LOCKED, 14800025, "SQLite: A table in the database is locked." },
47     { NativeRdb::E_SQLITE_NOMEM, 14800026, "SQLite: The database is out of memory." },
48     { NativeRdb::E_SQLITE_READONLY, 14800027, "SQLite: Attempt to write a readonly database." },
49     { NativeRdb::E_SQLITE_IOERR, 14800028, "SQLite: Some kind of disk I/O error occurred." },
50     { NativeRdb::E_SQLITE_CANTOPEN, 14800030, "SQLite: Unable to open the database file." },
51     { NativeRdb::E_SQLITE_TOOBIG, 14800031, "SQLite: TEXT or BLOB exceeds size limit." },
52     { NativeRdb::E_SQLITE_CONSTRAINT, 14800032, "SQLite: Abort due to constraint violation." },
53     { NativeRdb::E_SQLITE_MISMATCH, 14800033, "SQLite: Data type mismatch." },
54     { NativeRdb::E_SQLITE_MISUSE, 14800034, "SQLite: Library used incorrectly." },
55     { NativeRdb::E_CONFIG_INVALID_CHANGE, 14800017, "StoreConfig is changed." },
56     { NativeRdb::E_INVALID_SECRET_KEY, 14800020, "The secret key is corrupted or lost." },
57     { NativeRdb::E_SQLITE_IOERR_FULL, 14800028, "SQLite: Some kind of disk I/O error occurred." },
58     { NativeRdb::E_INVALID_ARGS_NEW, 14800001, "Invalid args." },
59     { NativeRdb::E_NOT_SUPPORT, 801, "Capability not support." },
60 };
61 
IsIncreasing()62 static constexpr bool IsIncreasing()
63 {
64     for (size_t i = 1; i < sizeof(JS_ERROR_CODE_MSGS) / sizeof(JsErrorCode); i++) {
65         if (JS_ERROR_CODE_MSGS[i].status <= JS_ERROR_CODE_MSGS[i - 1].status) {
66             return false;
67         }
68     }
69     return true;
70 }
71 // JS_ERROR_CODE_MSGS must ensure increment
72 static_assert(IsIncreasing());
73 
GetJsErrorCode(int32_t errorCode)74 const std::optional<JsErrorCode> GetJsErrorCode(int32_t errorCode)
75 {
76     auto jsErrorCode = JsErrorCode{ errorCode, -1, "" };
77     auto iter = std::lower_bound(JS_ERROR_CODE_MSGS,
78         JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]), jsErrorCode,
79         [](const JsErrorCode &jsErrorCode1, const JsErrorCode &jsErrorCode2) {
80             return jsErrorCode1.status < jsErrorCode2.status;
81         });
82     if (iter < JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]) &&
83         iter->status == errorCode) {
84         return *iter;
85     }
86     return std::nullopt;
87 }
88 
89 } // namespace RelationalStoreJsKit
90 } // namespace OHOS