diff options
author | hyunho <hhstark.kang@samsung.com> | 2019-05-21 08:56:11 +0900 |
---|---|---|
committer | hyunho <hhstark.kang@samsung.com> | 2019-05-21 10:39:24 +0900 |
commit | 21b4103de72fd5b925f0cb73ba71ba25cbe9d7a8 (patch) | |
tree | bf64bf9d954c79ae1883e3b0c2495194295c4e6f | |
parent | 7a3b24dc09d624b95adcc6275b598544a8bcef32 (diff) | |
download | bundle-21b4103de72fd5b925f0cb73ba71ba25cbe9d7a8.tar.gz bundle-21b4103de72fd5b925f0cb73ba71ba25cbe9d7a8.tar.bz2 bundle-21b4103de72fd5b925f0cb73ba71ba25cbe9d7a8.zip |
Add error handling logic
-Throw exception for contructor error
-Add log for errors
Change-Id: I76c3621cfc10a4ef8586f151a2268b082ed4117c
Signed-off-by: hyunho <hhstark.kang@samsung.com>
-rw-r--r-- | src/bundle_cpp.cc | 52 |
1 files changed, 45 insertions, 7 deletions
diff --git a/src/bundle_cpp.cc b/src/bundle_cpp.cc index 5a5001d..2e6f30b 100644 --- a/src/bundle_cpp.cc +++ b/src/bundle_cpp.cc @@ -42,25 +42,37 @@ Bundle::Impl::~Impl() = default; Bundle::Bundle() : impl_(new Impl(this)) { impl_->handle_ = bundle_create(); + if (impl_->handle_ == nullptr) + throw std::bad_alloc(); } Bundle::Bundle(BundleRaw raw) : impl_(new Impl(this)) { impl_->handle_ = bundle_decode(raw.first.get(), raw.second); + if (impl_->handle_ == nullptr) + throw std::bad_alloc(); } Bundle::Bundle(const std::string& raw) : impl_(new Impl(this)) { impl_->handle_ = bundle_decode(reinterpret_cast<const bundle_raw*>( raw.c_str()), raw.length()); + if (impl_->handle_ == nullptr) + throw std::bad_alloc(); } Bundle::Bundle(bundle* b, bool copy, bool own) : impl_(new Impl(this, copy, own)) { - if (!impl_->copy_) + if (b == nullptr) + throw std::invalid_argument("b cannot be null"); + + if (!impl_->copy_) { impl_->handle_ = b; - else + } else { impl_->handle_ = bundle_dup(b); + if (impl_->handle_ == nullptr) + throw std::bad_alloc(); + } } Bundle::~Bundle() { @@ -71,6 +83,8 @@ Bundle::~Bundle() { Bundle::Bundle(const Bundle& b) : impl_(new Impl(this)) { impl_->handle_ = bundle_dup(b.impl_->handle_); + if (impl_->handle_ == nullptr) + throw std::bad_alloc(); } Bundle::KeyInfo::KeyInfo(const bundle_keyval_t* handle, std::string name) @@ -98,12 +112,16 @@ Bundle::KeyInfo::KeyInfo(const KeyInfo& k) : impl_(new Impl(this)) { impl_->handle_ = bundle_keyval_dup(k.impl_->handle_); impl_->name_ = k.impl_->name_; + if (impl_->handle_ == nullptr) + throw std::bad_alloc(); } Bundle::KeyInfo& Bundle::KeyInfo::operator = (const Bundle::KeyInfo& k) { if (this != &k) { impl_->handle_ = bundle_keyval_dup(k.impl_->handle_); impl_->name_ = k.impl_->name_; + if (impl_->handle_ == nullptr) + throw std::bad_alloc(); } return *this; } @@ -143,6 +161,8 @@ const std::string& Bundle::KeyInfo::GetName() const { Bundle& Bundle::operator = (const Bundle& b) { if (this != &b) { impl_->handle_ = bundle_dup(b.impl_->handle_); + if (impl_->handle_ == nullptr) + throw std::bad_alloc(); } return *this; } @@ -174,7 +194,10 @@ std::vector<Bundle::KeyInfo> Bundle::GetKeys() { } int Bundle::Add(const std::string& key, const std::string& val) { - return bundle_add_str(impl_->handle_, key.c_str(), val.c_str()); + int ret = bundle_add_str(impl_->handle_, key.c_str(), val.c_str()); + if (ret != BUNDLE_ERROR_NONE) + LOGE("Add fail key(%s), val(%s), ret(%d)", key.c_str(), val.c_str(), ret); + return ret; } int Bundle::Add(const std::string& key, const std::vector<std::string>& val) { @@ -183,15 +206,26 @@ int Bundle::Add(const std::string& key, const std::vector<std::string>& val) { v.push_back(i.c_str()); } - return bundle_add_str_array(impl_->handle_, key.c_str(), v.data(), v.size()); + int ret = bundle_add_str_array( + impl_->handle_, key.c_str(), v.data(), v.size()); + if (ret != BUNDLE_ERROR_NONE) + LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret); + + return ret; } int Bundle::Add(const std::string& key, const std::vector<unsigned char>& val) { - return bundle_add_byte(impl_->handle_, key.c_str(), val.data(), val.size()); + int ret = bundle_add_byte(impl_->handle_, key.c_str(), val.data(), val.size()); + if (ret != BUNDLE_ERROR_NONE) + LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret); + return ret; } int Bundle::Delete(const std::string& key) { - return bundle_del(impl_->handle_, key.c_str()); + int ret = bundle_del(impl_->handle_, key.c_str()); + if (ret != BUNDLE_ERROR_NONE) + LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret); + return ret; } std::string Bundle::GetString(const std::string& key) const { @@ -230,7 +264,11 @@ std::vector<unsigned char> Bundle::GetByte(const std::string& key) const { Bundle::BundleRaw Bundle::ToRaw() { bundle_raw* raw = nullptr; int len = 0; - bundle_encode(impl_->handle_, &raw, &len); + int ret = bundle_encode(impl_->handle_, &raw, &len); + if (raw == nullptr) { + LOGE("Fail to encode data (%d)", ret); + throw std::bad_alloc(); + } return BundleRaw( std::unique_ptr<bundle_raw, decltype(std::free)*>(raw, std::free), len); |