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