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 "doc_errno.h"
17 
18 #include "grd_base/grd_error.h"
19 
20 namespace DocumentDB {
GetErrorCategory(int errCode)21 int GetErrorCategory(int errCode)
22 {
23     int categoryCode = errCode % 1000000; // 1000000: mod to get last 6 digits
24     categoryCode /= 1000; // 1000: deviced to remove first 3 digits
25     categoryCode *= 1000; // 1000: multiply to pad the output
26     return categoryCode;
27 }
28 
TransferDocErr(int err)29 int TransferDocErr(int err)
30 {
31     if (err > 0) {
32         return err;
33     }
34 
35     switch (err) {
36         case E_OK:
37             return GRD_OK;
38         case -E_ERROR:
39             return GetErrorCategory(GRD_INNER_ERR);
40         case -E_INVALID_ARGS:
41             return GetErrorCategory(GRD_INVALID_ARGS);
42         case -E_FILE_OPERATION:
43             return GetErrorCategory(GRD_FAILED_FILE_OPERATION);
44         case -E_OVER_LIMIT:
45             return GetErrorCategory(GRD_OVER_LIMIT);
46         case -E_INVALID_JSON_FORMAT:
47             return GetErrorCategory(GRD_INVALID_JSON_FORMAT);
48         case -E_INVALID_CONFIG_VALUE:
49             return GetErrorCategory(GRD_INVALID_CONFIG_VALUE);
50         case -E_DATA_CONFLICT:
51             return GetErrorCategory(GRD_DATA_CONFLICT);
52         case -E_COLLECTION_CONFLICT:
53             return GetErrorCategory(GRD_COLLECTION_CONFLICT);
54         case -E_NO_DATA:
55         case -E_NOT_FOUND:
56             return GetErrorCategory(GRD_NO_DATA);
57         case -E_INVALID_COLL_NAME_FORMAT:
58             return GetErrorCategory(GRD_INVALID_COLLECTION_NAME);
59         case -E_RESOURCE_BUSY:
60             return GetErrorCategory(GRD_RESOURCE_BUSY);
61         case -E_FAILED_MEMORY_ALLOCATE:
62         case -E_OUT_OF_MEMORY:
63             return GetErrorCategory(GRD_FAILED_MEMORY_ALLOCATE);
64         case -E_INVALID_FILE_FORMAT:
65             return GetErrorCategory(GRD_INVALID_FILE_FORMAT);
66         case -E_FAILED_FILE_OPERATION:
67             return GetErrorCategory(GRD_FAILED_FILE_OPERATION);
68         case -E_NOT_SUPPORT:
69             return GetErrorCategory(GRD_NOT_SUPPORT);
70         case -E_INNER_ERROR:
71         default:
72             return GetErrorCategory(GRD_INNER_ERR);
73     }
74 }
75 } // namespace DocumentDB