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:
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:
42  HazelnuppValueNotConvertibleException(const std::string& msg) : HazelnuppException(msg) {};
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 }
Hazelnp
Definition: CmdArgsInterface.h:7
DataType.h
Hazelnp::HazelnuppInvalidKeyException::HazelnuppInvalidKeyException
HazelnuppInvalidKeyException(const std::string &msg)
Definition: HazelnuppException.h:33
Hazelnp::HazelnuppConstraintException::HazelnuppConstraintException
HazelnuppConstraintException()
Definition: HazelnuppException.h:50
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::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::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