34 #include <Ice/LocalException.h>
35 #include <IceUtil/Exception.h>
46 LocalException::LocalException() : reason(
""), backtrace(
""), output_buffer(
"")
48 backtrace = std::runtime_error(generateBacktrace());
52 LocalException::LocalException(
const LocalException& e) noexcept :
53 reason(e.reason), backtrace(e.backtrace), output_buffer(
"")
58 LocalException::setReason(
const std::string& r)
60 this->reason = std::runtime_error(r);
64 LocalException::getReason()
const
70 LocalException::what() const noexcept
73 output_buffer = std::runtime_error(generateOutputString());
75 return output_buffer.what();
79 LocalException::generateBacktrace()
83 const int nMaxFrames =
sizeof(callstack) /
sizeof(callstack[0]);
86 int nFrames = ::backtrace(callstack, nMaxFrames);
87 char** symbols = backtrace_symbols(callstack, nFrames);
89 std::ostringstream trace_buf;
91 for (
int i = skip; i < nFrames; i++)
95 if (dladdr(callstack[i], &info) && info.dli_sname)
97 char* demangled =
nullptr;
100 if (info.dli_sname[0] ==
'_')
102 demangled = abi::__cxa_demangle(info.dli_sname,
nullptr,
nullptr, &
status);
107 "%-3d %*p %s + %zd\n",
109 int(2 +
sizeof(
void*) * 2),
112 : info.dli_sname ==
nullptr ? symbols[i]
114 (
char*)callstack[i] - (
char*)info.dli_saddr);
123 int(2 +
sizeof(
void*) * 2),
133 if (nFrames == nMaxFrames)
135 trace_buf <<
"[truncated]\n";
138 return trace_buf.str();
142 LocalException::generateOutputString()
const
145 std::stringstream what_buf;
147 what_buf << std::endl;
148 what_buf <<
"Reason: " << ((strlen(reason.what()) == 0) ?
"undefined" : reason.what())
150 what_buf <<
"Backtrace: " << std::endl;
151 what_buf << backtrace.what() << std::endl;
153 return what_buf.str();
167 std::stringstream result;
173 catch (
const UserException& userException)
176 result <<
"Caught userException: " << userException.what() <<
"\n"
177 <<
"\tReason: " << userException.reason <<
"\n"
178 <<
"\tStacktrace: \n"
179 << userException.ice_stackTrace();
183 catch (
const LocalException& exception)
186 result <<
"Caught " << exception.name() <<
":\n" << exception.what();
189 catch (
const Ice::NoValueFactoryException& e)
191 std::stringstream availableFactories;
196 ObjectFactoryMap::iterator it = objFacMap.begin();
198 for (; it != objFacMap.end(); it++)
200 availableFactories <<
"\t" << it->first;
201 availableFactories <<
"\n";
205 auto typePart = e.type.substr(e.type.find_last_of(
':') + 1);
208 <<
"Caught NoObjectFactoryException:\n " << e.what() <<
"\n\nPossible reasons:\n"
209 <<
"\t- You did not link the library where the object factory is declared\n"
210 <<
"\t- You did not include the header of the object factory\n\n"
211 <<
"\tThese object factories are usually located in header files called "
212 "XYZObjectFactories.h. This header file needs to be included into your library."
213 <<
"Additionally, also the library needs to be included. To find these file you "
214 "can try the following command (linux):\n"
215 <<
"\tgrep -r \"" << typePart <<
"\" ~/armarx/ --include \"*Factor*\"\n"
216 <<
"\t Include that header file and find out to which libray this header belongs "
217 "(usually the CMakeLists.txt next to it) and link this lib to your library."
218 <<
"\n\nAvailable factories: \n"
219 << availableFactories.str()
223 catch (
const Ice::UnexpectedObjectException& e)
225 std::stringstream availableFactories;
230 ObjectFactoryMap::iterator it = objFacMap.begin();
232 for (; it != objFacMap.end(); it++)
234 availableFactories <<
"\t" << it->first;
235 availableFactories <<
"\n";
239 auto typePart = e.expectedType.substr(e.expectedType.find_last_of(
':') + 1);
242 <<
"Caught UnexpectedObjectException:\n " << e.what() <<
"\n\nPossible reasons:\n"
243 <<
"\t- You did not link the library where the object factory is declared\n"
244 <<
"\t- You did not include the header of the object factory\n\n"
245 <<
"\tThese object factories are usually located in header files called "
246 "XYZObjectFactories.h. This header file needs to be included into your library."
247 <<
"Additionally, also the library needs to be included. To find these file you "
248 "can try the following command (linux):\n"
249 <<
"\tgrep -r \"" << typePart <<
"\" ~/armarx/ --include \"*Factor*\"\n"
250 <<
"\t Include that header file and find out to which libray this header belongs "
251 "(usually the CMakeLists.txt next to it) and link this lib to your library."
252 <<
"\n\nAvailable factories: \n"
253 << availableFactories.str()
258 catch (
const Ice::OperationNotExistException& e)
260 result <<
"\033[1mCaught OperationNotExistException:\n " << e.what()
261 <<
"\n\nPossible reasons:\n"
262 <<
"\t- You are using a topic instead of offering a topic (usingTopic(name): "
263 "receive data, offeringTopic(name): provide data) \n"
267 catch (
const IceUtil::Exception& exception)
270 result <<
"Caught IceUtil::Exception:\n " << exception.what()
271 <<
"\nBacktrace: " << exception.ice_stackTrace();
273 catch (
const std::exception& exception)
276 result <<
"Caught std::exception:\n" << exception.what();
280 result <<
"Caught unknown exception!";
282 result <<
"\nTrace created by calling ARMARX_TRACE:\n";