Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 1 | #include <BaSyx/vab/backend/connector/native/frame/EntityWrapper.h> |
| 2 | |
| 3 | using basyx::vab::EntityWrapper; |
| 4 | |
| 5 | |
| 6 | namespace { |
| 7 | std::string prepareErrorCode(basyx::object::error errorCode) |
| 8 | { |
| 9 | std::string error; |
| 10 | switch (errorCode) |
| 11 | { |
| 12 | case basyx::object::error::PropertyNotFound: |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 13 | error = "404"; |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 14 | break; |
| 15 | case basyx::object::error::ObjectAlreadyExists: |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 16 | error = "422"; |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 17 | break; |
| 18 | case basyx::object::error::MalformedRequest: |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 19 | error = "400"; |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 20 | break; |
| 21 | default: |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 22 | error = "500"; |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 23 | 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 Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 34 | basyx::object build_exception(const std::string & code, const std::string & message) |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 35 | { |
| 36 | basyx::object::error error = basyx::object::error::ProviderException; |
| 37 | |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 38 | if (code == "404") |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 39 | { |
| 40 | error = basyx::object::error::PropertyNotFound; |
| 41 | } |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 42 | else if (code == "422") |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 43 | { |
| 44 | error = basyx::object::error::ObjectAlreadyExists; |
| 45 | } |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 46 | else if (code == "400") |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 47 | { |
| 48 | error = basyx::object::error::MalformedRequest; |
| 49 | } |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 50 | else if (code == "500") |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 51 | { |
| 52 | error = basyx::object::error::ProviderException; |
| 53 | }; |
| 54 | |
| 55 | return basyx::object::make_error(error, message); |
| 56 | }; |
| 57 | |
| 58 | basyx::json_t EntityWrapper::build_from_error(basyx::object::error error, const std::string & message) |
| 59 | { |
| 60 | json_t msg; |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 61 | msg["messageType"] = "Information"; |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 62 | msg["text"] = prepareErrorMessage(error, message); |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 63 | msg["code"] = prepareErrorCode(error); |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 64 | |
| 65 | basyx::json_t j_obj; |
| 66 | j_obj["success"] = false; |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 67 | j_obj["messages"] = json_t::array({ msg }); |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 68 | return j_obj; |
| 69 | }; |
| 70 | |
| 71 | basyx::json_t EntityWrapper::build_from_object(const basyx::object & object) |
| 72 | { |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 73 | if (object.IsError()) |
| 74 | { |
| 75 | return build_from_error(object.getError(), object.getErrorMessage()); |
| 76 | } |
| 77 | else |
| 78 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 79 | return basyx::serialization::json::serialize(object); |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 80 | } |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | |
| 84 | basyx::object EntityWrapper::from_json(const basyx::json_t & json) |
| 85 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 86 | bool exception = (json.contains("success") && !json["success"]); |
| 87 | |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 88 | // everyhing okay, deserialize entity |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 89 | if (!exception) |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 90 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 91 | return basyx::serialization::json::deserialize(json); |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 92 | } |
| 93 | // something went wrong, check for exception |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 94 | else if (json.contains("messages")) |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 95 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 96 | auto & msg = json["messages"][0]; |
| 97 | |
| 98 | auto code = msg["code"]; |
| 99 | auto text = msg["text"]; |
| 100 | |
| 101 | return build_exception(code, text); |
Thomas Psota | 92ce648 | 2020-07-10 12:30:43 +0200 | [diff] [blame] | 102 | } |
| 103 | // error and no exception; create one |
| 104 | else |
| 105 | { |
| 106 | return basyx::object::make_error(basyx::object::error::MalformedRequest); |
| 107 | }; |
| 108 | }; |