Hazelnut now quits the application and prints to stderr when exceptions occur

This commit is contained in:
Leonetienne 2021-06-02 19:51:06 +02:00
parent 53300dd1fd
commit d77480ef27
3 changed files with 80 additions and 29 deletions

View File

@ -6,6 +6,8 @@
#include "ListValue.h"
#include "HazelnuppException.h"
#include "StringTools.h"
#include <iostream>
#include <cstdlib>
Hazelnupp::Hazelnupp()
{
@ -30,6 +32,8 @@ Hazelnupp::~Hazelnupp()
void Hazelnupp::Parse(const int argc, const char* const* argv)
{
try
{
// Populate raw arguments
PopulateRawArgs(argc, argv);
@ -55,6 +59,17 @@ void Hazelnupp::Parse(const int argc, const char* const* argv)
// Apply constraints such as default values, and required parameters.
// Types have already been enforced.
ApplyConstraints();
}
catch (HazelnuppConstraintTypeMissmatch hctm)
{
std::cerr << "Fatal error: Command-line parameter value-type mismatch at \"" << hctm.What() << "\"!";
quick_exit(-1009);
}
catch (HazelnuppConstraintMissingValue hctm)
{
std::cerr << "Fatal error: Missing required command-line parameter \"" << hctm.What() << "\"!";
quick_exit(-1010);
}
return;
}
@ -149,7 +164,7 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
if ((constrainType) &&
(constraint->wantedType != DATA_TYPE::LIST))
{
throw HazelnutConstraintMissmatch();
throw HazelnuppConstraintTypeMissmatch(values[0] + " " + values[1]);
}
ListValue* newList = new ListValue();
@ -185,7 +200,7 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
}
// Else it not possible to convert to a numeric
else
throw HazelnutConstraintMissmatch();
throw HazelnuppConstraintTypeMissmatch(val);
}
return new StringValue(val);
@ -267,7 +282,7 @@ void Hazelnupp::ApplyConstraints()
// Is it important to have the missing parameter?
if (pc.second.required)
// Throw an error message then
throw HazelnutConstraintMissmatch();
throw HazelnuppConstraintMissingValue(pc.second.key);
}
}

View File

@ -1,5 +1,41 @@
#pragma once
#include <stdexcept>
class HazelnutException : public std::exception {};
class HazelnutConstraintMissmatch : public HazelnutException {};
/** Generic hazelnupp exception
*/
class HazelnuppException : public std::exception
{
public:
HazelnuppException() {};
HazelnuppException(const std::string& msg) : message{ msg } {};
//! Will return an error message
const std::string& What() const
{
return message;
}
protected:
std::string message;
};
class HazelnuppConstraintException : public HazelnuppException
{
public:
HazelnuppConstraintException() : HazelnuppException() {};
HazelnuppConstraintException(const std::string& msg) : HazelnuppException(msg) {};
};
class HazelnuppConstraintTypeMissmatch : public HazelnuppConstraintException
{
public:
HazelnuppConstraintTypeMissmatch() : HazelnuppConstraintException() {};
HazelnuppConstraintTypeMissmatch(const std::string& msg) : HazelnuppConstraintException(msg) {};
};
class HazelnuppConstraintMissingValue : public HazelnuppConstraintException
{
public:
HazelnuppConstraintMissingValue() : HazelnuppConstraintException() {};
HazelnuppConstraintMissingValue(const std::string& msg) : HazelnuppConstraintException(msg) {};
};

View File

@ -14,7 +14,7 @@ public:
bool constrainType = false;
//! Constrain the parameter to this value. Requires `constrainType` to be set to true.
DATA_TYPE wantedType;
DATA_TYPE wantedType = DATA_TYPE::VOID;
//! The default value for this parameter.
//! Gets applied if this parameter was not given.