blob: 9d2aa2a9cf07bf8ddd9c92d0e8c5f849156eda01 [file] [log] [blame]
Thomas Psota92ce6482020-07-10 12:30:43 +02001#include <BaSyx/vab/backend/connector/native/frame/EntityWrapper.h>
2
3using basyx::vab::EntityWrapper;
4
5
6namespace {
7 std::string prepareErrorCode(basyx::object::error errorCode)
8 {
9 std::string error;
10 switch (errorCode)
11 {
12 case basyx::object::error::PropertyNotFound:
Constantin Ziesche02817f12020-08-04 21:40:43 +020013 error = "404";
Thomas Psota92ce6482020-07-10 12:30:43 +020014 break;
15 case basyx::object::error::ObjectAlreadyExists:
Constantin Ziesche02817f12020-08-04 21:40:43 +020016 error = "422";
Thomas Psota92ce6482020-07-10 12:30:43 +020017 break;
18 case basyx::object::error::MalformedRequest:
Constantin Ziesche02817f12020-08-04 21:40:43 +020019 error = "400";
Thomas Psota92ce6482020-07-10 12:30:43 +020020 break;
21 default:
Constantin Ziesche02817f12020-08-04 21:40:43 +020022 error = "500";
Thomas Psota92ce6482020-07-10 12:30:43 +020023 break;
24 };
25 return error;
26 };
27
28 std::string prepareErrorMessage(basyx::object::error errorCode, const std::string & message)
29 {
30 return prepareErrorCode(errorCode) + ": " + message;
31 };
32}
33
Constantin Ziesche02817f12020-08-04 21:40:43 +020034basyx::object build_exception(const std::string & code, const std::string & message)
Thomas Psota92ce6482020-07-10 12:30:43 +020035{
36 basyx::object::error error = basyx::object::error::ProviderException;
37
Constantin Ziesche02817f12020-08-04 21:40:43 +020038 if (code == "404")
Thomas Psota92ce6482020-07-10 12:30:43 +020039 {
40 error = basyx::object::error::PropertyNotFound;
41 }
Constantin Ziesche02817f12020-08-04 21:40:43 +020042 else if (code == "422")
Thomas Psota92ce6482020-07-10 12:30:43 +020043 {
44 error = basyx::object::error::ObjectAlreadyExists;
45 }
Constantin Ziesche02817f12020-08-04 21:40:43 +020046 else if (code == "400")
Thomas Psota92ce6482020-07-10 12:30:43 +020047 {
48 error = basyx::object::error::MalformedRequest;
49 }
Constantin Ziesche02817f12020-08-04 21:40:43 +020050 else if (code == "500")
Thomas Psota92ce6482020-07-10 12:30:43 +020051 {
52 error = basyx::object::error::ProviderException;
53 };
54
55 return basyx::object::make_error(error, message);
56};
57
58basyx::json_t EntityWrapper::build_from_error(basyx::object::error error, const std::string & message)
59{
60 json_t msg;
Constantin Ziesche02817f12020-08-04 21:40:43 +020061 msg["messageType"] = "Information";
Thomas Psota92ce6482020-07-10 12:30:43 +020062 msg["text"] = prepareErrorMessage(error, message);
Constantin Ziesche02817f12020-08-04 21:40:43 +020063 msg["code"] = prepareErrorCode(error);
Thomas Psota92ce6482020-07-10 12:30:43 +020064
65 basyx::json_t j_obj;
66 j_obj["success"] = false;
Thomas Psota92ce6482020-07-10 12:30:43 +020067 j_obj["messages"] = json_t::array({ msg });
Thomas Psota92ce6482020-07-10 12:30:43 +020068 return j_obj;
69};
70
71basyx::json_t EntityWrapper::build_from_object(const basyx::object & object)
72{
Thomas Psota92ce6482020-07-10 12:30:43 +020073 if (object.IsError())
74 {
75 return build_from_error(object.getError(), object.getErrorMessage());
76 }
77 else
78 {
Constantin Ziesche02817f12020-08-04 21:40:43 +020079 return basyx::serialization::json::serialize(object);
Thomas Psota92ce6482020-07-10 12:30:43 +020080 }
Thomas Psota92ce6482020-07-10 12:30:43 +020081};
82
83
84basyx::object EntityWrapper::from_json(const basyx::json_t & json)
85{
Constantin Ziesche02817f12020-08-04 21:40:43 +020086 bool exception = (json.contains("success") && !json["success"]);
87
Thomas Psota92ce6482020-07-10 12:30:43 +020088 // everyhing okay, deserialize entity
Constantin Ziesche02817f12020-08-04 21:40:43 +020089 if (!exception)
Thomas Psota92ce6482020-07-10 12:30:43 +020090 {
Constantin Ziesche02817f12020-08-04 21:40:43 +020091 return basyx::serialization::json::deserialize(json);
Thomas Psota92ce6482020-07-10 12:30:43 +020092 }
93 // something went wrong, check for exception
Constantin Ziesche02817f12020-08-04 21:40:43 +020094 else if (json.contains("messages"))
Thomas Psota92ce6482020-07-10 12:30:43 +020095 {
Constantin Ziesche02817f12020-08-04 21:40:43 +020096 auto & msg = json["messages"][0];
97
98 auto code = msg["code"];
99 auto text = msg["text"];
100
101 return build_exception(code, text);
Thomas Psota92ce6482020-07-10 12:30:43 +0200102 }
103 // error and no exception; create one
104 else
105 {
106 return basyx::object::make_error(basyx::object::error::MalformedRequest);
107 };
108};