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