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 @@
#include <language/utils/ASTDotPrinter.hpp>
#include <language/utils/ASTPrinter.hpp>
#include <language/utils/SymbolTable.hpp>
#include <utils/Exceptions.hpp>
#include <utils/PugsAssert.hpp>
#include <utils/PugsUtils.hpp>
#include <utils/SignalManager.hpp>
#include <pegtl/contrib/analyze.hpp>
#include <pegtl/contrib/parse_tree.hpp>
......@@ -41,10 +42,8 @@ parser(const std::string& filename)
std::cout << rang::style::bold << "Parsing file " << rang::style::reset << rang::style::underline << filename
<< rang::style::reset << " ...\n";
std::unique_ptr<ASTNode> root_node;
read_input input(filename);
try {
root_node = ASTBuilder::build(input);
auto parse_and_execute = [](auto& input) {
std::unique_ptr<ASTNode> root_node = ASTBuilder::build(input);
ASTModulesImporter{*root_node};
ASTNodeTypeCleaner<language::import_instruction>{*root_node};
......@@ -82,18 +81,27 @@ parser(const std::string& filename)
ExecutionPolicy exec_all;
root_node->execute(exec_all);
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) {
const auto p = e.positions.front();
std::ostringstream os;
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 << '\n'
std::cerr << 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
<< '\n'
<< input.line_at(p) << '\n'
<< std::string(p.byte_in_line, ' ') << rang::fgB::yellow << '^' << rang::fg::reset;
throw RawError(os.str());
<< std::string(p.byte_in_line, ' ') << rang::fgB::yellow << '^' << rang::fg::reset << '\n';
finalize();
std::exit(1);
}
} else {
read_input input(filename);
parse_and_execute(input);
}
std::cout << "Parsed: " << filename << '\n';
}
......@@ -5,7 +5,6 @@
#include <language/node_processor/FunctionArgumentConverter.hpp>
#include <language/node_processor/INodeProcessor.hpp>
#include <language/utils/BuiltinFunctionEmbedder.hpp>
#include <utils/Exceptions.hpp>
class BuiltinFunctionExpressionProcessor final : public INodeProcessor
{
......@@ -63,7 +62,7 @@ class BuiltinFunctionProcessor : public INodeProcessor
try {
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()});
}
}
......
......@@ -39,7 +39,6 @@
int
main(int argc, char* argv[])
{
try {
std::string filename = initialize(argc, argv);
std::regex gmsh_regex("(.*).msh");
......@@ -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
<< "] 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;
}
......@@ -48,7 +48,7 @@ initialize(int& argc, char* argv[])
{
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;
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