Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @file error.h |
| 13 | * @brief This file defines basic elements for the library reporting |
| 14 | * |
| 15 | * The library throws a number of exceptions. |
| 16 | * In general, the following example shows how to print out diagnostic information |
| 17 | * when one of the exceptions is thrown |
| 18 | * @code |
| 19 | * try |
| 20 | * { |
| 21 | * ... operations with ndn::Name |
| 22 | * } |
| 23 | * catch (boost::exception &e) |
| 24 | * { |
| 25 | * std::cerr << boost::diagnostic_information (e) << std::endl; |
| 26 | * } |
| 27 | * @endcode |
| 28 | */ |
| 29 | |
| 30 | #ifndef NDN_ERROR2_H |
| 31 | #define NDN_ERROR2_H |
| 32 | |
| 33 | #include "ns3/ndn-common.h" |
| 34 | #include <boost/exception/all.hpp> |
| 35 | |
| 36 | NDN_NAMESPACE_BEGIN |
| 37 | |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 38 | /** |
| 39 | * @ingroup ndn-cxx |
| 40 | * @brief Namespace holding all errors from NDN.cxx API |
| 41 | */ |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 42 | namespace error |
| 43 | { |
| 44 | |
| 45 | struct Error : public virtual boost::exception, public virtual std::exception {}; ///< @brief Some error with error reporting engine |
| 46 | struct Uri : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with URI processing |
| 47 | struct StringTransform : public virtual boost::exception, public virtual std::exception {}; |
| 48 | struct Name : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Name |
| 49 | namespace name { |
| 50 | struct Component : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with name::Component |
| 51 | } |
| 52 | struct Exclude : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Exclude |
| 53 | struct KeyLocator : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with KeyLocator |
| 54 | namespace wire { |
| 55 | struct Ccnb : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with wire::Ccnb encoding |
| 56 | } |
| 57 | struct Keychain : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with security::Keychain |
| 58 | |
| 59 | // Diagnostic information fields |
| 60 | |
| 61 | /** |
| 62 | * @brief Free-formatted text message explaining the error |
| 63 | * |
| 64 | * @code |
| 65 | * ... |
| 66 | * catch (boost::exception &e) |
| 67 | * { |
| 68 | * if (const std::string *error = boost::get_error_info<error::msg> (e)) |
| 69 | * ... |
| 70 | * } |
| 71 | * @endcode |
| 72 | * |
| 73 | * @see get_msg |
| 74 | */ |
| 75 | typedef boost::error_info<struct tag_msg, std::string> msg; |
| 76 | |
| 77 | /** |
| 78 | * @brief Helper method to get error message from the exception |
| 79 | * |
| 80 | * Method assumes that message is present, if not, an exception will be thrown |
| 81 | */ |
| 82 | inline const std::string & |
| 83 | get_msg (boost::exception &e) |
| 84 | { |
| 85 | const std::string *error = boost::get_error_info<msg> (e); |
| 86 | if (error == 0) |
| 87 | BOOST_THROW_EXCEPTION (Error ()); |
| 88 | return *error; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @brief Report of the position of the error (error-specific meaning) |
| 93 | * |
| 94 | * @code |
| 95 | * ... |
| 96 | * catch (boost::exception &e) |
| 97 | * { |
| 98 | * if (const int *error = boost::get_error_info<error::pos> (e)) |
| 99 | * ... |
| 100 | * } |
| 101 | * @endcode |
| 102 | * |
| 103 | * @see get_pos |
| 104 | */ |
| 105 | typedef boost::error_info<struct tag_pos, int> pos; |
| 106 | |
| 107 | /** |
| 108 | * @brief Helper method to get position of the error from the exception |
| 109 | * |
| 110 | * Method assumes that position is present, if not, an exception will be thrown |
| 111 | */ |
| 112 | inline int |
| 113 | get_pos (boost::exception &e) |
| 114 | { |
| 115 | const int *position = boost::get_error_info<pos> (e); |
| 116 | if (position == 0) |
| 117 | BOOST_THROW_EXCEPTION (Error ()); |
| 118 | return *position; |
| 119 | } |
| 120 | |
| 121 | } // error |
| 122 | |
| 123 | NDN_NAMESPACE_END |
| 124 | |
| 125 | #endif // NDN_ERROR2_H |