1 /*
2  * Copyright (C) 2021-2022 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 #ifndef OHOS_MEDIALIBRARY_CLIENT_ERRNO_H
17 #define OHOS_MEDIALIBRARY_CLIENT_ERRNO_H
18 
19 #include <errno.h>
20 #include <unordered_map>
21 
22 #include "medialibrary_errno.h"
23 
24 namespace OHOS {
25 namespace Media {
26 constexpr int32_t FILEIO_MODULE_CODE = 139;
27 constexpr int32_t UFM_MODULE_CODE = 140;
28 constexpr int32_t UFM_SYSCAP_BASE = 202;
29 
30 constexpr int32_t OHOS_PERMISSION_DENIED_CODE = 201;
31 constexpr int32_t OHOS_INVALID_PARAM_CODE = 401;
32 
33 #define MODULE_OFFSET  100000
34 #define MODULE_CODE(code) (((code) * MODULE_OFFSET))
35 #define UFM_JS_ERR(moduleCode, errCode) ((MODULE_CODE(moduleCode))  + (errCode))
36 // file io common error code
37 constexpr int32_t JS_ERR_NO_SUCH_FILE      = UFM_JS_ERR(FILEIO_MODULE_CODE, 2);         // no such file
38 constexpr int32_t JS_ERR_NO_MEM            = UFM_JS_ERR(FILEIO_MODULE_CODE, 11);        // cannot allocate memory
39 constexpr int32_t JS_ERR_PERMISSION_DENIED = UFM_JS_ERR(FILEIO_MODULE_CODE, 12);        // permission deny
40 constexpr int32_t JS_ERR_FILE_EXIST        = UFM_JS_ERR(FILEIO_MODULE_CODE, 15);        // file has exist
41 constexpr int32_t JS_ERR_PARAMETER_INVALID = UFM_JS_ERR(FILEIO_MODULE_CODE, 20);        // input parameter invalid
42 
43 // userfileMananger error code
44 constexpr int32_t JS_E_DISPLAYNAME    = UFM_JS_ERR(UFM_MODULE_CODE, 1);
45 constexpr int32_t JS_E_URI            = UFM_JS_ERR(UFM_MODULE_CODE, 2);
46 constexpr int32_t JS_E_FILE_EXTENSION = UFM_JS_ERR(UFM_MODULE_CODE, 3);
47 constexpr int32_t JS_E_TRASHED        = UFM_JS_ERR(UFM_MODULE_CODE, 4);
48 constexpr int32_t JS_E_OPEN_MODE      = UFM_JS_ERR(UFM_MODULE_CODE, 5);
49 constexpr int32_t JS_E_NOT_ALBUM      = UFM_JS_ERR(UFM_MODULE_CODE, 6);
50 constexpr int32_t JS_E_ROOT_DIR       = UFM_JS_ERR(UFM_MODULE_CODE, 7);
51 constexpr int32_t JS_E_MOVE_DENIED    = UFM_JS_ERR(UFM_MODULE_CODE, 8);
52 constexpr int32_t JS_E_RENAME_DENIED  = UFM_JS_ERR(UFM_MODULE_CODE, 9);
53 constexpr int32_t JS_E_RELATIVEPATH   = UFM_JS_ERR(UFM_MODULE_CODE, 10);
54 constexpr int32_t JS_INNER_FAIL       = UFM_JS_ERR(UFM_MODULE_CODE, 11);
55 // file type is not allow in the directory
56 constexpr int32_t JS_E_FILE_TYPE      = UFM_JS_ERR(UFM_MODULE_CODE, 12);
57 constexpr int32_t JS_E_NO_MEMORY      = UFM_JS_ERR(UFM_MODULE_CODE, 13);    // no memory left
58 constexpr int32_t JS_E_FILE_KEY       = UFM_JS_ERR(UFM_MODULE_CODE, 14);    // wrong member name
59 constexpr int32_t JS_E_INPUT          = UFM_JS_ERR(UFM_MODULE_CODE, 15);
60 // media change request error
61 constexpr int32_t JS_E_OPERATION_NOT_SUPPORT = UFM_JS_ERR(UFM_MODULE_CODE, 16);
62 
63 constexpr int32_t JS_E_NAMETOOLONG    = UFM_JS_ERR(UFM_SYSCAP_BASE, 36);
64 
65 // trans server errorCode to js Error code
66 const std::unordered_map<int, int> trans2JsError = {
67     { E_PERMISSION_DENIED,    JS_ERR_PERMISSION_DENIED },
68     { E_FAIL,                 JS_INNER_FAIL },
69     { E_NO_SUCH_FILE,         JS_ERR_NO_SUCH_FILE },
70     { E_FILE_EXIST,           JS_ERR_FILE_EXIST },
71     { E_NO_MEMORY,            JS_E_NO_MEMORY },
72     { E_FILE_NAME_INVALID,    JS_E_DISPLAYNAME },
73     { E_INVALID_DISPLAY_NAME, JS_E_DISPLAYNAME },
74     { E_CHECK_EXTENSION_FAIL, JS_E_FILE_TYPE },
75     { E_FILE_OPER_FAIL,       JS_INNER_FAIL },
76     { -ENAMETOOLONG,          JS_E_NAMETOOLONG },
77     { -EINVAL,                JS_ERR_PARAMETER_INVALID },
78     { -ENOMEM,                JS_ERR_NO_MEM },
79 };
80 
81 const std::unordered_map<int, std::string> jsErrMap = {
82     { JS_ERR_PERMISSION_DENIED, "without medialibrary permission" },
83     { JS_INNER_FAIL,            "medialibrary inner fail" },
84     { JS_ERR_PARAMETER_INVALID, "invalid parameter" },
85     { JS_E_DISPLAYNAME,         "display name invalid" },
86     { JS_ERR_NO_SUCH_FILE,      "no such file" },
87     { JS_ERR_FILE_EXIST,        "file has existed" },
88     { JS_E_FILE_TYPE,           "file type is not allow in the directory" },
89     { JS_E_FILE_KEY,            "member not exist" },
90     { JS_ERR_NO_MEM,            "cannot allocate memory" },
91     { JS_E_NAMETOOLONG,         "file name is too long" },
92     { OHOS_PERMISSION_DENIED_CODE, "Permission denied" },
93     { OHOS_INVALID_PARAM_CODE,  "invalid parameter" },
94 };
95 
96 const std::unordered_map<int32_t, int32_t> ClientErrTable {
97     { E_INVALID_DISPLAY_NAME, JS_E_DISPLAYNAME },
98     { E_FILE_NAME_INVALID, JS_E_DISPLAYNAME },
99     { E_URI_INVALID, JS_E_URI },
100     { E_INVALID_URI, JS_E_URI },
101     { E_DISTIBUTED_URI_NO_SUPPORT, JS_E_URI },
102     { E_URI_IS_NOT_ALBUM, JS_E_URI },
103     { E_DIR_CHECK_DIR_FAIL, JS_E_ROOT_DIR },
104     { E_CHECK_MEDIATYPE_MATCH_EXTENSION_FAIL, JS_E_FILE_EXTENSION },
105     { E_CHECK_MEDIATYPE_FAIL, JS_E_FILE_EXTENSION },
106     { E_CHECK_EXTENSION_FAIL, JS_E_FILE_EXTENSION },
107     { E_OPENFILE_INVALID_FLAG, JS_E_OPEN_MODE },
108     { E_NO_SUCH_FILE, JS_ERR_NO_SUCH_FILE },
109     { E_FILE_EXIST, JS_ERR_FILE_EXIST },
110     { E_DENIED_MOVE, JS_E_MOVE_DENIED },
111     { E_DENIED_RENAME, JS_E_RENAME_DENIED },
112 };
113 } // namespace Media
114 } // namespace OHOS
115 
116 
117 #endif  // OHOS_MEDIALIBRARY_CLIENT_ERRNO_H
118