blob: d6379c6a5219779230bebd8d3c9acd51d825aa1b [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -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_ERROR_H
31#define NDN_ERROR_H
32
33#include <boost/exception/all.hpp>
34
35namespace ndn
36{
37namespace error
38{
39
40struct Error : public virtual boost::exception, public virtual std::exception {}; ///< @brief Some error with error reporting engine
41struct Uri : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with URI processing
42struct StringTransform : public virtual boost::exception, public virtual std::exception {};
43struct Name : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Name
44namespace name {
45struct Component : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with name::Component
46}
47struct Exclude : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with Exclude
48struct KeyLocator : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with KeyLocator
49namespace wire {
50struct Ccnb : public virtual boost::exception, public virtual std::exception {}; ///< @brief An error with wire::Ccnb encoding
51}
52struct 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 */
70typedef 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 */
77inline const std::string &
78get_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 */
100typedef 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 */
107inline int
108get_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