blob: db6b751f76a6a71b534e2339ff9425ea836a8dc4 [file] [log] [blame]
Alexander Afanasyev92136012013-07-16 20:36:30 -07001/* -*- 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
36NDN_NAMESPACE_BEGIN
37
Alexander Afanasyev79206512013-07-27 16:49:12 -070038/**
39 * @ingroup ndn-cxx
40 * @brief Namespace holding all errors from NDN.cxx API
41 */
Alexander Afanasyev92136012013-07-16 20:36:30 -070042namespace error
43{
44
45struct Error : public virtual boost::exception, public virtual std::exception {}; ///< @brief Some error with error reporting engine
46struct Uri : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with URI processing
47struct StringTransform : public virtual boost::exception, public virtual std::exception {};
48struct Name : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Name
49namespace name {
50struct Component : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with name::Component
51}
52struct Exclude : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Exclude
53struct KeyLocator : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with KeyLocator
54namespace wire {
55struct Ccnb : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with wire::Ccnb encoding
56}
57struct 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 */
75typedef 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 */
82inline const std::string &
83get_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 */
105typedef 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 */
112inline int
113get_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
123NDN_NAMESPACE_END
124
125#endif // NDN_ERROR2_H