| Leonetienne/Hazelnupp Simple, easy to use, command line parameter interface

HazelnuppException.h
Go to the documentation of this file.
1 #pragma once
2 #include <stdexcept>
3 #include <string>
4 #include <sstream>
5 #include "DataType.h"
6
7 namespace Hazelnp
8 {
9 /** Generic hazelnupp exception
10 */
11 class HazelnuppException : public std::exception
12 {
13 public:
14 HazelnuppException() {};
15 HazelnuppException(const std::string& msg) : message{ msg } {};
16
17 //! Will return an error message
18 const std::string& What() const
19 {
20 return message;
21 }
22
23 protected:
24 std::string message;
25 };
26
27 /** Gets thrown when an non-existent key gets dereferenced
28 */
29 class HazelnuppInvalidKeyException : public HazelnuppException
30 {
31 public:
32 HazelnuppInvalidKeyException() : HazelnuppException() {};
33 HazelnuppInvalidKeyException(const std::string& msg) : HazelnuppException(msg) {};
34 };
35
36 /** Gets thrown when an attempt is made to retrieve the wrong data type from a value, when the value not convertible
37 */
38 class HazelnuppValueNotConvertibleException : public HazelnuppException
39 {
40 public:
41 HazelnuppValueNotConvertibleException() : HazelnuppException() {};
42 HazelnuppValueNotConvertibleException(const std::string& msg) : HazelnuppException(msg) {};
43 };
44
45 /** Gets thrown something bad happens because of parameter constraints
46 */
47 class HazelnuppConstraintException : public HazelnuppException
48 {
49 public:
50 HazelnuppConstraintException() : HazelnuppException() {};
51 HazelnuppConstraintException(const std::string& msg) : HazelnuppException(msg) {};
52 };
53
54 /** Gets thrown when a parameter is of a type that does not match the required type, and is not convertible to it
55 */
56 class HazelnuppConstraintTypeMissmatch : public HazelnuppConstraintException
57 {
58 public:
59 HazelnuppConstraintTypeMissmatch() : HazelnuppConstraintException() {};
60 HazelnuppConstraintTypeMissmatch(const std::string& msg) : HazelnuppConstraintException(msg) {};
61
62 HazelnuppConstraintTypeMissmatch(const std::string& key, const DATA_TYPE requiredType, const DATA_TYPE actualType, const std::string& paramDescription = "")
63 {
64 // Generate descriptive error message
65 std::stringstream ss;
66 ss << "Cannot convert parameter " << key << " to type " << DataTypeToString(requiredType)
67 << ". You supplied type: " << DataTypeToString(actualType) << ".";
68
69 // Add the parameter description, if provided
70 if (paramDescription.length() > 0)
71 ss << std::endl << key << " => " << paramDescription;
72
73 message = ss.str();
74 return;
75 };
76 };
77
78 /** Gets thrown when a parameter constrained to be required is not provided, and has no default value set
79 */
80 class HazelnuppConstraintMissingValue : public HazelnuppConstraintException
81 {
82 public:
83 HazelnuppConstraintMissingValue() : HazelnuppConstraintException() {};
84 HazelnuppConstraintMissingValue(const std::string& key, const std::string& paramDescription = "")
85 {
86 // Generate descriptive error message
87 std::stringstream ss;
88 ss << "Missing required parameter " << key << ".";
89
90 // Add the parameter description, if provided
91 if (paramDescription.length() > 0)
92 ss << std::endl << key << " => " << paramDescription;
93
94 message = ss.str();
95 return;
96 };
97 };
98
99 /** Gets thrown when a parameter constrained to be incompatible with other parameters gets supplied alongside at least one of those incompatible ones
100 */
101 class HazelnuppConstraintIncompatibleParameters : public HazelnuppConstraintException
102 {
103 public:
104 HazelnuppConstraintIncompatibleParameters() : HazelnuppConstraintException() {};
105 HazelnuppConstraintIncompatibleParameters(const std::string& key1, const std::string& key2)
106 {
107 // Generate descriptive error message
108 std::stringstream ss;
109 ss << "Parameter \"" << key1 << "\" is NOT compatible with parameter \"" << key2 << "\"!";
110
111 message = ss.str();
112 return;
113 };
114 };
115 }
Hazelnp
Definition: CmdArgsInterface.h:9
DataType.h
Hazelnp::HazelnuppInvalidKeyException::HazelnuppInvalidKeyException
HazelnuppInvalidKeyException(const std::string &msg)
Definition: HazelnuppException.h:33
Hazelnp::HazelnuppConstraintException::HazelnuppConstraintException
HazelnuppConstraintException()
Definition: HazelnuppException.h:50
Hazelnp::HazelnuppConstraintIncompatibleParameters
Gets thrown when a parameter constrained to be incompatible with other parameters gets supplied along...
Definition: HazelnuppException.h:101
Hazelnp::HazelnuppInvalidKeyException
Gets thrown when an non-existent key gets dereferenced.
Definition: HazelnuppException.h:29
Hazelnp::HazelnuppConstraintException::HazelnuppConstraintException
HazelnuppConstraintException(const std::string &msg)
Definition: HazelnuppException.h:51
Hazelnp::HazelnuppException::HazelnuppException
HazelnuppException(const std::string &msg)
Definition: HazelnuppException.h:15
Hazelnp::HazelnuppException::What
const std::string & What() const
Will return an error message.
Definition: HazelnuppException.h:18
Hazelnp::HazelnuppConstraintMissingValue::HazelnuppConstraintMissingValue
HazelnuppConstraintMissingValue()
Definition: HazelnuppException.h:83
Hazelnp::HazelnuppConstraintTypeMissmatch::HazelnuppConstraintTypeMissmatch
HazelnuppConstraintTypeMissmatch()
Definition: HazelnuppException.h:59
Hazelnp::HazelnuppConstraintException
Gets thrown something bad happens because of parameter constraints.
Definition: HazelnuppException.h:47
Hazelnp::HazelnuppConstraintIncompatibleParameters::HazelnuppConstraintIncompatibleParameters
HazelnuppConstraintIncompatibleParameters(const std::string &key1, const std::string &key2)
Definition: HazelnuppException.h:105
Hazelnp::HazelnuppException
Generic hazelnupp exception.
Definition: HazelnuppException.h:11
Hazelnp::HazelnuppConstraintMissingValue
Gets thrown when a parameter constrained to be required is not provided, and has no default value set...
Definition: HazelnuppException.h:80
Hazelnp::HazelnuppConstraintTypeMissmatch
Gets thrown when a parameter is of a type that does not match the required type, and is not convertib...
Definition: HazelnuppException.h:56
Hazelnp::HazelnuppException::HazelnuppException
HazelnuppException()
Definition: HazelnuppException.h:14
Hazelnp::HazelnuppException::message
std::string message
Definition: HazelnuppException.h:24
Hazelnp::HazelnuppValueNotConvertibleException::HazelnuppValueNotConvertibleException
HazelnuppValueNotConvertibleException(const std::string &msg)
Definition: HazelnuppException.h:42
Hazelnp::HazelnuppConstraintIncompatibleParameters::HazelnuppConstraintIncompatibleParameters
HazelnuppConstraintIncompatibleParameters()
Definition: HazelnuppException.h:104
Hazelnp::DATA_TYPE
DATA_TYPE
The different data types a paramater can be.
Definition: DataType.h:8
Hazelnp::HazelnuppConstraintMissingValue::HazelnuppConstraintMissingValue
HazelnuppConstraintMissingValue(const std::string &key, const std::string &paramDescription="")
Definition: HazelnuppException.h:84
Hazelnp::HazelnuppConstraintTypeMissmatch::HazelnuppConstraintTypeMissmatch
HazelnuppConstraintTypeMissmatch(const std::string &key, const DATA_TYPE requiredType, const DATA_TYPE actualType, const std::string &paramDescription="")
Definition: HazelnuppException.h:62
Hazelnp::DataTypeToString
static std::string DataTypeToString(DATA_TYPE type)
Definition: DataType.h:17
Hazelnp::HazelnuppConstraintTypeMissmatch::HazelnuppConstraintTypeMissmatch
HazelnuppConstraintTypeMissmatch(const std::string &msg)
Definition: HazelnuppException.h:60
Hazelnp::HazelnuppValueNotConvertibleException
Gets thrown when an attempt is made to retrieve the wrong data type from a value, when the value not ...
Definition: HazelnuppException.h:38
Hazelnp::HazelnuppValueNotConvertibleException::HazelnuppValueNotConvertibleException
HazelnuppValueNotConvertibleException()
Definition: HazelnuppException.h:41
Hazelnp::HazelnuppInvalidKeyException::HazelnuppInvalidKeyException
HazelnuppInvalidKeyException()
Definition: HazelnuppException.h:32