github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/common/exception_queue.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #include "common/exception_queue.h"
    21  #include "common/configuration.h"
    22  #include "common/ir_conf_reader.h"
    23  #include "fs/storage.h"
    24  
    25  namespace neb {
    26  void exception_queue::push_back(neb_exception::neb_exception_type type,
    27                                  const char *what) {
    28    std::unique_lock<std::mutex> _l(m_mutex);
    29    bool was_empty = m_exceptions.empty();
    30    m_exceptions.push_back(std::make_shared<neb_exception>(type, what));
    31    _l.unlock();
    32    if (was_empty) {
    33      m_cond_var.notify_one();
    34    }
    35  }
    36  void exception_queue::push_back(const std::exception &p) {
    37    std::unique_lock<std::mutex> _l(m_mutex);
    38    bool was_empty = m_exceptions.empty();
    39    m_exceptions.push_back(std::make_shared<neb_exception>(
    40        neb_exception::neb_std_exception, p.what()));
    41    _l.unlock();
    42    if (was_empty) {
    43      m_cond_var.notify_one();
    44    }
    45  }
    46  
    47  neb_exception_ptr exception_queue::pop_front() {
    48    std::unique_lock<std::mutex> _l(m_mutex);
    49  
    50    if (m_exceptions.empty()) {
    51      m_cond_var.wait(_l);
    52    }
    53    if (m_exceptions.empty()) {
    54      return nullptr;
    55    }
    56    neb_exception_ptr ret = m_exceptions.front();
    57    m_exceptions.erase(m_exceptions.begin());
    58    return ret;
    59  }
    60  
    61  void exception_queue::catch_exception(const std::function<void()> &func) {
    62  #define EC(a)                                                                  \
    63    LOG(ERROR) << "got exception: " << e.what();                                 \
    64    exception_queue::instance().push_back(neb_exception::a, e.what());
    65  
    66    try {
    67      func();
    68    } catch (const neb::configure_general_failure &e) {
    69      EC(neb_configure_general_failure);
    70    } catch (const neb::json_general_failure &e) {
    71      EC(neb_json_general_failure);
    72    } catch (const neb::fs::storage_exception_no_such_key &e) {
    73      EC(neb_storage_exception_no_such_key);
    74    } catch (const neb::fs::storage_exception_no_init &e) {
    75      EC(neb_storage_exception_no_init);
    76    } catch (const neb::fs::storage_general_failure &e) {
    77      EC(neb_storage_general_failure);
    78    } catch (const std::exception &e) {
    79      exception_queue::instance().push_back(e);
    80    }
    81  
    82  #undef EC
    83  }
    84  } // namespace neb