exceptions.cpp
Go to the documentation of this file.
1 #include "exceptions.h"
2 
3 #include <sstream>
4 
5 #include <SimoxUtility/algorithm/string.h>
6 
8 {
9 
10  ArvizReflectionError::ArvizReflectionError(const std::string& msg) : std::runtime_error(msg)
11  {
12  }
13 
15  const nlohmann::json& j) :
16  ArvizReflectionError(makeMsg(missingKey, j))
17  {
18  }
19 
20  static std::string
21  getAvailableKeys(const nlohmann::json& j)
22  {
23  std::stringstream ss;
24  for (const auto& item : j.items())
25  {
26  ss << "\n- " << item.key();
27  }
28  return ss.str();
29  }
30 
31  std::string
32  NoTypeNameEntryInJsonObject::makeMsg(const std::string& missingKey, const nlohmann::json& j)
33  {
34  std::stringstream ss;
35  ss << "No type name entry with key '" << missingKey << "' in JSON object.\n";
36  if (j.is_object())
37  {
38  ss << "Available keys: " << getAvailableKeys(j);
39  }
40  else
41  {
42  ss << "JSON document is not an object, but a " << j.type_name() << ".";
43  }
44  return ss.str();
45  }
46 
48  const std::string& typeName,
49  const nlohmann::json& j) :
50  ArvizReflectionError(makeMsg(key, typeName, j))
51  {
52  }
53 
54  std::string
55  TypeNameEntryAlreadyInJsonObject::makeMsg(const std::string& key,
56  const std::string& typeName,
57  const nlohmann::json& j)
58  {
59  std::stringstream ss;
60  ss << "Key '" << key << "' already used in JSON object "
61  << "when trying to store the type name '" << typeName << "'.\n";
62  ss << "Used keys:" << getAvailableKeys(j);
63  return ss.str();
64  }
65 
66  TypeNameMismatch::TypeNameMismatch(const std::string& typeInJson,
67  const std::string& typeOfObject) :
68  ArvizReflectionError(makeMsg(typeInJson, typeOfObject))
69  {
70  }
71 
72  std::string
73  TypeNameMismatch::makeMsg(const std::string& typeInJson, const std::string& typeOfObject)
74  {
75  std::stringstream ss;
76  ss << "Type stored JSON (" << typeInJson << ") does not match the type of passed object ("
77  << typeOfObject << ").";
78  return ss.str();
79  }
80 
81  NoSerializerForType::NoSerializerForType(const std::string& typeName) :
82  ArvizReflectionError("No registered serializer for type '" + typeName + "'.")
83  {
84  }
85 
87  const std::string& typeName,
88  const std::vector<std::string>& acceptedTypes) :
89  ArvizReflectionError(makeMsg(typeName, acceptedTypes))
90  {
91  }
92 
93  std::string
94  SerializerAlreadyRegisteredForType::makeMsg(const std::string& typeName,
95  const std::vector<std::string>& acceptedTypes)
96  {
97  std::stringstream ss;
98  ss << "There is already a registered serializer for type '" + typeName + "'.";
99  if (!acceptedTypes.empty())
100  {
101  ss << "\nAccepted types:\n" + simox::alg::join(acceptedTypes, "\n- ");
102  }
103  return ss.str();
104  }
105 
106 } // namespace armarx::viz::error
armarx::viz::error::SerializerAlreadyRegisteredForType::SerializerAlreadyRegisteredForType
SerializerAlreadyRegisteredForType(const std::string &typeName, const std::vector< std::string > &acceptedTypes={})
Definition: exceptions.cpp:86
armarx::viz::error::ArvizReflectionError
Base exception class.
Definition: exceptions.h:11
armarx::viz::error::NoSerializerForType::NoSerializerForType
NoSerializerForType(const std::string &typeName)
Definition: exceptions.cpp:81
armarx::viz::error
Definition: exceptions.cpp:7
armarx::viz::error::TypeNameEntryAlreadyInJsonObject::TypeNameEntryAlreadyInJsonObject
TypeNameEntryAlreadyInJsonObject(const std::string &key, const std::string &typeName, const nlohmann::json &j)
Definition: exceptions.cpp:47
armarx::viz::error::NoTypeNameEntryInJsonObject::NoTypeNameEntryInJsonObject
NoTypeNameEntryInJsonObject(const std::string &missingKey, const nlohmann::json &j)
Definition: exceptions.cpp:14
std
Definition: Application.h:66
exceptions.h
armarx::viz::error::ArvizReflectionError::ArvizReflectionError
ArvizReflectionError(const std::string &msg)
Definition: exceptions.cpp:10
armarx::viz::error::TypeNameMismatch::TypeNameMismatch
TypeNameMismatch(const std::string &typeInJson, const std::string &typeOfObject)
Definition: exceptions.cpp:66