Made error messages MUCH MORE descriptive
This commit is contained in:
parent
82375e334c
commit
d82d85adb8
@ -65,27 +65,27 @@ void Hazelnupp::Parse(const int argc, const char* const* argv)
|
|||||||
if ((!catchHelp) || (!HasParam("--help")))
|
if ((!catchHelp) || (!HasParam("--help")))
|
||||||
ApplyConstraints();
|
ApplyConstraints();
|
||||||
}
|
}
|
||||||
catch (const HazelnuppConstraintTypeMissmatch& hctm)
|
catch (const HazelnuppConstraintTypeMissmatch& exc)
|
||||||
{
|
{
|
||||||
if (crashOnFail)
|
if (crashOnFail)
|
||||||
{
|
{
|
||||||
std::cout << GenerateDocumentation() << std::endl;
|
std::cout << GenerateDocumentation() << std::endl << std::endl;
|
||||||
std::cerr << "Fatal error: Command-line parameter value-type mismatch at \"" << hctm.What() << "\"!";
|
std::cerr << "Parameter error: " << exc.What() << std::endl;
|
||||||
quick_exit(-1009);
|
quick_exit(-1009);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw hctm; // yeet
|
throw exc; // yeet
|
||||||
}
|
}
|
||||||
catch (const HazelnuppConstraintMissingValue& hctm)
|
catch (const HazelnuppConstraintMissingValue& exc)
|
||||||
{
|
{
|
||||||
if (crashOnFail)
|
if (crashOnFail)
|
||||||
{
|
{
|
||||||
std::cout << GenerateDocumentation() << std::endl;
|
std::cout << GenerateDocumentation() << std::endl << std::endl;
|
||||||
std::cerr << "Fatal error: Missing required command-line parameter \"" << hctm.What() << "\"!";
|
std::cerr << "Parameter error: " << exc.What() << std::endl;
|
||||||
quick_exit(-1010);
|
quick_exit(-1010);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw hctm; // yeet
|
throw exc; // yeet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Catch --help parameter
|
// Catch --help parameter
|
||||||
@ -169,28 +169,44 @@ bool Hazelnupp::HasParam(const std::string& key) const
|
|||||||
|
|
||||||
Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint)
|
Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const ParamConstraint* constraint)
|
||||||
{
|
{
|
||||||
|
// This is the raw (unconverted) data type the user provided
|
||||||
|
DATA_TYPE rawInputType;
|
||||||
|
|
||||||
// Constraint values
|
// Constraint values
|
||||||
const bool constrainType = (constraint != nullptr) && (constraint->constrainType);
|
const bool constrainType = (constraint != nullptr) && (constraint->constrainType);
|
||||||
|
|
||||||
// Void-type
|
// Void-type
|
||||||
if (values.size() == 0)
|
if (values.size() == 0)
|
||||||
{
|
{
|
||||||
|
rawInputType = DATA_TYPE::VOID;
|
||||||
|
|
||||||
// Is a list forced via a constraint? If yes, return an empty list
|
// Is a list forced via a constraint? If yes, return an empty list
|
||||||
if ((constrainType) &&
|
if ((constrainType) &&
|
||||||
(constraint->wantedType == DATA_TYPE::LIST))
|
(constraint->wantedType == DATA_TYPE::LIST))
|
||||||
return new ListValue();
|
return new ListValue();
|
||||||
|
|
||||||
// Is a string forced via a constraint? If yes, return an empty string
|
// Is a string forced via a constraint? If yes, return an empty string
|
||||||
if ((constrainType) &&
|
else if ((constrainType) &&
|
||||||
(constraint->wantedType == DATA_TYPE::STRING))
|
(constraint->wantedType == DATA_TYPE::STRING))
|
||||||
return new StringValue("");
|
return new StringValue("");
|
||||||
|
|
||||||
|
// Is an int or float forced via constraint? If yes, throw an exception
|
||||||
|
else if ((constrainType) &&
|
||||||
|
((constraint->wantedType == DATA_TYPE::INT) ||
|
||||||
|
(constraint->wantedType == DATA_TYPE::FLOAT)))
|
||||||
|
throw HazelnuppConstraintTypeMissmatch(
|
||||||
|
constraint->key,
|
||||||
|
constraint->wantedType,
|
||||||
|
rawInputType,
|
||||||
|
GetDescription(constraint->key)
|
||||||
|
);
|
||||||
|
|
||||||
// Else, just return the void type
|
// Else, just return the void type
|
||||||
return new VoidValue;
|
return new VoidValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Force void type by constraint
|
// Force void type by constraint
|
||||||
if ((constrainType) &&
|
else if ((constrainType) &&
|
||||||
(constraint->wantedType == DATA_TYPE::VOID))
|
(constraint->wantedType == DATA_TYPE::VOID))
|
||||||
{
|
{
|
||||||
return new VoidValue;
|
return new VoidValue;
|
||||||
@ -199,11 +215,18 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
|
|||||||
// List-type
|
// List-type
|
||||||
else if (values.size() > 1)
|
else if (values.size() > 1)
|
||||||
{
|
{
|
||||||
|
rawInputType = DATA_TYPE::LIST;
|
||||||
|
|
||||||
// Should the type be something other than list?
|
// Should the type be something other than list?
|
||||||
if ((constrainType) &&
|
if ((constrainType) &&
|
||||||
(constraint->wantedType != DATA_TYPE::LIST))
|
(constraint->wantedType != DATA_TYPE::LIST))
|
||||||
{
|
{
|
||||||
throw HazelnuppConstraintTypeMissmatch(values[0] + " " + values[1]);
|
throw HazelnuppConstraintTypeMissmatch(
|
||||||
|
constraint->key,
|
||||||
|
constraint->wantedType,
|
||||||
|
rawInputType,
|
||||||
|
GetDescription(constraint->key)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ListValue* newList = new ListValue();
|
ListValue* newList = new ListValue();
|
||||||
@ -222,6 +245,8 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
|
|||||||
// String
|
// String
|
||||||
if (!StringTools::IsNumeric(val, true))
|
if (!StringTools::IsNumeric(val, true))
|
||||||
{
|
{
|
||||||
|
rawInputType = DATA_TYPE::STRING;
|
||||||
|
|
||||||
// Is the type not supposed to be a string?
|
// Is the type not supposed to be a string?
|
||||||
// void and list are already sorted out
|
// void and list are already sorted out
|
||||||
if ((constrainType) &&
|
if ((constrainType) &&
|
||||||
@ -237,9 +262,14 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
|
|||||||
tmp = nullptr;
|
tmp = nullptr;
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
// Else it not possible to convert to a numeric
|
// Else it is not possible to convert to a numeric
|
||||||
else
|
else
|
||||||
throw HazelnuppConstraintTypeMissmatch(val);
|
throw HazelnuppConstraintTypeMissmatch(
|
||||||
|
constraint->key,
|
||||||
|
constraint->wantedType,
|
||||||
|
rawInputType,
|
||||||
|
GetDescription(constraint->key)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StringValue(val);
|
return new StringValue(val);
|
||||||
@ -257,6 +287,8 @@ Value* Hazelnupp::ParseValue(const std::vector<std::string>& values, const Param
|
|||||||
|
|
||||||
if (StringTools::ParseNumber(val, isInt, num))
|
if (StringTools::ParseNumber(val, isInt, num))
|
||||||
{
|
{
|
||||||
|
rawInputType = isInt ? DATA_TYPE::INT : DATA_TYPE::FLOAT;
|
||||||
|
|
||||||
// Is the type constrained?
|
// Is the type constrained?
|
||||||
// (only int and float left)
|
// (only int and float left)
|
||||||
if (constrainType)
|
if (constrainType)
|
||||||
@ -458,7 +490,8 @@ std::string Hazelnupp::GenerateDocumentation() const
|
|||||||
if (pde.description.length() > 0)
|
if (pde.description.length() > 0)
|
||||||
ss << pde.description;
|
ss << pde.description;
|
||||||
|
|
||||||
ss << std::endl << std::endl;
|
if (&it.second != &(paramInfos.cend().operator--())->second)
|
||||||
|
ss << std::endl << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -492,7 +525,10 @@ void Hazelnupp::ApplyConstraints()
|
|||||||
// Is it important to have the missing parameter?
|
// Is it important to have the missing parameter?
|
||||||
if (pc.second.required)
|
if (pc.second.required)
|
||||||
// Throw an error message then
|
// Throw an error message then
|
||||||
throw HazelnuppConstraintMissingValue(pc.second.key);
|
throw HazelnuppConstraintMissingValue(
|
||||||
|
pc.second.key,
|
||||||
|
GetDescription(pc.second.key)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include "DataType.h"
|
||||||
|
|
||||||
namespace Hazelnp
|
namespace Hazelnp
|
||||||
{
|
{
|
||||||
@ -55,6 +58,21 @@ namespace Hazelnp
|
|||||||
public:
|
public:
|
||||||
HazelnuppConstraintTypeMissmatch() : HazelnuppConstraintException() {};
|
HazelnuppConstraintTypeMissmatch() : HazelnuppConstraintException() {};
|
||||||
HazelnuppConstraintTypeMissmatch(const std::string& msg) : HazelnuppConstraintException(msg) {};
|
HazelnuppConstraintTypeMissmatch(const std::string& msg) : HazelnuppConstraintException(msg) {};
|
||||||
|
|
||||||
|
HazelnuppConstraintTypeMissmatch(const std::string& key, const DATA_TYPE requiredType, const DATA_TYPE actualType, const std::string& paramDescription = "")
|
||||||
|
{
|
||||||
|
// Generate descriptive error message
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Cannot convert parameter " << key << " to type " << DataTypeToString(requiredType)
|
||||||
|
<< ". You supplied type: " << DataTypeToString(actualType) << ".";
|
||||||
|
|
||||||
|
// Add the parameter description, if provided
|
||||||
|
if (paramDescription.length() > 0)
|
||||||
|
ss << std::endl << key << " => " << paramDescription;
|
||||||
|
|
||||||
|
message = ss.str();
|
||||||
|
return;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Gets thrown when a parameter constrained to be required is not provided, and has no default value set
|
/** Gets thrown when a parameter constrained to be required is not provided, and has no default value set
|
||||||
@ -63,6 +81,18 @@ namespace Hazelnp
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HazelnuppConstraintMissingValue() : HazelnuppConstraintException() {};
|
HazelnuppConstraintMissingValue() : HazelnuppConstraintException() {};
|
||||||
HazelnuppConstraintMissingValue(const std::string& msg) : HazelnuppConstraintException(msg) {};
|
HazelnuppConstraintMissingValue(const std::string& key, const std::string& paramDescription = "")
|
||||||
|
{
|
||||||
|
// Generate descriptive error message
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Missing required parameter " << key << ".";
|
||||||
|
|
||||||
|
// Add the parameter description, if provided
|
||||||
|
if (paramDescription.length() > 0)
|
||||||
|
ss << std::endl << key << " => " << paramDescription;
|
||||||
|
|
||||||
|
message = ss.str();
|
||||||
|
return;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user