Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make HostFailureException compatible with the old xbt_ex mechanism
[simgrid.git] / include / simgrid / Exception.hpp
1 /* Copyright (c) 2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_EXCEPTIONS_HPP
7 #define SIMGRID_EXCEPTIONS_HPP
8
9 /** @file exception.hpp SimGrid-specific Exceptions
10  *
11  *  Defines all possible exception that could occur in a SimGrid library.
12  */
13
14 #include <exception>
15 #include <stdexcept>
16 #include <string>
17 #include <type_traits>
18 #include <vector>
19
20 #include <xbt/backtrace.h>
21 #include <xbt/backtrace.hpp>
22 #include <xbt/base.h>
23 #include <xbt/ex.h>
24 #include <xbt/log.h>
25 #include <xbt/misc.h>  // xbt_procname
26 #include <xbt/virtu.h> // xbt_getpid
27
28 namespace simgrid {
29 namespace xbt {
30
31 /** Contextual information about an execution location (file:line:func and backtrace, procname, pid)
32  *
33  *  Constitute the contextual information of where an exception was thrown
34  *
35  *  These tuples (__FILE__, __LINE__, __func__, backtrace, procname, pid)
36  *  are best created with @ref XBT_THROW_POINT.
37  *
38  *  @ingroup XBT_ex
39  */
40 class ThrowPoint {
41 public:
42   ThrowPoint() = default;
43   explicit ThrowPoint(const char* file, int line, const char* function, Backtrace bt, std::string actor_name, int pid)
44       : file_(file), line_(line), function_(function), backtrace_(bt), procname_(actor_name), pid_(pid)
45   {
46   }
47
48   const char* file_     = nullptr;
49   int line_             = 0;
50   const char* function_ = nullptr;
51   Backtrace backtrace_;
52   std::string procname_ = ""; /**< Name of the process who thrown this */
53   int pid_              = 0;  /**< PID of the process who thrown this */
54 };
55
56 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
57 #define XBT_THROW_POINT                                                                                                \
58   ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__, simgrid::xbt::backtrace(), xbt_procname(), xbt_getpid())
59 } // namespace xbt
60
61 /** Ancestor class of all SimGrid exception */
62 class Exception : public std::runtime_error {
63 public:
64   Exception(simgrid::xbt::ThrowPoint throwpoint, const char* message)
65       : std::runtime_error(message), throwpoint_(throwpoint)
66   {
67   }
68
69   /** Return the information about where the exception was thrown */
70   xbt::ThrowPoint const& throw_point() const { return throwpoint_; }
71
72 private:
73   xbt::ThrowPoint throwpoint_;
74 };
75
76 } // namespace simgrid
77
78 /** A legacy exception
79  *
80  *  It is defined by a category and a value within that category (as well as
81  *  an optional error message).
82  *
83  *  This used to be a structure for C exceptions but it has been retrofitted
84  *  as a C++ exception and some of its data has been moved in the
85  *  @ref WithContextException base class. We should deprecate it and replace it
86  *  with either C++ different exceptions or `std::system_error` which already
87  *  provides this (category + error code) logic.
88  *
89  *  @ingroup XBT_ex_c
90  */
91 class XBT_PUBLIC xbt_ex : public simgrid::Exception {
92 public:
93   /**
94    *
95    * @param throwpoint Throw point (use XBT_THROW_POINT)
96    * @param message    Exception message
97    */
98   xbt_ex(simgrid::xbt::ThrowPoint throwpoint, const char* message) : simgrid::Exception(throwpoint, message) {}
99
100   ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
101
102   /** Category (what went wrong) */
103   xbt_errcat_t category = unknown_error;
104
105   /** Why did it went wrong */
106   int value = 0;
107 };
108
109 namespace simgrid {
110
111 /** Exception raised when a timeout elapsed */
112 class TimeoutError : public xbt_ex {
113 };
114
115 /** Exception raised when an host fails */
116 class HostFailureException : public xbt_ex {
117 public:
118   HostFailureException(simgrid::xbt::ThrowPoint throwpoint, const char* message) : xbt_ex(throwpoint, message)
119   {
120     category = host_error;
121   }
122 };
123
124 /** Exception raised when a communication fails because of the network */
125 class NetworkFailureException : public xbt_ex {
126 };
127
128 /** Exception raised when something got canceled before completion */
129 class CancelException : public xbt_ex {
130 };
131 } // namespace simgrid
132
133 #endif