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
7namespace Hazelnp
8{
9 /** Generic hazelnupp exception
10 */
11 class HazelnuppException : public std::exception
12 {
13 public:
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 */
30 {
31 public:
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 */
39 {
40 public:
43 };
44
45 /** Gets thrown something bad happens because of parameter constraints
46 */
48 {
49 public:
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 */
57 {
58 public:
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 */
81 {
82 public:
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 */
102 {
103 public:
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}
Gets thrown something bad happens because of parameter constraints.
HazelnuppConstraintException(const std::string &msg)
Gets thrown when a parameter constrained to be incompatible with other parameters gets supplied along...
HazelnuppConstraintIncompatibleParameters(const std::string &key1, const std::string &key2)
Gets thrown when a parameter constrained to be required is not provided, and has no default value set...
HazelnuppConstraintMissingValue(const std::string &key, const std::string &paramDescription="")
Gets thrown when a parameter is of a type that does not match the required type, and is not convertib...
HazelnuppConstraintTypeMissmatch(const std::string &msg)
HazelnuppConstraintTypeMissmatch(const std::string &key, const DATA_TYPE requiredType, const DATA_TYPE actualType, const std::string &paramDescription="")
Generic hazelnupp exception.
const std::string & What() const
Will return an error message.
HazelnuppException(const std::string &msg)
Gets thrown when an non-existent key gets dereferenced.
HazelnuppInvalidKeyException(const std::string &msg)
Gets thrown when an attempt is made to retrieve the wrong data type from a value, when the value not ...
HazelnuppValueNotConvertibleException(const std::string &msg)
DATA_TYPE
The different data types a paramater can be.
Definition: DataType.h:9
static std::string DataTypeToString(DATA_TYPE type)
Definition: DataType.h:17