Add error handling logic 07/206507/3
authorhyunho <hhstark.kang@samsung.com>
Mon, 20 May 2019 23:56:11 +0000 (08:56 +0900)
committerhyunho <hhstark.kang@samsung.com>
Tue, 21 May 2019 01:39:24 +0000 (10:39 +0900)
-Throw exception for contructor error
-Add log for errors

Change-Id: I76c3621cfc10a4ef8586f151a2268b082ed4117c
Signed-off-by: hyunho <hhstark.kang@samsung.com>
src/bundle_cpp.cc

index 5a5001d..2e6f30b 100644 (file)
@@ -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);