修改错误码

Signed-off-by: wangjianqiang <wangjianqiang19@huawei.com>
This commit is contained in:
wangjianqiang 2022-08-08 14:25:24 +08:00
parent c3729abe61
commit 840b4a2282
8 changed files with 205 additions and 150 deletions

View File

@ -52,7 +52,7 @@ int FileAccessExtProxy::OpenFile(const Uri &uri, const int flags)
if (err != NO_ERROR) {
HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return err;
return ERR_IPC_ERROR;
}
int fd = reply.ReadFileDescriptor();
@ -100,7 +100,7 @@ int FileAccessExtProxy::CreateFile(const Uri &parent, const std::string &display
if (err != NO_ERROR) {
HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return err;
return ERR_IPC_ERROR;
}
int ret = reply.ReadInt32();
@ -156,7 +156,7 @@ int FileAccessExtProxy::Mkdir(const Uri &parent, const std::string &displayName,
if (err != NO_ERROR) {
HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return err;
return ERR_IPC_ERROR;
}
int ret = reply.ReadInt32();
@ -201,7 +201,7 @@ int FileAccessExtProxy::Delete(const Uri &sourceFile)
if (err != NO_ERROR) {
HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return err;
return ERR_IPC_ERROR;
}
int ret = reply.ReadInt32();
@ -249,7 +249,7 @@ int FileAccessExtProxy::Move(const Uri &sourceFile, const Uri &targetParent, Uri
if (err != NO_ERROR) {
HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return err;
return ERR_IPC_ERROR;
}
int ret = reply.ReadInt32();
@ -305,7 +305,7 @@ int FileAccessExtProxy::Rename(const Uri &sourceFile, const std::string &display
if (err != NO_ERROR) {
HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return err;
return ERR_IPC_ERROR;
}
int ret = reply.ReadInt32();
@ -427,7 +427,7 @@ int FileAccessExtProxy::IsFileExist(const Uri &uri, bool &isExist)
if (err != NO_ERROR) {
HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return err;
return ERR_IPC_ERROR;
}
int ret = reply.ReadInt32();

View File

@ -131,7 +131,7 @@ ErrCode FileAccessExtStub::CmdCreateFile(MessageParcel &data, MessageParcel &rep
if (displayName.empty()) {
HILOG_ERROR("parameter CreateFile displayName is nullptr");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_INVALID_PARAM;
return ERR_PARAM_FORMAT;
}
std::shared_ptr<Uri> fileNew(data.ReadParcelable<Uri>());
@ -178,7 +178,7 @@ ErrCode FileAccessExtStub::CmdMkdir(MessageParcel &data, MessageParcel &reply)
if (displayName.empty()) {
HILOG_ERROR("parameter Mkdir mode is nullptr");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_INVALID_PARAM;
return ERR_PARAM_FORMAT;
}
std::shared_ptr<Uri> fileNew(data.ReadParcelable<Uri>());
@ -299,7 +299,7 @@ ErrCode FileAccessExtStub::CmdRename(MessageParcel &data, MessageParcel &reply)
if (displayName.empty()) {
HILOG_ERROR("parameter Rename mode is nullptr");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_INVALID_PARAM;
return ERR_PARAM_FORMAT;
}
std::shared_ptr<Uri> fileNew(data.ReadParcelable<Uri>());

View File

@ -29,14 +29,13 @@ std::shared_ptr<FileAccessExtAbility> FileAccessExtStubImpl::GetOwner()
int FileAccessExtStubImpl::OpenFile(const Uri &uri, const int flags)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OpenFile");
int ret = ERR_ERROR;
if (extension_ == nullptr) {
HILOG_ERROR("OpenFile get extension failed.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
return ERR_FILEIO_FAIL;
}
ret = extension_->OpenFile(uri, flags);
int ret = extension_->OpenFile(uri, flags);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
}
@ -44,14 +43,13 @@ int FileAccessExtStubImpl::OpenFile(const Uri &uri, const int flags)
int FileAccessExtStubImpl::CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CreateFile");
int ret = ERR_ERROR;
if (extension_ == nullptr) {
HILOG_ERROR("CreateFile get extension failed.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
return ERR_FILEIO_FAIL;
}
ret = extension_->CreateFile(parent, displayName, newFile);
int ret = extension_->CreateFile(parent, displayName, newFile);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
}
@ -59,14 +57,13 @@ int FileAccessExtStubImpl::CreateFile(const Uri &parent, const std::string &disp
int FileAccessExtStubImpl::Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Mkdir");
int ret = ERR_ERROR;
if (extension_ == nullptr) {
HILOG_ERROR("Mkdir get extension failed.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
return ERR_FILEIO_FAIL;
}
ret = extension_->Mkdir(parent, displayName, newFile);
int ret = extension_->Mkdir(parent, displayName, newFile);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
}
@ -74,14 +71,13 @@ int FileAccessExtStubImpl::Mkdir(const Uri &parent, const std::string &displayNa
int FileAccessExtStubImpl::Delete(const Uri &sourceFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Delete");
int ret = ERR_ERROR;
if (extension_ == nullptr) {
HILOG_ERROR("Delete get extension failed.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
return ERR_FILEIO_FAIL;
}
ret = extension_->Delete(sourceFile);
int ret = extension_->Delete(sourceFile);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
}
@ -89,14 +85,13 @@ int FileAccessExtStubImpl::Delete(const Uri &sourceFile)
int FileAccessExtStubImpl::Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Move");
int ret = ERR_ERROR;
if (extension_ == nullptr) {
HILOG_ERROR("Move get extension failed.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
return ERR_FILEIO_FAIL;
}
ret = extension_->Move(sourceFile, targetParent, newFile);
int ret = extension_->Move(sourceFile, targetParent, newFile);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
}
@ -104,14 +99,13 @@ int FileAccessExtStubImpl::Move(const Uri &sourceFile, const Uri &targetParent,
int FileAccessExtStubImpl::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Rename");
int ret = ERR_ERROR;
if (extension_ == nullptr) {
HILOG_ERROR("Rename get extension failed.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
return ERR_FILEIO_FAIL;
}
ret = extension_->Rename(sourceFile, displayName, newFile);
int ret = extension_->Rename(sourceFile, displayName, newFile);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
}
@ -149,14 +143,13 @@ std::vector<DeviceInfo> FileAccessExtStubImpl::GetRoots()
int FileAccessExtStubImpl::IsFileExist(const Uri &uri, bool &isExist)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "IsFileExist");
int ret = ERR_ERROR;
if (extension_ == nullptr) {
HILOG_ERROR("IsFileExist get extension failed.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
return ERR_FILEIO_FAIL;
}
ret = extension_->IsFileExist(uri, isExist);
int ret = extension_->IsFileExist(uri, isExist);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
}

View File

@ -363,15 +363,14 @@ bool FileAccessHelper::GetProxy()
int FileAccessHelper::OpenFile(Uri &uri, int flags)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OpenFile");
int fd = ERR_ERROR;
sptr<IFileAccessExtBase> fileExtProxy = GetProxy(uri);
if (fileExtProxy == nullptr) {
HILOG_ERROR("failed with invalid fileAccessExtProxy");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return fd;
return ERR_FILEIO_FAIL;
}
fd = fileExtProxy->OpenFile(uri, flags);
int fd = fileExtProxy->OpenFile(uri, flags);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return fd;
}
@ -379,15 +378,14 @@ int FileAccessHelper::OpenFile(Uri &uri, int flags)
int FileAccessHelper::CreateFile(Uri &parent, const std::string &displayName, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CreateFile");
int index = ERR_ERROR;
sptr<IFileAccessExtBase> fileExtProxy = GetProxy(parent);
if (fileExtProxy == nullptr) {
HILOG_ERROR("failed with invalid fileAccessExtProxy");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
return ERR_FILEIO_FAIL;
}
index = fileExtProxy->CreateFile(parent, displayName, newFile);
int index = fileExtProxy->CreateFile(parent, displayName, newFile);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
}
@ -395,15 +393,14 @@ int FileAccessHelper::CreateFile(Uri &parent, const std::string &displayName, Ur
int FileAccessHelper::Mkdir(Uri &parent, const std::string &displayName, Uri &newDir)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Mkdir");
int index = ERR_ERROR;
sptr<IFileAccessExtBase> fileExtProxy = GetProxy(parent);
if (fileExtProxy == nullptr) {
HILOG_ERROR("failed with invalid fileAccessExtProxy");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
return ERR_FILEIO_FAIL;
}
index = fileExtProxy->Mkdir(parent, displayName, newDir);
int index = fileExtProxy->Mkdir(parent, displayName, newDir);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
}
@ -411,15 +408,14 @@ int FileAccessHelper::Mkdir(Uri &parent, const std::string &displayName, Uri &ne
int FileAccessHelper::Delete(Uri &selectFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Delete");
int index = ERR_ERROR;
sptr<IFileAccessExtBase> fileExtProxy = GetProxy(selectFile);
if (fileExtProxy == nullptr) {
HILOG_ERROR("failed with invalid fileAccessExtProxy");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
return ERR_FILEIO_FAIL;
}
index = fileExtProxy->Delete(selectFile);
int index = fileExtProxy->Delete(selectFile);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
}
@ -434,15 +430,14 @@ int FileAccessHelper::Move(Uri &sourceFile, Uri &targetParent, Uri &newFile)
return ERR_OPERATION_NOT_PERMITTED;
}
int index = ERR_ERROR;
sptr<IFileAccessExtBase> fileExtProxy = GetProxy(sourceFile);
if (fileExtProxy == nullptr) {
HILOG_ERROR("failed with invalid fileAccessExtProxy");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
return ERR_FILEIO_FAIL;
}
index = fileExtProxy->Move(sourceFile, targetParent, newFile);
int index = fileExtProxy->Move(sourceFile, targetParent, newFile);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
}
@ -450,15 +445,14 @@ int FileAccessHelper::Move(Uri &sourceFile, Uri &targetParent, Uri &newFile)
int FileAccessHelper::Rename(Uri &sourceFile, const std::string &displayName, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Rename");
int index = ERR_ERROR;
sptr<IFileAccessExtBase> fileExtProxy = GetProxy(sourceFile);
if (fileExtProxy == nullptr) {
HILOG_ERROR("failed with invalid fileAccessExtProxy");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
return ERR_FILEIO_FAIL;
}
index = fileExtProxy->Rename(sourceFile, displayName, newFile);
int index = fileExtProxy->Rename(sourceFile, displayName, newFile);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return index;
}
@ -529,16 +523,14 @@ std::vector<AAFwk::Want> FileAccessHelper::GetRegisterFileAccessExtAbilityInfo()
int FileAccessHelper::IsFileExist(Uri &uri, bool &isExist)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "IsFileExist");
int ret = ERR_ERROR;
sptr<IFileAccessExtBase> fileExtProxy = GetProxy(uri);
if (fileExtProxy == nullptr) {
HILOG_ERROR("failed with invalid fileAccessExtProxy");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
return ERR_FILEIO_FAIL;
}
ret = fileExtProxy->IsFileExist(uri, isExist);
int ret = fileExtProxy->IsFileExist(uri, isExist);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ret;
}

View File

@ -162,7 +162,7 @@ static int DoCallJsMethod(CallJsParam *param)
if (jsRuntime == nullptr) {
HILOG_ERROR("failed to get jsRuntime.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_INVALID_PARAM;
return ERR_PARAM_FORMAT;
}
HandleScope handleScope(*jsRuntime);
auto& nativeEngine = jsRuntime->GetNativeEngine();
@ -172,7 +172,7 @@ static int DoCallJsMethod(CallJsParam *param)
if (!param->argParser(nativeEngine, argv, argc)) {
HILOG_ERROR("failed to get params.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_INVALID_PARAM;
return ERR_PARAM_FORMAT;
}
}
NativeValue *value = param->jsObj->Get();
@ -196,7 +196,7 @@ static int DoCallJsMethod(CallJsParam *param)
if (param->retParser == nullptr) {
HILOG_ERROR("ResultValueParser must not null.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_INVALID_PARAM;
return ERR_PARAM_FORMAT;
}
if (!param->retParser(nativeEngine, handleScope.Escape(nativeEngine.CallFunction(value, method, argv, argc)))) {
HILOG_INFO("Parser js result fail.");
@ -281,10 +281,16 @@ void JsFileAccessExtAbility::GetSrcPath(std::string &srcPath)
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
}
template <typename T>
struct Value {
T data;
int code {0};
};
int JsFileAccessExtAbility::OpenFile(const Uri &uri, int flags)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OpenFile");
auto fd = std::make_shared<int>();
auto value = std::make_shared<Value<int>>();
auto argParser = [uri, flags](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool {
NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length());
if (nativeUri == nullptr) {
@ -301,8 +307,10 @@ int JsFileAccessExtAbility::OpenFile(const Uri &uri, int flags)
argc = ARGC_TWO;
return true;
};
auto retParser = [fd](NativeEngine &engine, NativeValue *result) -> bool {
bool ret = ConvertFromJsValue(engine, result, *fd);
auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool {
NativeObject *obj = ConvertNativeValueTo<NativeObject>(result);
bool ret = ConvertFromJsValue(engine, obj->GetProperty("fd"), value->data);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code);
if (!ret) {
HILOG_ERROR("Convert js value fail.");
}
@ -316,13 +324,13 @@ int JsFileAccessExtAbility::OpenFile(const Uri &uri, int flags)
return errCode;
}
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return *fd;
return value->data;
}
int JsFileAccessExtAbility::CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CreateFile");
auto uri = std::make_shared<std::string>();
auto value = std::make_shared<Value<std::string>>();
auto argParser = [parent, displayName](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool {
NativeValue *nativeParent = engine.CreateString(parent.ToString().c_str(), parent.ToString().length());
if (nativeParent == nullptr) {
@ -339,8 +347,10 @@ int JsFileAccessExtAbility::CreateFile(const Uri &parent, const std::string &dis
argc = ARGC_TWO;
return true;
};
auto retParser = [uri](NativeEngine &engine, NativeValue *result) -> bool {
bool ret = ConvertFromJsValue(engine, result, *uri);
auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool {
NativeObject *obj = ConvertNativeValueTo<NativeObject>(result);
bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), value->data);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code);
if (!ret) {
HILOG_ERROR("Convert js value fail.");
}
@ -353,12 +363,13 @@ int JsFileAccessExtAbility::CreateFile(const Uri &parent, const std::string &dis
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}
if ((*uri).empty()) {
if ((value->data).empty()) {
HILOG_ERROR("call CreateFile with return empty.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_ERROR;
}
newFile = Uri(*uri);
newFile = Uri(value->data);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}
@ -366,7 +377,7 @@ int JsFileAccessExtAbility::CreateFile(const Uri &parent, const std::string &dis
int JsFileAccessExtAbility::Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Mkdir");
auto uri = std::make_shared<std::string>();
auto value = std::make_shared<Value<std::string>>();
auto argParser = [parent, displayName](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool {
NativeValue *nativeParent = engine.CreateString(parent.ToString().c_str(), parent.ToString().length());
if (nativeParent == nullptr) {
@ -383,8 +394,10 @@ int JsFileAccessExtAbility::Mkdir(const Uri &parent, const std::string &displayN
argc = ARGC_TWO;
return true;
};
auto retParser = [uri](NativeEngine &engine, NativeValue *result) -> bool {
bool ret = ConvertFromJsValue(engine, result, *uri);
auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool {
NativeObject *obj = ConvertNativeValueTo<NativeObject>(result);
bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), value->data);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code);
if (!ret) {
HILOG_ERROR("Convert js value fail.");
}
@ -397,12 +410,13 @@ int JsFileAccessExtAbility::Mkdir(const Uri &parent, const std::string &displayN
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}
if ((*uri).empty()) {
if ((value->data).empty()) {
HILOG_ERROR("call Mkdir with return empty.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_ERROR;
}
newFile = Uri(*uri);
newFile = Uri(value->data);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}
@ -410,7 +424,7 @@ int JsFileAccessExtAbility::Mkdir(const Uri &parent, const std::string &displayN
int JsFileAccessExtAbility::Delete(const Uri &sourceFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Delete");
auto ret = std::make_shared<int>();
auto value = std::make_shared<Value<int>>();
auto argParser = [uri = sourceFile](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool {
NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length());
if (nativeUri == nullptr) {
@ -421,12 +435,14 @@ int JsFileAccessExtAbility::Delete(const Uri &sourceFile)
argc = ARGC_ONE;
return true;
};
auto retParser = [ret](NativeEngine &engine, NativeValue *result) -> bool {
bool res = ConvertFromJsValue(engine, result, *ret);
if (!res) {
auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool {
NativeObject *obj = ConvertNativeValueTo<NativeObject>(result);
bool ret = ConvertFromJsValue(engine, obj->GetProperty("index"), value->data);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code);
if (!ret) {
HILOG_ERROR("Convert js value fail.");
}
return res;
return ret;
};
auto errCode = CallJsMethod("delete", jsRuntime_, jsObj_.get(), argParser, retParser);
@ -436,13 +452,13 @@ int JsFileAccessExtAbility::Delete(const Uri &sourceFile)
return errCode;
}
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return *ret;
return value->data;
}
int JsFileAccessExtAbility::Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Move");
auto uri = std::make_shared<std::string>();
auto value = std::make_shared<Value<std::string>>();
auto argParser = [sourceFile, targetParent](NativeEngine &engine, NativeValue* argv[], size_t &argc) -> bool {
NativeValue *srcUri = engine.CreateString(sourceFile.ToString().c_str(),
sourceFile.ToString().length());
@ -461,8 +477,10 @@ int JsFileAccessExtAbility::Move(const Uri &sourceFile, const Uri &targetParent,
argc = ARGC_TWO;
return true;
};
auto retParser = [uri](NativeEngine &engine, NativeValue *result) -> bool {
bool ret = ConvertFromJsValue(engine, result, *uri);
auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool {
NativeObject *obj = ConvertNativeValueTo<NativeObject>(result);
bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), value->data);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code);
if (!ret) {
HILOG_ERROR("Convert js value fail.");
}
@ -475,12 +493,13 @@ int JsFileAccessExtAbility::Move(const Uri &sourceFile, const Uri &targetParent,
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}
if ((*uri).empty()) {
if ((value->data).empty()) {
HILOG_ERROR("call move with return empty.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_ERROR;
}
newFile = Uri(*uri);
newFile = Uri(value->data);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}
@ -488,7 +507,7 @@ int JsFileAccessExtAbility::Move(const Uri &sourceFile, const Uri &targetParent,
int JsFileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Rename");
auto uri = std::make_shared<std::string>();
auto value = std::make_shared<Value<std::string>>();
auto argParser = [sourceFile, displayName](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool {
NativeValue *nativeSourceFile = engine.CreateString(sourceFile.ToString().c_str(),
sourceFile.ToString().length());
@ -506,8 +525,10 @@ int JsFileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &dis
argc = ARGC_TWO;
return true;
};
auto retParser = [uri](NativeEngine &engine, NativeValue *result) -> bool {
bool ret = ConvertFromJsValue(engine, result, *uri);
auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool {
NativeObject *obj = ConvertNativeValueTo<NativeObject>(result);
bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), value->data);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code);
if (!ret) {
HILOG_ERROR("Convert js value fail.");
}
@ -520,20 +541,21 @@ int JsFileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &dis
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}
if ((*uri).empty()) {
if ((value->data).empty()) {
HILOG_ERROR("call Rename with return empty.");
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return ERR_ERROR;
}
newFile = Uri(*uri);
newFile = Uri(value->data);
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}
std::vector<FileInfo> JsFileAccessExtAbility::ListFile(const Uri &sourceFile)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "ListFile");
auto fileVec = std::make_shared<std::vector<FileInfo>>();
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "ListFile");
auto value = std::make_shared<Value<std::vector<FileInfo>>>();
auto argParser = [sourceFile](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool {
NativeValue *uri = engine.CreateString(sourceFile.ToString().c_str(), sourceFile.ToString().length());
if (uri == nullptr) {
@ -544,19 +566,16 @@ std::vector<FileInfo> JsFileAccessExtAbility::ListFile(const Uri &sourceFile)
argc = ARGC_ONE;
return true;
};
auto retParser = [fileVec](NativeEngine &engine, NativeValue *result) -> bool {
if (!result->IsArray()) {
HILOG_INFO("result is not array.");
return false;
}
NativeArray *nativeArray = ConvertNativeValueTo<NativeArray>(result);
auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool {
NativeObject *obj = ConvertNativeValueTo<NativeObject>(result);
bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), value->code);
NativeArray *nativeArray = ConvertNativeValueTo<NativeArray>(obj->GetProperty("infos"));
for (uint32_t i = 0; i < nativeArray->GetLength(); i++) {
NativeValue *nativeFileInfo = nativeArray->GetElement(i);
NativeObject *obj = ConvertNativeValueTo<NativeObject>(nativeFileInfo);
obj = ConvertNativeValueTo<NativeObject>(nativeFileInfo);
FileInfo fileInfo;
std::string uri;
bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), uri);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("uri"), uri);
fileInfo.uri = Uri(uri);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("fileName"), fileInfo.fileName);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mode"), fileInfo.mode);
@ -567,7 +586,7 @@ std::vector<FileInfo> JsFileAccessExtAbility::ListFile(const Uri &sourceFile)
HILOG_ERROR("Convert js value fail.");
return ret;
}
(*fileVec).emplace_back(std::move(fileInfo));
(value->data).emplace_back(std::move(fileInfo));
}
return true;
};
@ -577,29 +596,27 @@ std::vector<FileInfo> JsFileAccessExtAbility::ListFile(const Uri &sourceFile)
HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode);
}
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return std::move(*fileVec);
return value->data;
}
std::vector<DeviceInfo> JsFileAccessExtAbility::GetRoots()
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "GetRoots");
auto devVec = std::make_shared<std::vector<DeviceInfo>>();
auto value = std::make_shared<Value<std::vector<DeviceInfo>>>();
auto argParser = [](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool {
argc = ARGC_ZERO;
return true;
};
auto retParser = [devVec](NativeEngine &engine, NativeValue *result) -> bool {
if (!result->IsArray()) {
HILOG_INFO("result is not array.");
return false;
}
NativeArray *nativeArray = ConvertNativeValueTo<NativeArray>(result);
auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool {
NativeObject *obj = ConvertNativeValueTo<NativeObject>(result);
bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), value->code);
NativeArray *nativeArray = ConvertNativeValueTo<NativeArray>(obj->GetProperty("roots"));
for (uint32_t i = 0; i < nativeArray->GetLength(); i++) {
NativeValue *nativeDeviceInfo = nativeArray->GetElement(i);
NativeObject *obj = ConvertNativeValueTo<NativeObject>(nativeDeviceInfo);
obj = ConvertNativeValueTo<NativeObject>(nativeDeviceInfo);
DeviceInfo deviceInfo;
std::string uri;
bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), uri);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("uri"), uri);
deviceInfo.uri = Uri(uri);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("displayName"), deviceInfo.displayName);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("deviceId"), deviceInfo.deviceId);
@ -609,7 +626,7 @@ std::vector<DeviceInfo> JsFileAccessExtAbility::GetRoots()
HILOG_ERROR("Convert js value fail.");
return ret;
}
(*devVec).emplace_back(std::move(deviceInfo));
(value->data).emplace_back(std::move(deviceInfo));
}
return true;
};
@ -619,25 +636,27 @@ std::vector<DeviceInfo> JsFileAccessExtAbility::GetRoots()
HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode);
}
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return std::move(*devVec);
return value->data;
}
int JsFileAccessExtAbility::IsFileExist(const Uri &uri, bool &isExist)
{
StartTrace(HITRACE_TAG_FILEMANAGEMENT, "IsFileExist");
auto ret = std::make_shared<bool>();
auto value = std::make_shared<Value<bool>>();
auto argParser = [uri](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool {
NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length());
argv[ARGC_ZERO] = nativeUri;
argc = ARGC_ONE;
return true;
};
auto retParser = [ret](NativeEngine &engine, NativeValue *result) -> bool {
bool res = ConvertFromJsValue(engine, result, *ret);
if (!res) {
auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool {
NativeObject *obj = ConvertNativeValueTo<NativeObject>(result);
bool ret = ConvertFromJsValue(engine, obj->GetProperty("isExist"), value->data);
ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code);
if (!ret) {
HILOG_ERROR("Convert js value fail.");
}
return res;
return ret;
};
auto errCode = CallJsMethod("isFileExist", jsRuntime_, jsObj_.get(), argParser, retParser);
@ -646,7 +665,7 @@ int JsFileAccessExtAbility::IsFileExist(const Uri &uri, bool &isExist)
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}
isExist = *ret;
isExist = value->data;
FinishTrace(HITRACE_TAG_FILEMANAGEMENT);
return errCode;
}

