Skip to content

Commit 3207291

Browse files
svorenovathk123
svorenova
authored and
thk123
committed
Introduce nested exception printing
1 parent 7d247da commit 3207291

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/util/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ SRC = arith_tools.cpp \
9696
unicode.cpp \
9797
union_find.cpp \
9898
union_find_replace.cpp \
99+
unwrap_nested_exception.cpp \
99100
xml.cpp \
100101
xml_expr.cpp \
101102
xml_irep.cpp \

src/util/unwrap_nested_exception.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*******************************************************************\
2+
3+
Module: util
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#include "unwrap_nested_exception.h"
10+
#include "invariant.h"
11+
#include "string_utils.h"
12+
#include "suffix.h"
13+
14+
#include <sstream>
15+
#include <vector>
16+
17+
/// Given a potentially nested exception, produce a string with all of nested
18+
/// exceptions information. If a nested exception string contains new lines
19+
/// then the newlines are indented to the correct level.
20+
/// \param e: The outer exeception
21+
/// \param level: How many exceptions have already been unrolled
22+
/// \return A string with all nested exceptions printed and indented
23+
std::string unwrap_exception(const std::exception &e, int level)
24+
{
25+
const std::string msg = e.what();
26+
std::vector<std::string> lines;
27+
split_string(msg, '\n', lines, false, true);
28+
std::ostringstream message_stream;
29+
message_stream << std::string(level, ' ') << "exception: ";
30+
join_strings(
31+
message_stream, lines.begin(), lines.end(), "\n" + std::string(level, ' '));
32+
33+
try
34+
{
35+
std::rethrow_if_nested(e);
36+
}
37+
catch(const std::exception &e)
38+
{
39+
std::string nested_message = unwrap_exception(e, level + 1);
40+
// Some exception messages already end in a new line (e.g. as they have
41+
// dumped an irept. Most do not so add a new line on.
42+
if(nested_message.back() != '\n')
43+
{
44+
message_stream << '\n';
45+
}
46+
message_stream << nested_message;
47+
}
48+
catch(...)
49+
{
50+
UNREACHABLE;
51+
}
52+
return message_stream.str();
53+
}

src/util/unwrap_nested_exception.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*******************************************************************\
2+
3+
Module: util
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_UNWRAP_NESTED_EXCEPTION_H
10+
#define CPROVER_UTIL_UNWRAP_NESTED_EXCEPTION_H
11+
12+
#include <exception>
13+
#include <string>
14+
15+
std::string unwrap_exception(const std::exception &e, int level = 0);
16+
17+
#endif // CPROVER_UTIL_UNWRAP_NESTED_EXCEPTION_H

0 commit comments

Comments
 (0)