Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -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_ERROR_H |
| 31 | #define NDN_ERROR_H |
| 32 | |
| 33 | #include <boost/exception/all.hpp> |
| 34 | |
| 35 | namespace ndn |
| 36 | { |
| 37 | namespace error |
| 38 | { |
| 39 | |
| 40 | struct Error : public virtual boost::exception, public virtual std::exception {}; ///< @brief Some error with error reporting engine |
| 41 | struct Uri : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with URI processing |
| 42 | struct StringTransform : public virtual boost::exception, public virtual std::exception {}; |
| 43 | struct Name : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Name |
| 44 | namespace name { |
| 45 | struct Component : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with name::Component |
| 46 | } |
| 47 | struct Exclude : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Exclude |
| 48 | struct KeyLocator : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with KeyLocator |
| 49 | namespace wire { |
| 50 | struct Ccnb : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with wire::Ccnb encoding |
| 51 | } |
| 52 | struct Keychain : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with security::Keychain |
| 53 | |
| 54 | // Diagnostic information fields |
| 55 | |
| 56 | /** |
| 57 | * @brief Free-formatted text message explaining the error |
| 58 | * |
| 59 | * @code |
| 60 | * ... |
| 61 | * catch (boost::exception &e) |
| 62 | * { |
| 63 | * if (const std::string *error = boost::get_error_info<error::msg> (e)) |
| 64 | * ... |
| 65 | * } |
| 66 | * @endcode |
| 67 | * |
| 68 | * @see get_msg |
| 69 | */ |
| 70 | typedef boost::error_info<struct tag_msg, std::string> msg; |
| 71 | |
| 72 | /** |
| 73 | * @brief Helper method to get error message from the exception |
| 74 | * |
| 75 | * Method assumes that message is present, if not, an exception will be thrown |
| 76 | */ |
| 77 | inline const std::string & |
| 78 | get_msg (boost::exception &e) |
| 79 | { |
| 80 | const std::string *error = boost::get_error_info<msg> (e); |
| 81 | if (error == 0) |
| 82 | BOOST_THROW_EXCEPTION (Error ()); |
| 83 | return *error; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @brief Report of the position of the error (error-specific meaning) |
| 88 | * |
| 89 | * @code |
| 90 | * ... |
| 91 | * catch (boost::exception &e) |
| 92 | * { |
| 93 | * if (const int *error = boost::get_error_info<error::pos> (e)) |
| 94 | * ... |
| 95 | * } |
| 96 | * @endcode |
| 97 | * |
| 98 | * @see get_pos |
| 99 | */ |
| 100 | typedef boost::error_info<struct tag_pos, int> pos; |
| 101 | |
| 102 | /** |
| 103 | * @brief Helper method to get position of the error from the exception |
| 104 | * |
| 105 | * Method assumes that position is present, if not, an exception will be thrown |
| 106 | */ |
| 107 | inline int |
| 108 | get_pos (boost::exception &e) |
| 109 | { |
| 110 | const int *position = boost::get_error_info<pos> (e); |
| 111 | if (position == 0) |
| 112 | BOOST_THROW_EXCEPTION (Error ()); |
| 113 | return *position; |
| 114 | } |
| 115 | |
| 116 | } // error |
| 117 | } // ndn |
| 118 | |
| 119 | #endif // NDN_ERROR_H |