SObjectizer 5.8
Loading...
Searching...
No Matches
process_unhandled_exception.cpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \since
7 * v.5.4.0
8 *
9 * \file
10 * \brief Helpers for handling unhandled exceptions from agent's event handlers.
11 */
12
13#include <so_5/impl/process_unhandled_exception.hpp>
14
15#include <so_5/environment.hpp>
16
17#include <so_5/details/abort_on_fatal_error.hpp>
18#include <so_5/details/suppress_exceptions.hpp>
19
20namespace so_5 {
21
22namespace impl {
23
24namespace {
25
26/*!
27 * \since
28 * v.5.4.0
29 *
30 * \brief Switch agent to special state and deregister its cooperation.
31 *
32 * Calls abort() if an exception is raised during work.
33 */
34void
36 //! Agent who is the producer of the exception.
37 so_5::agent_t & a_exception_producer ) noexcept
38 {
39 // Declared here because it'll be used in catch section.
40 coop_handle_t coop;
41 try
42 {
43 // agent_t::so_coop() isn't noexcept method, so it has to be
44 // called inside try section.
45 coop = a_exception_producer.so_coop();
47 a_exception_producer.so_environment().deregister_coop(
48 coop,
50 }
51 catch( const std::exception & x )
52 {
53 // The work can't continued, but information about the problem has to
54 // be logged.
56 SO_5_LOG_ERROR( a_exception_producer.so_environment(), log_stream )
57 {
58 log_stream << "An exception '" << x.what()
59 << "' during deregistring cooperation "
60 << coop << " on unhandled exception"
61 "processing. Application will be aborted.";
62 }
63 } );
64 }
65 }
66
67/*!
68 * \since
69 * v.5.4.0
70 *
71 * \brief Switch agent to special state and initiate stopping
72 * of SObjectizer Environment.
73 *
74 * Calls abort() if an exception is raised during work.
75 */
76void
78 //! Agent who is the producer of the exception.
79 agent_t & a_exception_producer ) noexcept
80 {
81 try
82 {
83 // NOTE: so_switch_to_awaiting_deregistration_state() isn't noexcept,
84 // so call it inside try section to catch and log a possible
85 // exception.
87 a_exception_producer.so_environment().stop();
88 }
89 catch( const std::exception & x )
90 {
91 // The work can't continued, but information about the problem has to
92 // be logged.
94 SO_5_LOG_ERROR( a_exception_producer.so_environment(), log_stream )
95 {
96 log_stream << "An exception '" << x.what()
97 << "' during shutting down SObjectizer on unhandled "
98 "exception processing. Application will be aborted.";
99 }
100 } );
101 }
102 }
103
104/*!
105 * \since
106 * v.5.4.0
107 *
108 * \brief Log unhandled exception from cooperation.
109 *
110 * \note
111 * This function is noexcept since v.5.6.0.
112 */
113void
115 //! Raised and caught exception.
116 const std::exception & ex_to_log,
117 //! Agent who is the producer of the exception.
118 agent_t & a_exception_producer ) noexcept
119 {
121 ex_to_log,
122 a_exception_producer.so_coop() );
123 }
124
125} /* namespace anonymous */
126
127//
128// process_unhandled_exception
129//
130void
132 current_thread_id_t working_thread_id,
133 const std::exception & ex,
134 agent_t & a_exception_producer ) noexcept
135 {
136 log_unhandled_exception( ex, a_exception_producer );
137
138 auto reaction = a_exception_producer.so_exception_reaction();
139 if( working_thread_id == null_current_thread_id() &&
140 ignore_exception != reaction &&
141 abort_on_exception != reaction )
142 {
144 SO_5_LOG_ERROR( a_exception_producer.so_environment(), log_stream )
145 {
146 log_stream << "Illegal exception_reaction code "
147 "for the multithreadded agent: "
148 << static_cast< int >(reaction) << ". "
149 "The only allowed exception_reaction for "
150 "such kind of agents are ignore_exception or "
151 "abort_on_exception. "
152 "Application will be aborted. "
153 "Unhandled exception '" << ex.what()
154 << "' from cooperation "
155 << a_exception_producer.so_coop();
156 }
157 } );
158 }
159
160 if( abort_on_exception == reaction )
161 {
163 SO_5_LOG_ERROR( a_exception_producer.so_environment(), log_stream )
164 {
165 log_stream << "Application will be aborted due to unhandled "
166 "exception '" << ex.what() << "' from cooperation "
167 << a_exception_producer.so_coop();
168 }
169 } );
170 }
171 else if( shutdown_sobjectizer_on_exception == reaction )
172 {
173 // Since v.5.6.2 all logging-related exceptions are suppressed here.
175 SO_5_LOG_ERROR( a_exception_producer.so_environment(), log_stream )
176 {
177 log_stream << "SObjectizer will be shutted down due to "
178 "unhandled exception '" << ex.what()
179 << "' from cooperation "
180 << a_exception_producer.so_coop();
181 }
182 } );
183
185 a_exception_producer );
186 }
187 else if( deregister_coop_on_exception == reaction )
188 {
189 // Since v.5.6.2 all logging-related exceptions are suppressed here.
191 SO_5_LOG_ERROR( a_exception_producer.so_environment(), log_stream )
192 {
193 log_stream << "Cooperation "
194 << a_exception_producer.so_coop()
195 << " will be deregistered due to unhandled exception '"
196 << ex.what() << "'";
197 }
198 } );
199
201 a_exception_producer );
202 }
203 else if( ignore_exception == reaction )
204 {
205 // Since v.5.6.2 all logging-related exceptions are suppressed here.
207 SO_5_LOG_ERROR( a_exception_producer.so_environment(), log_stream )
208 {
209 log_stream << "Ignore unhandled exception '"
210 << ex.what() << "' from cooperation "
211 << a_exception_producer.so_coop();
212 }
213 } );
214 }
215 else
216 {
218 SO_5_LOG_ERROR( a_exception_producer.so_environment(), log_stream )
219 {
220 log_stream << "Unknown exception_reaction code: "
221 << static_cast< int >(reaction)
222 << ". Application will be aborted. Unhandled exception '"
223 << ex.what() << "' from cooperation "
224 << a_exception_producer.so_coop();
225 }
226 } );
227 }
228 }
229
230void
232 current_thread_id_t working_thread_id,
233 agent_t & a_exception_producer ) noexcept
234 {
235 // Just call process_unhandled_exception with dummy exception object.
236 exception_t dummy{
237 "an exception of unknown type is caught",
239 };
240
242 working_thread_id,
243 dummy,
244 a_exception_producer );
245 }
246
247} /* namespace impl */
248
249} /* namespace so_5 */
A base class for agents.
Definition agent.hpp:673
void so_switch_to_awaiting_deregistration_state()
Switching agent to special state in case of unhandled exception.
Definition agent.cpp:756
coop_handle_t so_coop() const
Get a handle of agent's coop.
Definition agent.cpp:860
virtual exception_reaction_t so_exception_reaction() const noexcept
A reaction from SObjectizer to an exception from agent's event.
Definition agent.cpp:746
environment_t & so_environment() const noexcept
Access to the SObjectizer Environment which this agent is belong.
Definition agent.cpp:853
Type of smart handle for a cooperation.
void call_exception_logger(const std::exception &event_exception, const coop_handle_t &coop) noexcept
Call event exception logger for logging an exception.
void stop() noexcept
Send a shutdown signal to the Run-Time.
void deregister_coop(coop_handle_t coop, int reason) noexcept
Deregister the cooperation.
The base class for all SObjectizer exceptions.
Definition exception.hpp:34
exception_t(const std::string &error_descr, int error_code)
Definition exception.hpp:36
#define SO_5_LOG_ERROR(logger, var_name)
A special macro for helping error logging.
Enumeration of cooperation deregistration reasons.
Definition coop.hpp:39
const int unhandled_exception
Deregistration because of unhandled exception.
Definition coop.hpp:55
Some reusable and low-level classes/functions which can be used in public header files.
void abort_on_fatal_error(L logging_lambda) noexcept
void suppress_exceptions(Lambda &&lambda) noexcept
Helper function for execution a block of code with suppression of any exceptions raised inside that b...
void switch_agent_to_special_state_and_deregister_coop(so_5::agent_t &a_exception_producer) noexcept
Switch agent to special state and deregister its cooperation.
void log_unhandled_exception(const std::exception &ex_to_log, agent_t &a_exception_producer) noexcept
Log unhandled exception from cooperation.
void switch_agent_to_special_state_and_shutdown_sobjectizer(agent_t &a_exception_producer) noexcept
Switch agent to special state and initiate stopping of SObjectizer Environment.
Details of SObjectizer run-time implementations.
Definition agent.cpp:780
void process_unhandled_unknown_exception(current_thread_id_t working_thread_id, agent_t &a_exception_producer) noexcept
Processor of unhandled exception of unknown type from agent's event handler.
void process_unhandled_exception(current_thread_id_t working_thread_id, const std::exception &ex, agent_t &a_exception_producer) noexcept
Processor of unhandled exception from agent's event handler.
Private part of message limit implementation.
Definition agent.cpp:33
@ abort_on_exception
Execution of application must be aborted immediatelly.
Definition agent.hpp:67
@ ignore_exception
Exception should be ignored and agent should continue its work.
Definition agent.hpp:75
@ deregister_coop_on_exception
Definition agent.hpp:73
@ shutdown_sobjectizer_on_exception
Definition agent.hpp:70
current_thread_id_t null_current_thread_id()
Get NULL thread id.
const int rc_unknown_exception_type
An exception of unknown type is caught.
Definition ret_code.hpp:556