cprover
unwrap_nested_exception.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: util
4 
5 Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
10 #include "invariant.h"
11 #include "string_utils.h"
12 #include "suffix.h"
13 #include "throw_with_nested.h"
14 
15 #include <sstream>
16 #include <vector>
17 
24 std::string unwrap_exception(const std::exception &e, int level)
25 {
26  const std::string msg = e.what();
27  std::vector<std::string> lines =
28  split_string(msg, '\n', false, true);
29  std::ostringstream message_stream;
30  message_stream << std::string(level, ' ') << "exception: ";
32  message_stream, lines.begin(), lines.end(), "\n" + std::string(level, ' '));
33 
34  try
35  {
37  }
38  catch(const std::exception &e)
39  {
40  std::string nested_message = unwrap_exception(e, level + 1);
41  // Some exception messages already end in a new line (e.g. as they have
42  // dumped an irept. Most do not so add a new line on.
43  if(nested_message.back() != '\n')
44  {
45  message_stream << '\n';
46  }
47  message_stream << nested_message;
48  }
49  catch(...)
50  {
52  }
53  return message_stream.str();
54 }
std::string unwrap_exception(const std::exception &e, int level)
Given a potentially nested exception, produce a string with all of nested exceptions information.
Stream & join_strings(Stream &os, const It b, const It e, const Delimiter &delimiter)
Prints items to an stream, separated by a constant delimiter.
Definition: string_utils.h:52
void util_rethrow_if_nested(const E &e)
void split_string(const std::string &s, char delim, std::vector< std::string > &result, bool strip, bool remove_empty)
Given a string s, split into a sequence of substrings when separated by specified delimiter.
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:478