From fccbd3644e09ca311433e1e24bd45db1ffeffae8 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Tue, 22 Nov 2022 10:42:49 +0800 Subject: [PATCH 1/2] libn_add_errcode Signed-off-by: zhuhongtao666 --- utils/filemgmt_libn/include/n_error.h | 178 ++++++++++++++++++++++++++ utils/filemgmt_libn/src/n_error.cpp | 97 ++++++++++++-- 2 files changed, 265 insertions(+), 10 deletions(-) diff --git a/utils/filemgmt_libn/include/n_error.h b/utils/filemgmt_libn/include/n_error.h index 82343d367..f91722ccd 100644 --- a/utils/filemgmt_libn/include/n_error.h +++ b/utils/filemgmt_libn/include/n_error.h @@ -19,13 +19,189 @@ #include #include #include +#include #include "n_napi.h" namespace OHOS { namespace FileManagement { namespace LibN { + constexpr int ERRNO_NOERR = 0; +constexpr int STORAGE_SERVICE_SYS_CAP_TAG = 13600000; +constexpr int FILEIO_SYS_CAP_TAG = 13900000; +constexpr int USER_FILE_MANAGER_SYS_CAP_TAG = 14000000; +constexpr int USER_FILE_SERVICE_SYS_CAP_TAG = 14000000; +const std::string FILEIO_TAG_ERR_CODE = "code"; +const std::string FILEIO_TAG_ERR_MSG = "message"; + +enum ErrCodeSuffixOfFileIO { + E_PERM = 1, + E_NOENT, + E_SRCH, + E_INTR, + E_IO, + E_NXIO, + E_2BIG, + E_BADF, + E_CHILD, + E_AGAIN, + E_NOMEM, + E_ACCES, + E_FAULT, + E_BUSY, + E_EXIST, + E_XDEV, + E_NODEV, + E_NOTDIR, + E_ISDIR, + E_INVAL, + E_NFILE, + E_MFILE, + E_TXTBSY, + E_FBIG, + E_NOSPC, + E_SPIPE, + E_ROFS, + E_MLINK, + E_DEADLK, + E_NAMETOOLONG, + E_NOSYS, + E_NOTEMPTY, + E_LOOP, + E_WOULDBLOCK, + E_BADR, + E_NOSTR, + E_NODATA, + E_OVERFLOW, + E_BADFD, + E_RESTART, + E_DQUOT, + E_UKERR +}; + +enum ErrCodeSuffixOfUserFileManager { + E_DISPLAYNAME = 1, + E_URIM, + E_SUFFIX, + E_TRASH +}; + +enum ErrCodeSuffixOfStorageService { + E_IPCSS = 1, + E_SUPPORTEDFS, + E_MOUNT, + E_UNMOUNT, + E_VOLUMESTATE, + E_PREPARE, + E_DELETE, + E_NOOBJECT, + E_OUTOFRANGE +}; + +enum ErrCodeSuffixOfUserFileService { + E_IPCS = 1, + E_URIS, + E_GETINFO, + E_GETRESULT, + E_REGISTER, + E_REMOVE, + E_INIT, + E_NOTIFY +}; + +enum CommonErrCode { + E_PERMISSION = 201, + E_PERMISSION_SYS = 202, + E_PARAMS = 401, + E_DEVICENOTSUPPORT = 801, + E_OSNOTSUPPORT = 901 +}; + +const std::unordered_map> errCodeTable { + { EPERM, { FILEIO_SYS_CAP_TAG + E_PERM, "Operation not permitted" } }, + { ENOENT, { FILEIO_SYS_CAP_TAG + E_NOENT, "No such file or directory" } }, + { ESRCH, { FILEIO_SYS_CAP_TAG + E_SRCH, "No such process" } }, + { EINTR, { FILEIO_SYS_CAP_TAG + E_INTR, "Interrupted system call" } }, + { EIO, { FILEIO_SYS_CAP_TAG + E_IO, "I/O error" } }, + { ENXIO, { FILEIO_SYS_CAP_TAG + E_NXIO, "No such device or address" } }, + { E2BIG, { FILEIO_SYS_CAP_TAG + E_2BIG, "Arg list too long" } }, + { EBADF, { FILEIO_SYS_CAP_TAG + E_BADF, "Bad file descriptor" } }, + { ECHILD, { FILEIO_SYS_CAP_TAG + E_CHILD, "No child processes" } }, + { EAGAIN, { FILEIO_SYS_CAP_TAG + E_AGAIN, "Try again" } }, + { ENOMEM, { FILEIO_SYS_CAP_TAG + E_NOMEM, "Out of memory" } }, + { EACCES, { FILEIO_SYS_CAP_TAG + E_ACCES, "Permission denied" } }, + { EFAULT, { FILEIO_SYS_CAP_TAG + E_FAULT, "Bad address" } }, + { EBUSY, { FILEIO_SYS_CAP_TAG + E_BUSY, "Device or resource busy" } }, + { EEXIST, { FILEIO_SYS_CAP_TAG + E_EXIST, "File exists" } }, + { EXDEV, { FILEIO_SYS_CAP_TAG + E_XDEV, "Cross-device link" } }, + { ENODEV, { FILEIO_SYS_CAP_TAG + E_NODEV, "No such device" } }, + { ENOTDIR, { FILEIO_SYS_CAP_TAG + E_NOTDIR, "Not a directory" } }, + { EISDIR, { FILEIO_SYS_CAP_TAG + E_ISDIR, "Is a directory" } }, + { EINVAL, { FILEIO_SYS_CAP_TAG + E_INVAL, "Invalid argument" } }, + { ENFILE, { FILEIO_SYS_CAP_TAG + E_NFILE, "File table overflow" } }, + { EMFILE, { FILEIO_SYS_CAP_TAG + E_MFILE, "Too many open files" } }, + { ETXTBSY, { FILEIO_SYS_CAP_TAG + E_TXTBSY, "Text file busy" } }, + { EFBIG, { FILEIO_SYS_CAP_TAG + E_FBIG, "File too large" } }, + { ENOSPC, { FILEIO_SYS_CAP_TAG + E_NOSPC, "No space left on device" } }, + { ESPIPE, { FILEIO_SYS_CAP_TAG + E_SPIPE, "Illegal seek" } }, + { EROFS, { FILEIO_SYS_CAP_TAG + E_ROFS, "Read-only file system" } }, + { EMLINK, { FILEIO_SYS_CAP_TAG + E_MLINK, "Too many links" } }, + { EDEADLK, { FILEIO_SYS_CAP_TAG + E_DEADLK, "Resource deadlock would occur" } }, + { ENAMETOOLONG, { FILEIO_SYS_CAP_TAG + E_NAMETOOLONG, "File name too long" } }, + { ENOSYS, { FILEIO_SYS_CAP_TAG + E_NOSYS, "Function not implemented" } }, + { ENOTEMPTY, { FILEIO_SYS_CAP_TAG + E_NOTEMPTY, "Directory not empty" } }, + { ELOOP, { FILEIO_SYS_CAP_TAG + E_LOOP, "Too many symbolic links encountered" } }, + { EWOULDBLOCK, { FILEIO_SYS_CAP_TAG + E_WOULDBLOCK, "Operation would block" } }, + { EBADR, { FILEIO_SYS_CAP_TAG + E_BADR, "Invalid request descriptor" } }, + { ENOSTR, { FILEIO_SYS_CAP_TAG + E_NOSTR, "Device not a stream" } }, + { ENODATA, { FILEIO_SYS_CAP_TAG + E_NODATA, "No data available" } }, + { EOVERFLOW, { FILEIO_SYS_CAP_TAG + E_OVERFLOW, "Value too large for defined data type" } }, + { EBADFD, { FILEIO_SYS_CAP_TAG + E_BADFD, "File descriptor in bad state" } }, + { ERESTART, { FILEIO_SYS_CAP_TAG + E_RESTART, "Interrupted system call should be restarted" } }, + { EDQUOT, { FILEIO_SYS_CAP_TAG + E_DQUOT, "Quota exceeded" } }, + { -1, { FILEIO_SYS_CAP_TAG + E_UKERR, "Unknown error" } }, + { USER_FILE_MANAGER_SYS_CAP_TAG + E_DISPLAYNAME, { USER_FILE_MANAGER_SYS_CAP_TAG + E_DISPLAYNAME, + "Invalid display name" } }, + { USER_FILE_MANAGER_SYS_CAP_TAG + E_URIM, { USER_FILE_MANAGER_SYS_CAP_TAG + E_URIM, "Invalid uri" } }, + { USER_FILE_MANAGER_SYS_CAP_TAG + E_SUFFIX, { USER_FILE_MANAGER_SYS_CAP_TAG + E_SUFFIX, + "Invalid file extension" } }, + { USER_FILE_MANAGER_SYS_CAP_TAG + E_TRASH, { USER_FILE_MANAGER_SYS_CAP_TAG + E_TRASH, + "File has been put into trash bin" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_IPCSS, { STORAGE_SERVICE_SYS_CAP_TAG + E_IPCSS, "IPC error" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_SUPPORTEDFS, { STORAGE_SERVICE_SYS_CAP_TAG + E_SUPPORTEDFS, + "Not supported filesystem" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_MOUNT, { STORAGE_SERVICE_SYS_CAP_TAG + E_MOUNT, "Failed to mount" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_UNMOUNT, { STORAGE_SERVICE_SYS_CAP_TAG + E_UNMOUNT, "Failed to unmount" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_VOLUMESTATE, { STORAGE_SERVICE_SYS_CAP_TAG + E_VOLUMESTATE, + "Incorrect volume state" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_PREPARE, { STORAGE_SERVICE_SYS_CAP_TAG + E_PREPARE, + "Prepare directory or node error" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_DELETE, { STORAGE_SERVICE_SYS_CAP_TAG + E_DELETE, + "Delete directory or node error" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_NOOBJECT, { STORAGE_SERVICE_SYS_CAP_TAG + E_NOOBJECT, "No such object" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_OUTOFRANGE, { STORAGE_SERVICE_SYS_CAP_TAG + E_OUTOFRANGE, + "User id out of range" } }, + { STORAGE_SERVICE_SYS_CAP_TAG + E_NOOBJECT, { STORAGE_SERVICE_SYS_CAP_TAG + E_NOOBJECT, "No such object" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_IPCS, { USER_FILE_SERVICE_SYS_CAP_TAG + E_IPCS, "IPC error" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_URIS, { USER_FILE_SERVICE_SYS_CAP_TAG + E_URIS, "Invalid uri" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_GETINFO, { USER_FILE_SERVICE_SYS_CAP_TAG + E_GETINFO, + "Fail to get fileextension info" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_GETRESULT, { USER_FILE_SERVICE_SYS_CAP_TAG + E_GETRESULT, + "Get wrong result" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_REGISTER, { USER_FILE_SERVICE_SYS_CAP_TAG + E_REGISTER, + "Fail to register notification" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_REMOVE, { USER_FILE_SERVICE_SYS_CAP_TAG + E_REMOVE, + "Fail to remove notification" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_INIT, { USER_FILE_SERVICE_SYS_CAP_TAG + E_INIT, + "Fail to init notification agent" } }, + { USER_FILE_SERVICE_SYS_CAP_TAG + E_NOTIFY, { USER_FILE_SERVICE_SYS_CAP_TAG + E_NOTIFY, "Fail to notify agent" } }, + { E_PERMISSION, { E_PERMISSION, "Permission verification failed" } }, + { E_PERMISSION_SYS, { E_PERMISSION, "The caller is not a system application" } }, + { E_PARAMS, { E_PERMISSION, "The input parameter is invalid" } }, + { E_DEVICENOTSUPPORT, { E_PERMISSION, "The device doesn't support this api" } }, + { E_OSNOTSUPPORT, { E_PERMISSION, "The os doesn't support this api" } }, +}; class NError { public: @@ -35,7 +211,9 @@ public: ~NError() = default; explicit operator bool() const; napi_value GetNapiErr(napi_env env); + napi_value GetNapiErr(napi_env env, int code); void ThrowErr(napi_env env); + void ThrowErr(napi_env env, int code); void ThrowErr(napi_env env, std::string errMsg); private: diff --git a/utils/filemgmt_libn/src/n_error.cpp b/utils/filemgmt_libn/src/n_error.cpp index 88fff79c0..b84da571e 100644 --- a/utils/filemgmt_libn/src/n_error.cpp +++ b/utils/filemgmt_libn/src/n_error.cpp @@ -17,14 +17,37 @@ #include -#include "n_val.h" #include "filemgmt_libhilog.h" +#include "n_val.h" namespace OHOS { namespace FileManagement { namespace LibN { using namespace std; +static napi_value GenerateBusinessError(napi_env env, int32_t errCode, string errMsg) +{ + napi_value businessError = nullptr; + napi_value code = nullptr; + napi_value msg = nullptr; + napi_status status; + code = NVal::CreateInt32(env, errCode).val_; + msg = NVal::CreateUTF8String(env, errMsg).val_; + status = napi_create_error(env, nullptr, msg, &businessError); + if (status != napi_ok) { + HILOGE("Failed to create a businesserror, error message is %{public}s", errMsg.c_str()); + } + status = napi_set_named_property(env, businessError, FILEIO_TAG_ERR_CODE.c_str(), code); + if (status != napi_ok) { + HILOGE("Failed to set code property on error, error message is %{public}s", errMsg.c_str()); + } + status = napi_set_named_property(env, businessError, FILEIO_TAG_ERR_MSG.c_str(), msg); + if (status != napi_ok) { + HILOGE("Failed to set msg property on error, error message is %{public}s", errMsg.c_str()); + } + return businessError; +} + NError::NError() {} NError::NError(int ePosix) : errno_(ePosix), errMsg_(strerror(errno_)) {} @@ -41,15 +64,55 @@ NError::operator bool() const napi_value NError::GetNapiErr(napi_env env) { - napi_value code = NVal::CreateUTF8String(env, to_string(errno_)).val_; - napi_value msg = NVal::CreateUTF8String(env, errMsg_).val_; + if (errno_ == ERRNO_NOERR) { + return nullptr; + } + int32_t code; + string msg; + if (errCodeTable.find(errno_) != errCodeTable.end()) { + code = errCodeTable.at(errno_).first; + msg = errCodeTable.at(errno_).second; + } else { + code = errCodeTable.at(-1).first; + msg = errCodeTable.at(-1).second; + } + return GenerateBusinessError(env, code, msg); +} + +napi_value NError::GetNapiErr(napi_env env, int errCode) +{ + if (errCode == ERRNO_NOERR) { + return nullptr; + } + int32_t code; + string msg; + if (errCodeTable.find(errCode) != errCodeTable.end()) { + code = errCodeTable.at(errCode).first; + msg = errCodeTable.at(errCode).second; + } else { + code = errCodeTable.at(-1).first; + msg = errCodeTable.at(-1).second; + } + errno_ = errCode; + errMsg_ = msg; + return GenerateBusinessError(env, code, msg); +} - napi_value res = nullptr; - napi_status createRes = napi_create_error(env, code, msg, &res); - if (createRes) { - HILOGE("Failed to create an exception, msg = %{public}s", errMsg_.c_str()); +void NError::ThrowErr(napi_env env, int errCode) +{ + int32_t code; + string msg; + if (errCodeTable.find(errCode) != errCodeTable.end()) { + code = errCodeTable.at(errCode).first; + msg = errCodeTable.at(errCode).second; + } else { + code = errCodeTable.at(-1).first; + msg = errCodeTable.at(-1).second; + } + napi_status status = napi_throw(env, GenerateBusinessError(env, code, msg)); + if (status != napi_ok) { + HILOGE("Failed to throw a businesserror, err message is %{public}s", msg.c_str()); } - return res; } void NError::ThrowErr(napi_env env, string errMsg) @@ -59,13 +122,27 @@ void NError::ThrowErr(napi_env env, string errMsg) // Note that ace engine cannot thow errors created by napi_create_error so far napi_status throwStatus = napi_throw_error(env, nullptr, errMsg.c_str()); if (throwStatus != napi_ok) { - HILOGE("Failed to throw an exception, %{public}d, code = %{public}s", throwStatus, errMsg.c_str()); + HILOGE("Failed to throw an error, error message is %{public}s", errMsg.c_str()); } } void NError::ThrowErr(napi_env env) { - ThrowErr(env, errMsg_); + napi_value tmp = nullptr; + napi_get_and_clear_last_exception(env, &tmp); + int32_t code; + string msg; + if (errCodeTable.find(errno_) != errCodeTable.end()) { + code = errCodeTable.at(errno_).first; + msg = errCodeTable.at(errno_).second; + } else { + code = errCodeTable.at(-1).first; + msg = errCodeTable.at(-1).second; + } + napi_status status = napi_throw(env, GenerateBusinessError(env, code, msg)); + if (status != napi_ok) { + HILOGE("Failed to throw a businesserror, error message is %{public}s", msg.c_str()); + } } } // namespace LibN } // namespace FileManagement -- Gitee From bc6bae76b959442ea1cb05d847303773734d3192 Mon Sep 17 00:00:00 2001 From: zhuhongtao666 Date: Wed, 7 Dec 2022 09:31:23 +0800 Subject: [PATCH 2/2] fileio_libn_add_errcode Signed-off-by: zhuhongtao666 --- utils/filemgmt_libn/include/n_error.h | 4 +-- utils/filemgmt_libn/src/n_error.cpp | 45 +++++++++++---------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/utils/filemgmt_libn/include/n_error.h b/utils/filemgmt_libn/include/n_error.h index f91722ccd..862a07523 100644 --- a/utils/filemgmt_libn/include/n_error.h +++ b/utils/filemgmt_libn/include/n_error.h @@ -31,9 +31,8 @@ constexpr int ERRNO_NOERR = 0; constexpr int STORAGE_SERVICE_SYS_CAP_TAG = 13600000; constexpr int FILEIO_SYS_CAP_TAG = 13900000; constexpr int USER_FILE_MANAGER_SYS_CAP_TAG = 14000000; -constexpr int USER_FILE_SERVICE_SYS_CAP_TAG = 14000000; +constexpr int USER_FILE_SERVICE_SYS_CAP_TAG = 14300000; const std::string FILEIO_TAG_ERR_CODE = "code"; -const std::string FILEIO_TAG_ERR_MSG = "message"; enum ErrCodeSuffixOfFileIO { E_PERM = 1, @@ -119,6 +118,7 @@ enum CommonErrCode { }; const std::unordered_map> errCodeTable { + { ERRNO_NOERR, { ERRNO_NOERR, "No error imformation" } }, { EPERM, { FILEIO_SYS_CAP_TAG + E_PERM, "Operation not permitted" } }, { ENOENT, { FILEIO_SYS_CAP_TAG + E_NOENT, "No such file or directory" } }, { ESRCH, { FILEIO_SYS_CAP_TAG + E_SRCH, "No such process" } }, diff --git a/utils/filemgmt_libn/src/n_error.cpp b/utils/filemgmt_libn/src/n_error.cpp index b84da571e..ced3b0b95 100644 --- a/utils/filemgmt_libn/src/n_error.cpp +++ b/utils/filemgmt_libn/src/n_error.cpp @@ -36,21 +36,28 @@ static napi_value GenerateBusinessError(napi_env env, int32_t errCode, string er status = napi_create_error(env, nullptr, msg, &businessError); if (status != napi_ok) { HILOGE("Failed to create a businesserror, error message is %{public}s", errMsg.c_str()); + return nullptr; } status = napi_set_named_property(env, businessError, FILEIO_TAG_ERR_CODE.c_str(), code); if (status != napi_ok) { HILOGE("Failed to set code property on error, error message is %{public}s", errMsg.c_str()); - } - status = napi_set_named_property(env, businessError, FILEIO_TAG_ERR_MSG.c_str(), msg); - if (status != napi_ok) { - HILOGE("Failed to set msg property on error, error message is %{public}s", errMsg.c_str()); + return nullptr; } return businessError; } NError::NError() {} -NError::NError(int ePosix) : errno_(ePosix), errMsg_(strerror(errno_)) {} +NError::NError(int errCode) +{ + if (errCodeTable.find(errCode) != errCodeTable.end()) { + errno_ = errCodeTable.at(errCode).first; + errMsg_ = errCodeTable.at(errCode).second; + } else { + errno_ = errCode; + errMsg_ = "Unknown error"; + } +} NError::NError(std::function()> errGen) { @@ -67,16 +74,7 @@ napi_value NError::GetNapiErr(napi_env env) if (errno_ == ERRNO_NOERR) { return nullptr; } - int32_t code; - string msg; - if (errCodeTable.find(errno_) != errCodeTable.end()) { - code = errCodeTable.at(errno_).first; - msg = errCodeTable.at(errno_).second; - } else { - code = errCodeTable.at(-1).first; - msg = errCodeTable.at(-1).second; - } - return GenerateBusinessError(env, code, msg); + return GenerateBusinessError(env, errno_, errMsg_); } napi_value NError::GetNapiErr(napi_env env, int errCode) @@ -93,7 +91,7 @@ napi_value NError::GetNapiErr(napi_env env, int errCode) code = errCodeTable.at(-1).first; msg = errCodeTable.at(-1).second; } - errno_ = errCode; + errno_ = code; errMsg_ = msg; return GenerateBusinessError(env, code, msg); } @@ -109,6 +107,8 @@ void NError::ThrowErr(napi_env env, int errCode) code = errCodeTable.at(-1).first; msg = errCodeTable.at(-1).second; } + errno_ = code; + errMsg_ = msg; napi_status status = napi_throw(env, GenerateBusinessError(env, code, msg)); if (status != napi_ok) { HILOGE("Failed to throw a businesserror, err message is %{public}s", msg.c_str()); @@ -130,18 +130,9 @@ void NError::ThrowErr(napi_env env) { napi_value tmp = nullptr; napi_get_and_clear_last_exception(env, &tmp); - int32_t code; - string msg; - if (errCodeTable.find(errno_) != errCodeTable.end()) { - code = errCodeTable.at(errno_).first; - msg = errCodeTable.at(errno_).second; - } else { - code = errCodeTable.at(-1).first; - msg = errCodeTable.at(-1).second; - } - napi_status status = napi_throw(env, GenerateBusinessError(env, code, msg)); + napi_status status = napi_throw(env, GenerateBusinessError(env, errno_, errMsg_)); if (status != napi_ok) { - HILOGE("Failed to throw a businesserror, error message is %{public}s", msg.c_str()); + HILOGE("Failed to throw a businesserror, error message is %{public}s", errMsg_.c_str()); } } } // namespace LibN -- Gitee