Skip to content
Snippets Groups Projects
Commit d9f75776 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Improve exception handling

Backtrace is now only produced when '-p' option is provided

This gives better user information and allows to debug more simply
since exceptions are no more rethrown

Going further would require to be able to indicate the current line of
execution during a non exception crash
parent 0c38524b
No related branches found
No related tags found
1 merge request!37Feature/language
...@@ -16,8 +16,9 @@ ...@@ -16,8 +16,9 @@
#include <language/utils/ASTDotPrinter.hpp> #include <language/utils/ASTDotPrinter.hpp>
#include <language/utils/ASTPrinter.hpp> #include <language/utils/ASTPrinter.hpp>
#include <language/utils/SymbolTable.hpp> #include <language/utils/SymbolTable.hpp>
#include <utils/Exceptions.hpp>
#include <utils/PugsAssert.hpp> #include <utils/PugsAssert.hpp>
#include <utils/PugsUtils.hpp>
#include <utils/SignalManager.hpp>
#include <pegtl/contrib/analyze.hpp> #include <pegtl/contrib/analyze.hpp>
#include <pegtl/contrib/parse_tree.hpp> #include <pegtl/contrib/parse_tree.hpp>
...@@ -41,10 +42,8 @@ parser(const std::string& filename) ...@@ -41,10 +42,8 @@ parser(const std::string& filename)
std::cout << rang::style::bold << "Parsing file " << rang::style::reset << rang::style::underline << filename std::cout << rang::style::bold << "Parsing file " << rang::style::reset << rang::style::underline << filename
<< rang::style::reset << " ...\n"; << rang::style::reset << " ...\n";
std::unique_ptr<ASTNode> root_node; auto parse_and_execute = [](auto& input) {
read_input input(filename); std::unique_ptr<ASTNode> root_node = ASTBuilder::build(input);
try {
root_node = ASTBuilder::build(input);
ASTModulesImporter{*root_node}; ASTModulesImporter{*root_node};
ASTNodeTypeCleaner<language::import_instruction>{*root_node}; ASTNodeTypeCleaner<language::import_instruction>{*root_node};
...@@ -82,18 +81,27 @@ parser(const std::string& filename) ...@@ -82,18 +81,27 @@ parser(const std::string& filename)
ExecutionPolicy exec_all; ExecutionPolicy exec_all;
root_node->execute(exec_all); root_node->execute(exec_all);
std::cout << *(root_node->m_symbol_table) << '\n'; std::cout << *(root_node->m_symbol_table) << '\n';
};
if (not SignalManager::pauseOnError()) {
read_input input(filename);
try {
parse_and_execute(input);
} }
catch (const parse_error& e) { catch (const parse_error& e) {
const auto p = e.positions.front(); const auto p = e.positions.front();
std::ostringstream os; std::cerr << rang::style::bold << p.source << ':' << p.line << ':' << p.byte_in_line << ": " << rang::style::reset
os << rang::style::bold << p.source << ':' << p.line << ':' << p.byte_in_line << ": " << rang::style::reset << rang::fgB::red << "error: " << rang::fg::reset << rang::style::bold << e.what() << rang::style::reset
<< rang::fgB::red << "error: " << rang::fg::reset << rang::style::bold << e.what() << rang::style::reset << '\n' << '\n'
<< input.line_at(p) << '\n' << input.line_at(p) << '\n'
<< std::string(p.byte_in_line, ' ') << rang::fgB::yellow << '^' << rang::fg::reset; << std::string(p.byte_in_line, ' ') << rang::fgB::yellow << '^' << rang::fg::reset << '\n';
finalize();
throw RawError(os.str()); std::exit(1);
}
} else {
read_input input(filename);
parse_and_execute(input);
} }
std::cout << "Parsed: " << filename << '\n'; std::cout << "Parsed: " << filename << '\n';
} }
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include <language/node_processor/FunctionArgumentConverter.hpp> #include <language/node_processor/FunctionArgumentConverter.hpp>
#include <language/node_processor/INodeProcessor.hpp> #include <language/node_processor/INodeProcessor.hpp>
#include <language/utils/BuiltinFunctionEmbedder.hpp> #include <language/utils/BuiltinFunctionEmbedder.hpp>
#include <utils/Exceptions.hpp>
class BuiltinFunctionExpressionProcessor final : public INodeProcessor class BuiltinFunctionExpressionProcessor final : public INodeProcessor
{ {
...@@ -63,7 +62,7 @@ class BuiltinFunctionProcessor : public INodeProcessor ...@@ -63,7 +62,7 @@ class BuiltinFunctionProcessor : public INodeProcessor
try { try {
return m_function_expression_processor->execute(context_exec_policy); return m_function_expression_processor->execute(context_exec_policy);
} }
catch (NormalError& e) { catch (std::runtime_error& e) {
throw parse_error(e.what(), {m_argument_node.begin()}); throw parse_error(e.what(), {m_argument_node.begin()});
} }
} }
......
...@@ -39,7 +39,6 @@ ...@@ -39,7 +39,6 @@
int int
main(int argc, char* argv[]) main(int argc, char* argv[])
{ {
try {
std::string filename = initialize(argc, argv); std::string filename = initialize(argc, argv);
std::regex gmsh_regex("(.*).msh"); std::regex gmsh_regex("(.*).msh");
...@@ -417,18 +416,6 @@ main(int argc, char* argv[]) ...@@ -417,18 +416,6 @@ main(int argc, char* argv[])
std::cout << "* [" << rang::fgB::cyan << std::setw(size) << std::left << method_cost.first << rang::fg::reset std::cout << "* [" << rang::fgB::cyan << std::setw(size) << std::left << method_cost.first << rang::fg::reset
<< "] Execution time: " << rang::style::bold << method_cost.second << rang::style::reset << '\n'; << "] Execution time: " << rang::style::bold << method_cost.second << rang::style::reset << '\n';
} }
}
catch (const IExitError& e) {
if (SignalManager::pauseOnError()) {
std::rethrow_exception(std::current_exception());
} else {
// Each failing process must write
std::cerr.setstate(std::ios::goodbit);
std::cerr << e.what() << '\n';
return 1;
}
}
return 0; return 0;
} }
...@@ -48,7 +48,7 @@ initialize(int& argc, char* argv[]) ...@@ -48,7 +48,7 @@ initialize(int& argc, char* argv[])
{ {
CLI::App app{"Pugs help"}; CLI::App app{"Pugs help"};
app.add_option("filename,-f,--filename", filename, "gmsh file"); app.add_option("filename,-f,--filename", filename, "pugs script file")->check(CLI::ExistingFile);
int threads = -1; int threads = -1;
app.add_option("--threads", threads, "Number of Kokkos threads") app.add_option("--threads", threads, "Number of Kokkos threads")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment