diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index 2e946b33ed82ec63ce4beb9f410cdbf7afc9eb29..5c03fa628fdff69487b680ddfbbac4396d8613d9 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -21,6 +21,7 @@ #include #include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" #include "b_json/b_json_cached_entity.h" #include "b_json/b_json_entity_extension_config.h" #include "b_resources/b_constants.h" @@ -32,6 +33,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; +using namespace BExcepUltils; void ExtBackupJs::OnStart(const AAFwk::Want &want) { @@ -60,11 +62,7 @@ void ExtBackupJs::Init(const shared_ptr &record, HILOGI("Init the BackupExtensionAbility(JS)"); try { ExtBackup::Init(record, application, handler, token); - - if (!abilityInfo_) { - throw BError(BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); - } - + BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); // 获取应用扩展的 BackupExtensionAbility 的路径 const AppExecFwk::AbilityInfo &info = *abilityInfo_; string bundleName = info.bundleName; @@ -139,6 +137,7 @@ string ExtBackupJs::GetUsrConfig() const { vector config; AppExecFwk::BundleMgrClient client; + BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); const AppExecFwk::AbilityInfo &info = *abilityInfo_; if (!client.GetProfileFromAbility(info, "ohos.extension.backup", config)) { throw BError(BError::Codes::EXT_INVAL_ARG, "Failed to invoke the GetProfileFromAbility method."); @@ -166,16 +165,13 @@ BConstants::ExtensionAction ExtBackupJs::GetExtensionAction() const static BConstants::ExtensionAction VerifyAndGetAction(const AAFwk::Want &want, std::shared_ptr abilityInfo) { - if (!abilityInfo) { - string pendingMsg = "Received an empty ability. You must missed the init proc"; - throw BError(BError::Codes::EXT_INVAL_ARG, pendingMsg); - } - + string pendingMsg = "Received an empty ability. You must missed the init proc"; + BAssert(abilityInfo, BError::Codes::EXT_INVAL_ARG, pendingMsg); using namespace BConstants; ExtensionAction extAction {want.GetIntParam(EXTENSION_ACTION_PARA, static_cast(ExtensionAction::INVALID))}; if (extAction == ExtensionAction::INVALID) { int extActionInt = static_cast(extAction); - string pendingMsg = string("Want must specify a valid action instead of ").append(to_string(extActionInt)); + pendingMsg = string("Want must specify a valid action instead of ").append(to_string(extActionInt)); throw BError(BError::Codes::EXT_INVAL_ARG, pendingMsg); } @@ -186,6 +182,7 @@ sptr ExtBackupJs::OnConnect(const AAFwk::Want &want) { try { HILOGI("begin"); + BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); // 发起者必须是备份服务 auto extAction = VerifyAndGetAction(want, abilityInfo_); if (extAction_ != BConstants::ExtensionAction::INVALID && extAction == BConstants::ExtensionAction::INVALID && diff --git a/frameworks/native/backup_ext/src/ext_extension_stub.cpp b/frameworks/native/backup_ext/src/ext_extension_stub.cpp index b47515b078f8a83d2fdf08846839fbf787a0eb51..f78037b07c77a3a96890c362691f517d379f31e1 100644 --- a/frameworks/native/backup_ext/src/ext_extension_stub.cpp +++ b/frameworks/native/backup_ext/src/ext_extension_stub.cpp @@ -19,10 +19,13 @@ #include #include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::Backup { using namespace std; +using namespace BExcepUltils; + ExtExtensionStub::ExtExtensionStub() { opToInterfaceMap_[CMD_GET_FILE_HANDLE] = &ExtExtensionStub::CmdGetFileHandle; @@ -50,7 +53,7 @@ int32_t ExtExtensionStub::OnRemoteRequest(uint32_t code, return BError(BError::Codes::EXT_INVAL_ARG, "Invalid remote descriptor"); } - return BError::ExceptionCatcherLocked([&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); }); + return ExceptionCatcherLocked([&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); }); } ErrCode ExtExtensionStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply) diff --git a/utils/include/b_error/b_error.h b/utils/include/b_error/b_error.h index e12434f6589789368e77c9b89f56bf6c360ec3b4..058361d2bda372b4fa5f941b53feaf52233d2273 100644 --- a/utils/include/b_error/b_error.h +++ b/utils/include/b_error/b_error.h @@ -114,14 +114,6 @@ public: return msg_.c_str(); } - /** - * @brief 异常捕获 - * - * @param callBack 回调 - * @return ErrCode 错误码 - */ - static ErrCode ExceptionCatcherLocked(std::function callBack); - public: /** * @brief 重载bool操作符,判断当前错误是否是错误 diff --git a/utils/include/b_error/b_excep_utils.h b/utils/include/b_error/b_excep_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..57e1baaa68fcfa7b10b6effd885c1756adccb950 --- /dev/null +++ b/utils/include/b_error/b_excep_utils.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 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 OHOS_FILEMGMT_BACKUP_B_EXCEP_UTILES_H +#define OHOS_FILEMGMT_BACKUP_B_EXCEP_UTILES_H + +#include "b_error/b_error.h" + +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::Backup::BExcepUltils { +/** + * @brief 异常捕获 + * + * @param callBack 回调 + * @return ErrCode 错误码 + */ +[[maybe_unused]] static ErrCode ExceptionCatcherLocked(std::function callBack) +{ + try { + return callBack(); + } catch (const BError &e) { + return e.GetCode(); + } catch (const std::exception &e) { + HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); + return EPERM; + } catch (...) { + HILOGE("Unexpected exception"); + return EPERM; + } +} + +/** + * @brief 检查 AbilityInfo 是否有效 + * + * @param AbilityInfo + * @param code 错误码 + * @param msg 错误信息 + * @return 无 + */ +template +[[maybe_unused]] static void BAssert(const T &t, const BError::Codes &code, const std::string_view msg = "") +{ + if (!t) { + if (msg.empty()) { + throw BError(code); + } else { + throw BError(code, msg); + } + } +} +} // namespace OHOS::FileManagement::Backup::BExcepUltils +#endif // OHOS_FILEMGMT_BACKUP_B_EXCEP_UTILES_H \ No newline at end of file diff --git a/utils/src/b_error/b_error.cpp b/utils/src/b_error/b_error.cpp index f9745d5d8da257dbeafbf9e21e846a37cfa980e3..e6e108153f080d1da12e6476ca3a8d0720524def 100644 --- a/utils/src/b_error/b_error.cpp +++ b/utils/src/b_error/b_error.cpp @@ -49,19 +49,4 @@ string BError::WrapMessageWithExtraInfos(const char *fileName, HiviewDFX::HiLog::Error(FILEMGMT_LOG_LABEL, "%{public}s", res.c_str()); return res; } - -ErrCode BError::ExceptionCatcherLocked(std::function callBack) -{ - try { - return callBack(); - } catch (const BError &e) { - return e.GetCode(); - } catch (const exception &e) { - HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); - return EPERM; - } catch (...) { - HILOGE("Unexpected exception"); - return EPERM; - } -} } // namespace OHOS::FileManagement::Backup \ No newline at end of file