Skip to content

Commit

Permalink
improve config error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Tooley committed Apr 23, 2019
1 parent 753127e commit 4c91cce
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/baseconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <sstream>

#include "exceptions.hpp"
#include "infix_iterator.hpp"

const std::string ConfigurationBase::k_stem_token = "%name%";
Expand Down Expand Up @@ -64,7 +65,7 @@ void ConfigurationBase::validate_config()
errmsg << "Missing required argument(s) \"";
std::copy(missing.cbegin(), missing.cend(), infix_ostream_iterator<std::string>(errmsg, ", "));
errmsg << "\"";
throw std::runtime_error(errmsg.str());
throw BadConfigurationError(errmsg.str());
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,21 @@ class InternalError: public std::runtime_error {
}
};

class BadConfigurationError: public std::runtime_error {
public:
BadConfigurationError(const std::string& what, std::string file = "unknown", integer line = 0)
: std::runtime_error(build_errstring(what, file, line))
{
}

protected:
static std::string build_errstring(
const std::string& what, std::string file, integer line)
{
std::ostringstream errss;
errss << "Internal error at " << file << ":" << line << " \"" << what << "\"";
return errss.str();
}
};

#endif // EXCEPTIONS_HPP
20 changes: 15 additions & 5 deletions src/pfire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "setup.hpp"
#include "shirtemulation.hpp"

#include "exceptions.hpp"
#include "elastic.hpp"
#include "image.hpp"
#include "infix_iterator.hpp"
Expand All @@ -44,13 +45,20 @@ int main(int argc, char **argv)
std::string invocation_name = ConfigurationBase::get_invocation_name(argv[0]);

std::shared_ptr<ConfigurationBase> configobj(nullptr);
if (ShirtConfig::valid_invocation(invocation_name))
{
configobj = std::make_shared<ShirtConfig>(argc, argv);
try{
if (ShirtConfig::valid_invocation(invocation_name))
{
configobj = std::make_shared<ShirtConfig>(argc, argv);
}
else
{
configobj = std::make_shared<IniConfig>(argc, argv);
}
}
else
catch (const BadConfigurationError &err)
{
configobj = std::make_shared<IniConfig>(argc, argv);
PetscPrintf(PETSC_COMM_WORLD, "Failed to parse configuration: %s", err.what());
MPI_Abort(PETSC_COMM_WORLD, -1);
}

configobj->validate_config();
Expand Down Expand Up @@ -113,12 +121,14 @@ void mainflow(std::shared_ptr<ConfigurationBase> config)
std::string outfile = config->grab<std::string>("registered");
std::string h5group = config->grab<std::string>("registered_h5_path");
std::string output_path = outfile + ":" + h5group;
PetscPrintf(PETSC_COMM_WORLD, "Saving registered image to %s\n", output_path.c_str());
BaseWriter_unique wtr = BaseWriter::get_writer_for_filename(output_path, fixed->comm());
wtr->write_image(*reg.registered());

outfile = config->grab<std::string>("map");
h5group = config->grab<std::string>("map_h5_path");
output_path = outfile + ":" + h5group;
PetscPrintf(PETSC_COMM_WORLD, "Saving map to %s\n", output_path.c_str());
wtr = BaseWriter::get_writer_for_filename(output_path, fixed->comm());
wtr->write_map(*reg.m_p_map);
}

0 comments on commit 4c91cce

Please sign in to comment.