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_NOT_STAGE_MODE, 14801001, "Only supported in stage mode." },
25 { E_DATA_GROUP_ID_INVALID, 14801002, "The data group id is invalid." },
26 { NativeRdb::E_NOT_SELECT, 14800019, "The SQL must be a query statement." },
27 { NativeRdb::E_COLUMN_OUT_RANGE, 14800013, "Column out of bounds." },
28 { NativeRdb::E_INVALID_FILE_PATH, 14800010, "Invalid database path." },
29 { NativeRdb::E_ROW_OUT_RANGE, 14800012, "Row out of bounds." },
30 { NativeRdb::E_NO_ROW_IN_QUERY, 14800018, "No data meets the condition." },
31 { NativeRdb::E_ALREADY_CLOSED, 14800014, "Already closed." },
32 { NativeRdb::E_DATABASE_BUSY, 14800015, "The database does not respond." },
33 { NativeRdb::E_WAL_SIZE_OVER_LIMIT, 14800047, "The WAL file size over default limit." },
34 { NativeRdb::E_GET_DATAOBSMGRCLIENT_FAIL, 14801050, "Failed to get DataObsMgrClient." },
35 { NativeRdb::E_TYPE_MISMATCH, 14800051, "The type of the distributed table does not match." },
36 { NativeRdb::E_SQLITE_FULL, 14800029, "SQLite: The database is full." },
37 { NativeRdb::E_NOT_SUPPORT_THE_SQL, 14800021, "SQLite: Generic error."},
38 { NativeRdb::E_ATTACHED_DATABASE_EXIST, 14800016, "The database is already attached." },
39 { NativeRdb::E_SQLITE_ERROR, 14800021, "SQLite: Generic error." },
40 { NativeRdb::E_SQLITE_CORRUPT, 14800011, "Database corrupted." },
41 { NativeRdb::E_SQLITE_ABORT, 14800022, "SQLite: Callback routine requested an abort." },
42 { NativeRdb::E_SQLITE_PERM, 14800023, "SQLite: Access permission denied." },
43 { NativeRdb::E_SQLITE_BUSY, 14800024, "SQLite: The database file is locked." },
44 { NativeRdb::E_SQLITE_LOCKED, 14800025, "SQLite: A table in the database is locked." },
45 { NativeRdb::E_SQLITE_NOMEM, 14800026, "SQLite: The database is out of memory." },
46 { NativeRdb::E_SQLITE_READONLY, 14800027, "SQLite: Attempt to write a readonly database." },
47 { NativeRdb::E_SQLITE_IOERR, 14800028, "SQLite: Some kind of disk I/O error occurred." },
48 { NativeRdb::E_SQLITE_CANTOPEN, 14800030, "SQLite: Unable to open the database file." },
49 { NativeRdb::E_SQLITE_TOOBIG, 14800031, "SQLite: TEXT or BLOB exceeds size limit." },
50 { NativeRdb::E_SQLITE_CONSTRAINT, 14800032, "SQLite: Abort due to constraint violation." },
51 { NativeRdb::E_SQLITE_MISMATCH, 14800033, "SQLite: Data type mismatch." },
52 { NativeRdb::E_SQLITE_MISUSE, 14800034, "SQLite: Library used incorrectly." },
53 { NativeRdb::E_CONFIG_INVALID_CHANGE, 14800017, "Config changed." },
54 { NativeRdb::E_INVALID_SECRET_KEY, 14800020, "The secret key is corrupted or lost." },
55 { NativeRdb::E_NOT_SUPPORT, 801, "Capability not support." },
56 };
57
GetJsErrorCode(int32_t errorCode)58 const std::optional<JsErrorCode> GetJsErrorCode(int32_t errorCode)
59 {
60 auto jsErrorCode = JsErrorCode{ errorCode, -1, "" };
61 auto iter = std::lower_bound(JS_ERROR_CODE_MSGS,
62 JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]), jsErrorCode,
63 [](const JsErrorCode &jsErrorCode1, const JsErrorCode &jsErrorCode2) {
64 return jsErrorCode1.status < jsErrorCode2.status;
65 });
66 if (iter < JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]) &&
67 iter->status == errorCode) {
68 return *iter;
69 }
70 return std::nullopt;
71 }
72
73 } // namespace RelationalStoreJsKit
74 } // namespace OHOS