View File

@ -24,6 +24,7 @@
#include "hilog_wrapper.h"
#include "napi_base_context.h"
#include "napi_common_fileaccess.h"
#include "napi_error.h"
#include "n_val.h"
#include "securec.h"
#include "uri.h"
@ -46,7 +47,7 @@ static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info i
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -104,7 +105,7 @@ napi_value AcquireFileAccessHelperWrap(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -175,7 +176,7 @@ napi_value NAPI_GetRegisterFileAccessExtAbilityInfo(napi_env env, napi_callback_
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -198,7 +199,7 @@ napi_value NAPI_GetRegisterFileAccessExtAbilityInfo(napi_env env, napi_callback_
}
NVal cb(env, funcArg[NARG_POS::FIRST]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "argument type unmatched");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
@ -243,14 +244,14 @@ napi_value FileAccessHelperInit(napi_env env, napi_value exports)
static FileAccessHelper *GetFileAccessHelper(napi_env env, napi_value thisVar)
{
if (thisVar == nullptr) {
NError(EINVAL).ThrowErr(env, "thisVar is nullper");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
FileAccessHelper *fileAccessHelper = nullptr;
napi_unwrap(env, thisVar, (void **)&fileAccessHelper);
if (fileAccessHelper == nullptr) {
NError(EINVAL).ThrowErr(env, "fileAccessHelper is nullper");
NapiError(ERR_GET_FILEACCESS_HELPER).ThrowErr(env);
return nullptr;
}
return fileAccessHelper;
@ -265,13 +266,13 @@ static std::tuple<bool, std::unique_ptr<char[]>, std::unique_ptr<char[]>> GetRea
std::unique_ptr<char[]> name = nullptr;
std::tie(succ, uri, std::ignore) = NVal(env, sourceFile).ToUTF8String();
if (!succ) {
NError(EINVAL).ThrowErr(env, "first parameter is Invalid");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return { false, std::move(uri), std::move(name) };
}
std::tie(succ, name, std::ignore) = NVal(env, targetParent).ToUTF8String();
if (!succ) {
NError(EINVAL).ThrowErr(env, "second parameter is Invalid");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return { false, std::move(uri), std::move(name) };
}
@ -282,7 +283,7 @@ napi_value NAPI_OpenFile(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -290,14 +291,14 @@ napi_value NAPI_OpenFile(napi_env env, napi_callback_info info)
std::unique_ptr<char[]> uri;
std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
if (!succ) {
NError(EINVAL).ThrowErr(env, "Invalid uri");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
int flags;
std::tie(succ, flags) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32();
if (!succ) {
NError(EINVAL).ThrowErr(env, "Invalid flags");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
@ -328,7 +329,7 @@ napi_value NAPI_OpenFile(napi_env env, napi_callback_info info)
NVal cb(env, funcArg[NARG_POS::THIRD]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "not function type");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
@ -338,7 +339,7 @@ napi_value NAPI_CreateFile(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -380,7 +381,7 @@ napi_value NAPI_CreateFile(napi_env env, napi_callback_info info)
NVal cb(env, funcArg[NARG_POS::THIRD]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "not function type");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
@ -390,7 +391,7 @@ napi_value NAPI_Mkdir(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -432,7 +433,7 @@ napi_value NAPI_Mkdir(napi_env env, napi_callback_info info)
NVal cb(env, funcArg[NARG_POS::THIRD]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "not function type");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
@ -442,7 +443,7 @@ napi_value NAPI_Delete(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -450,7 +451,7 @@ napi_value NAPI_Delete(napi_env env, napi_callback_info info)
std::unique_ptr<char[]> uri;
std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
if (!succ) {
NError(EINVAL).ThrowErr(env, "Invalid uri");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
@ -481,7 +482,7 @@ napi_value NAPI_Delete(napi_env env, napi_callback_info info)
NVal cb(env, funcArg[NARG_POS::SECOND]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "not function type");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
@ -491,7 +492,7 @@ napi_value NAPI_Move(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -534,7 +535,7 @@ napi_value NAPI_Move(napi_env env, napi_callback_info info)
NVal cb(env, funcArg[NARG_POS::THIRD]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "not function type");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
@ -544,7 +545,7 @@ napi_value NAPI_Rename(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -586,7 +587,7 @@ napi_value NAPI_Rename(napi_env env, napi_callback_info info)
NVal cb(env, funcArg[NARG_POS::THIRD]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "not function type");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
@ -596,7 +597,7 @@ napi_value NAPI_ListFile(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -604,7 +605,7 @@ napi_value NAPI_ListFile(napi_env env, napi_callback_info info)
std::unique_ptr<char[]> uri;
std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
if (!succ) {
NError(EINVAL).ThrowErr(env, "Invalid uri");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
@ -636,7 +637,7 @@ napi_value NAPI_ListFile(napi_env env, napi_callback_info info)
NVal cb(env, funcArg[NARG_POS::SECOND]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "not function type");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
@ -646,7 +647,7 @@ napi_value NAPI_GetRoots(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -677,7 +678,7 @@ napi_value NAPI_GetRoots(napi_env env, napi_callback_info info)
NVal cb(env, funcArg[NARG_POS::FIRST]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "not function type");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
@ -687,7 +688,7 @@ napi_value NAPI_IsFileExist(napi_env env, napi_callback_info info)
{
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
NError(EINVAL).ThrowErr(env, "Number of arguments unmatched");
NapiError(ERR_PARAM_NUMBER).ThrowErr(env);
return nullptr;
}
@ -695,7 +696,7 @@ napi_value NAPI_IsFileExist(napi_env env, napi_callback_info info)
std::unique_ptr<char[]> uri;
std::tie(succ, uri, std::ignore) = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
if (!succ) {
NError(EINVAL).ThrowErr(env, "Invalid uri");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
@ -727,7 +728,7 @@ napi_value NAPI_IsFileExist(napi_env env, napi_callback_info info)
}
NVal cb(env, funcArg[NARG_POS::SECOND]);
if (!cb.TypeIs(napi_function)) {
NError(EINVAL).ThrowErr(env, "not function type");
NapiError(ERR_PARAM_FORMAT).ThrowErr(env);
return nullptr;
}
return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;

View File

@ -23,7 +23,7 @@ namespace FileAccessFwk {
enum {
MODULE_FILE_ACCESS_FRAMEWORK = 0x01
};
constexpr ErrCode BASE_OFFSET = ErrCodeOffset(SUBSYS_FILEMANAGEMENT, MODULE_FILE_ACCESS_FRAMEWORK);
constexpr ErrCode BASE_OFFSET = -ErrCodeOffset(SUBSYS_FILEMANAGEMENT, MODULE_FILE_ACCESS_FRAMEWORK);
enum {
ERR_OK = 0,
ERR_ERROR = -1,
@ -33,9 +33,11 @@ enum {
ERR_INVALID_URI, // invalid uri
ERR_URI_CHECK, // check uri head fail
ERR_FILEIO_FAIL, // fileio fail
ERR_INVALID_PARAM, // invalid parameter
ERR_PARAM_FORMAT, // Parameter format erroe
ERR_PARAM_NUMBER, // Parameter number is abnormal
ERR_PARSER_FAIL, // parser js result error
ERR_OPERATION_NOT_PERMITTED // Operation not permitted
ERR_OPERATION_NOT_PERMITTED, // Operation not permitted
ERR_GET_FILEACCESS_HELPER // get fileAccessHelper fail
};
} // namespace FileAccessFwk
} // namespace OHOS

48
utils/napi_error.h Normal file
View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NAPI_ERROR_H
#define NAPI_ERROR_H
#include "hilog_wrapper.h"
#include "n_napi.h"
#include "n_error.h"
namespace OHOS {
namespace FileAccessFwk {
namespace {
constexpr int ERRNO_NOERR = 0;
}
class NapiError : public OHOS::FileManagement::LibN::NError {
public:
NapiError(int ePosix) : errno_(ePosix) {}
~NapiError() = default;
void ThrowErr(napi_env env)
{
napi_value tmp = nullptr;
std::string errMsg = "";
napi_get_and_clear_last_exception(env, &tmp);
// Note that ace engine cannot thow errors created by napi_create_error so far
napi_status throwStatus = napi_throw_error(env, std::to_string(errno_).c_str(), errMsg.c_str());
if (throwStatus != napi_ok) {
HILOG_WARN("Failed to throw an exception, %{public}d, code = %{public}s", throwStatus, std::to_string(errno_).c_str());
}
}
private:
int errno_ = ERRNO_NOERR;
};
}
}
#endif