Skip to content

Commit

Permalink
Merge pull request #4 from ShujianQian/error-handling
Browse files Browse the repository at this point in the history
Add Error Handling and Help Message to ie2smp and bouded2smp
  • Loading branch information
cheshmi authored Sep 28, 2020
2 parents 708bbe3 + eb060b8 commit 9b3e7b1
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 130 deletions.
60 changes: 57 additions & 3 deletions bounded2smp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@

#include "utils.h"
#include "mp_format_converter.h"
#include "exceptions.h"


using namespace format;

int main(int argc, const char *argv[]){

std::map<std::string,std::string> qp_args;
parse_args_bounded(argc, argv, qp_args);

try {
parse_args_bounded(argc, argv, qp_args);
} catch (const missing_arg_error& e) {
std::cerr << "Error: Missing argument: " << e.arg() << std::endl;
exit(1);
} catch (...) {
std::cerr << "An unknown error has occurred while parsing" << std::endl;
exit(1);
}

std::string p_name, q_name, l_name, a_name, u_name;
std::string output = "noname.yml";
Expand All @@ -29,13 +39,57 @@ int main(int argc, const char *argv[]){
if(qp_args.find("output") != qp_args.end())
output = qp_args["output"];

auto *bf = load_bounded(p_name, q_name, l_name, a_name, u_name);
BoundedForm *bf = NULLPNTR;
try {
bf = load_bounded(p_name, q_name, l_name, a_name, u_name);
} catch (const read_file_error& e) {
std::cerr << "Error: Cannot read file: " << e.filename() << std::endl;
delete bf;
exit(1);
} catch (const mtx_format_error& e) {
std::cerr << "Error:\tMatrix format mismatch in " << e.filename() << std::endl
<< "\tExpecting " << e.expected_format()
<< " but got " << e.got_format()
<< std::endl;
delete bf;
exit(1);
} catch (const mtx_arith_error& e) {
std::cerr << "Error:\tMatrix arithmetic mismatch in "
<< e.filename() << std::endl
<< "\tExpecting " << e.expected_arith()
<< " but got " << e.got_arith()
<< std::endl;
delete bf;
exit(1);
} catch (const mtx_header_error& e) {
std::cerr << "Error:\tInvalid header in " << e.filename() << std::endl
<< "\t" << e.what() << std::endl;
delete bf;
exit(1);
} catch (...) {
std::cerr << "An unknown error has occurred while loading" << std::endl;
delete bf;
exit(1);
}

Description test;
std::string dtes=test.get_desc();
auto *qfc = new QPFormatConverter(bf);
//qfc->smp_->write("tmp2.yml");

qfc->bounded_to_smp();
try {
qfc->smp_->write(output);
} catch (const write_file_error& e) {
std::cerr << "Error: Unable to write to file " << e.filename();
delete bf;
delete qfc;
exit(1);
} catch (...) {
std::cerr << "An unknown error has occurred while writing" << std::endl;
delete bf;
delete qfc;
exit(1);
}

delete bf;
delete qfc;
Expand Down
141 changes: 141 additions & 0 deletions exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* exceptions.h
*
* Created on: Sep. 27, 2020
* Author: shujianqian
*/

#ifndef EXCEPTIONS_H_
#define EXCEPTIONS_H_


#include <exception>
#include <string>

namespace format {

class missing_arg_error : public std::runtime_error
{
public:
missing_arg_error (std::string arg, std::string msg="Argument missing")
: std::runtime_error(msg)
{
arg_ = arg;
}

std::string arg() const { return arg_; }

private:
std::string arg_;
};

class open_file_error : public std::runtime_error
{
public:
open_file_error (std::string filename, std::string msg="Failed to open file")
: std::runtime_error(msg)
{
filename_ = filename;
}

std::string filename() const { return filename_; }

private:
std::string filename_;
};

class read_file_error : public open_file_error
{
public:
read_file_error (std::string filename, std::string msg="Failed to read file")
: open_file_error(filename, msg)
{}
};

class write_file_error : public open_file_error
{
public:
write_file_error (
std::string filename,
std::string msg="Failed to write to file"
)
: open_file_error(filename, msg)
{}
};

class mtx_error : public std::runtime_error
{
public:
mtx_error (std::string filename, std::string msg="Error loading matrix")
: std::runtime_error(msg)
{
filename_ = filename;
}

std::string filename() const { return filename_; }

private:
std::string filename_;
};

class mtx_header_error : public mtx_error
{
public:
mtx_header_error (
std::string filename="Unknown",
std::string msg="Invalid matrix header"
)
: mtx_error(filename, msg)
{}
};

class mtx_format_error : public mtx_error
{
public:
mtx_format_error
(
std::string expected_format,
std::string got_format,
std::string filename = "Unknown",
std::string msg = "Matrix format mismatch"
)
: mtx_error(filename, msg)
{
expected_format_ = expected_format;
got_format_ = got_format;
}

std::string expected_format() const { return expected_format_; }
std::string got_format() const { return got_format_; }

private:
std::string expected_format_;
std::string got_format_;
};

class mtx_arith_error : public mtx_error
{
public:
mtx_arith_error
(
std::string expected_arith,
std::string got_arith,
std::string filename="Unknown",
std::string msg="Matrix arithmetic mismatch"
)
: mtx_error(filename, msg)
{
expected_arith_ = expected_arith;
got_arith_ = got_arith;
}
std::string expected_arith() const { return expected_arith_; }
std::string got_arith() const { return got_arith_; }

private:
std::string expected_arith_;
std::string got_arith_;
};

} // namespace format

#endif /* EXCEPTIONS_H_ */
60 changes: 56 additions & 4 deletions ie2smp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@

#include "utils.h"
#include "mp_format_converter.h"
#include "exceptions.h"


using namespace format;

int main(int argc, const char *argv[]){

std::map<std::string,std::string> qp_args;
parse_args_ie(argc, argv, qp_args);
try {
parse_args_ie(argc, argv, qp_args);
} catch (const missing_arg_error& e) {
std::cerr << "Error: Missing argument: " << e.arg() << std::endl;
exit(1);
} catch (...) {
std::cerr << "An unknown error has occurred while parsing" << std::endl;
exit(1);
}

std::string p_name, q_name, a_name, b_name, c_name, d_name;
std::string output = "noname.yml";
Expand All @@ -32,14 +41,57 @@ int main(int argc, const char *argv[]){
if(qp_args.find("output") != qp_args.end())
output = qp_args["output"];

auto *ie = load_ie(p_name, q_name, a_name,b_name, c_name, d_name);
IEForm *ie = NULLPNTR;
try {
ie = load_ie(p_name, q_name, a_name,b_name, c_name, d_name);
} catch (const read_file_error& e) {
std::cerr << "Error: Cannot read file: " << e.filename() << std::endl;
delete ie;
exit(1);
} catch (const mtx_format_error& e) {
std::cerr << "Error:\tMatrix format mismatch in " << e.filename() << std::endl
<< "\tExpecting " << e.expected_format()
<< " but got " << e.got_format()
<< std::endl;
delete ie;
exit(1);
} catch (const mtx_arith_error& e) {
std::cerr << "Error:\tMatrix arithmetic mismatch in "
<< e.filename() << std::endl
<< "\tExpecting " << e.expected_arith()
<< " but got " << e.got_arith()
<< std::endl;
delete ie;
exit(1);
} catch (const mtx_header_error& e) {
std::cerr << "Error:\tInvalid header in " << e.filename() << std::endl
<< "\t" << e.what() << std::endl;
delete ie;
exit(1);
} catch (...) {
std::cerr << "An unknown error has occurred while loading" << std::endl;
delete ie;
exit(1);
}

Description test;
std::string dtes=test.get_desc();
auto *qfc = new QPFormatConverter(ie);
//qfc->smp_->write("tmp2.yml");
qfc->ie_to_smp();
qfc->smp_->write(output);

try {
qfc->smp_->write(output);
} catch (const write_file_error& e) {
std::cerr << "Error: Unable to write to file " << e.filename();
delete ie;
delete qfc;
exit(1);
} catch (...) {
std::cerr << "An unknown error has occurred while writing" << std::endl;
delete ie;
delete qfc;
exit(1);
}
delete ie;
delete qfc;

Expand Down
Loading

0 comments on commit 9b3e7b1

Please sign in to comment.