Added error handling
This commit is contained in:
@@ -27,7 +27,7 @@ namespace fsh {
|
||||
}
|
||||
|
||||
Command& get(const std::string n) {
|
||||
if (cmds.find(n) == cmds.end()) { throw std::runtime_error("Command not found " + n); }
|
||||
if (cmds.find(n) == cmds.end()) { throw util::CmdNotFoundError(n); }
|
||||
return *(cmds[n]);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "ast/ast.hpp"
|
||||
#include "cmd/args/arg.hpp"
|
||||
#include "util/error.hpp"
|
||||
|
||||
namespace fsh {
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace fsh {
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
|
||||
if(std::remove(args.get<std::string>(0).value().c_str())) {
|
||||
throw std::runtime_error("Could not delete file");
|
||||
throw util::FileDeleteError();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace fsh {
|
||||
virtual void run(std::istream& in, std::ostream& out, ArgManager& args) override {
|
||||
auto name = *(args.get<std::string>(0));
|
||||
if (std::ifstream(name, std::ios::in).good() || !std::ofstream(name, std::ios::out).is_open()) {
|
||||
throw std::runtime_error("File exists");
|
||||
throw util::FileExistsError();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace fsh {
|
||||
protected:
|
||||
|
||||
std::string replace_all(std::string str,const std::string &what,const std::string &with) {
|
||||
if(what == "") throw std::runtime_error("\"\" cannot be used as what");
|
||||
if(what == "") throw util::CmdError("What cannot be empty string");
|
||||
|
||||
unsigned long long pos = str.find(what);
|
||||
|
||||
|
||||
@@ -21,11 +21,11 @@ namespace fsh {
|
||||
std::string filename = args.get<std::string>(0).value();
|
||||
|
||||
if(!std::ifstream(filename, std::ios_base::in)) {
|
||||
throw std::runtime_error("File does not exist");
|
||||
throw util::FileExistsError();
|
||||
}
|
||||
|
||||
if(!std::ofstream(filename, std::ios_base::out)) {
|
||||
throw std::runtime_error("Failed to truncate");
|
||||
throw util::CmdError("Failed to truncate");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user