35 #include <IceUtil/Exception.h>
37 #include <Ice/LocalException.h>
44 LocalException::LocalException() :
49 backtrace = std::runtime_error(generateBacktrace());
53 LocalException::LocalException(
const LocalException& e) noexcept:
54 reason(e.reason), backtrace(e.backtrace), output_buffer(
"")
58 void LocalException::setReason(
const std::string& r)
60 this->reason = std::runtime_error(r);
63 std::string LocalException::getReason()
const
68 const char* LocalException::what() const noexcept
71 output_buffer = std::runtime_error(generateOutputString());
73 return output_buffer.what();
76 std::string LocalException::generateBacktrace()
80 const int nMaxFrames =
sizeof(callstack) /
sizeof(callstack[0]);
83 int nFrames = ::backtrace(callstack, nMaxFrames);
84 char** symbols = backtrace_symbols(callstack, nFrames);
86 std::ostringstream trace_buf;
88 for (
int i = skip; i < nFrames; i++)
92 if (dladdr(callstack[i], &info) && info.dli_sname)
94 char* demangled =
nullptr;
97 if (info.dli_sname[0] ==
'_')
99 demangled = abi::__cxa_demangle(info.dli_sname,
nullptr,
nullptr, &
status);
102 snprintf(buf,
sizeof(buf),
"%-3d %*p %s + %zd\n",
103 i - skip + 1,
int(2 +
sizeof(
void*) * 2), callstack[i],
105 info.dli_sname ==
nullptr ? symbols[i] : info.dli_sname,
106 (
char*)callstack[i] - (
char*)info.dli_saddr);
111 snprintf(buf,
sizeof(buf),
"%-3d %*p %s\n",
112 i - skip + 1,
int(2 +
sizeof(
void*) * 2), callstack[i], symbols[i]);
120 if (nFrames == nMaxFrames)
122 trace_buf <<
"[truncated]\n";
125 return trace_buf.str();
128 std::string LocalException::generateOutputString()
const
131 std::stringstream what_buf;
133 what_buf << std::endl;
134 what_buf <<
"Reason: " << ((strlen(reason.what()) == 0) ?
"undefined" : reason.what()) << std::endl;
135 what_buf <<
"Backtrace: " << std::endl;
136 what_buf << backtrace.what() << std::endl;
138 return what_buf.str();
149 std:: stringstream result;
155 catch (
const UserException& userException)
159 <<
"Caught userException: " << userException.what() <<
"\n"
160 <<
"\tReason: " << userException.reason <<
"\n"
161 <<
"\tStacktrace: \n" << userException.ice_stackTrace();
167 catch (
const LocalException& exception)
171 <<
"Caught " << exception.name() <<
":\n" << exception.what()
175 catch (
const Ice::NoValueFactoryException& e)
177 std::stringstream availableFactories;
182 ObjectFactoryMap::iterator it = objFacMap.begin();
184 for (; it != objFacMap.end(); it++)
186 availableFactories <<
"\t" << it->first;
187 availableFactories <<
"\n";
191 auto typePart = e.type.substr(e.type.find_last_of(
':') + 1);
194 <<
"Caught NoObjectFactoryException:\n " << e.what()
195 <<
"\n\nPossible reasons:\n"
196 <<
"\t- You did not link the library where the object factory is declared\n"
197 <<
"\t- You did not include the header of the object factory\n\n"
198 <<
"\tThese object factories are usually located in header files called XYZObjectFactories.h. This header file needs to be included into your library."
199 <<
"Additionally, also the library needs to be included. To find these file you can try the following command (linux):\n"
200 <<
"\tgrep -r \"" << typePart <<
"\" ~/armarx/ --include \"*Factor*\"\n"
201 <<
"\t Include that header file and find out to which libray this header belongs (usually the CMakeLists.txt next to it) and link this lib to your library."
202 <<
"\n\nAvailable factories: \n" << availableFactories.str()
206 catch (
const Ice::UnexpectedObjectException& e)
208 std::stringstream availableFactories;
213 ObjectFactoryMap::iterator it = objFacMap.begin();
215 for (; it != objFacMap.end(); it++)
217 availableFactories <<
"\t" << it->first;
218 availableFactories <<
"\n";
222 auto typePart = e.expectedType.substr(e.expectedType.find_last_of(
':') + 1);
225 <<
"Caught UnexpectedObjectException:\n " << e.what()
226 <<
"\n\nPossible reasons:\n"
227 <<
"\t- You did not link the library where the object factory is declared\n"
228 <<
"\t- You did not include the header of the object factory\n\n"
229 <<
"\tThese object factories are usually located in header files called XYZObjectFactories.h. This header file needs to be included into your library."
230 <<
"Additionally, also the library needs to be included. To find these file you can try the following command (linux):\n"
231 <<
"\tgrep -r \"" << typePart <<
"\" ~/armarx/ --include \"*Factor*\"\n"
232 <<
"\t Include that header file and find out to which libray this header belongs (usually the CMakeLists.txt next to it) and link this lib to your library."
233 <<
"\n\nAvailable factories: \n" << availableFactories.str()
238 catch (
const Ice::OperationNotExistException& e)
241 <<
"\033[1mCaught OperationNotExistException:\n " << e.what()
242 <<
"\n\nPossible reasons:\n"
243 <<
"\t- You are using a topic instead of offering a topic (usingTopic(name): receive data, offeringTopic(name): provide data) \n"
247 catch (
const IceUtil::Exception& exception)
251 <<
"Caught IceUtil::Exception:\n " << exception.what()
252 <<
"\nBacktrace: " << exception.ice_stackTrace()
255 catch (
const std::exception& exception)
259 <<
"Caught std::exception:\n" << exception.what()
265 <<
"Caught unknown exception!"
268 result <<
"\nTrace created by calling ARMARX_TRACE:\n";