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: |
| 13 | error = "ResourceNotFoundException"; |
| 14 | break; |
| 15 | case basyx::object::error::ObjectAlreadyExists: |
| 16 | error = "ResourceAlreadyExistsException"; |
| 17 | break; |
| 18 | case basyx::object::error::MalformedRequest: |
| 19 | error = "MalformedRequestException"; |
| 20 | break; |
| 21 | default: |
| 22 | error = "ProviderException"; |
| 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 | |
| 34 | basyx::object build_exception(const std::string & type, const std::string & message) |
| 35 | { |
| 36 | basyx::object::error error = basyx::object::error::ProviderException; |
| 37 | |
| 38 | if (type == "ResourceNotFoundException") |
| 39 | { |
| 40 | error = basyx::object::error::PropertyNotFound; |
| 41 | } |
| 42 | else if (type == "ResourceAlreadyExistsException") |
| 43 | { |
| 44 | error = basyx::object::error::ObjectAlreadyExists; |
| 45 | } |
| 46 | else if (type == "MalformedRequestException") |
| 47 | { |
| 48 | error = basyx::object::error::MalformedRequest; |
| 49 | } |
| 50 | else if (type == "ProviderException") |
| 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; |
| 61 | msg["messageType"] = 6; |
| 62 | msg["text"] = prepareErrorMessage(error, message); |
| 63 | msg["code"] = nullptr; |
| 64 | |
| 65 | basyx::json_t j_obj; |
| 66 | j_obj["success"] = false; |
| 67 | j_obj["isException"] = true; |
| 68 | j_obj["messages"] = json_t::array({ msg }); |
| 69 | j_obj["entityType"] = prepareErrorCode(error); |
| 70 | return j_obj; |
| 71 | }; |
| 72 | |
| 73 | basyx::json_t EntityWrapper::build_from_object(const basyx::object & object) |
| 74 | { |
| 75 | basyx::json_t j_obj; |
| 76 | if (object.IsError()) |
| 77 | { |
| 78 | return build_from_error(object.getError(), object.getErrorMessage()); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | j_obj["success"] = true; |
| 83 | // j_obj["isException"] = false; |
| 84 | j_obj["entityType"] = "entity"; |
| 85 | j_obj["entity"] = basyx::serialization::json::serialize(object); |
| 86 | } |
| 87 | return j_obj; |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | basyx::object EntityWrapper::from_json(const basyx::json_t & json) |
| 92 | { |
| 93 | bool success = json["success"]; |
| 94 | // everyhing okay, deserialize entity |
| 95 | if (success) |
| 96 | { |
| 97 | if (json.contains("entity")) |
| 98 | return basyx::serialization::json::deserialize(json["entity"]); |
| 99 | else |
| 100 | return basyx::object::make_null(); |
| 101 | } |
| 102 | // something went wrong, check for exception |
| 103 | else if (json.contains("isException") && json.contains("messages")) |
| 104 | { |
| 105 | return build_exception(json["entityType"], json["messages"][0]["text"]); |
| 106 | } |
| 107 | // error and no exception; create one |
| 108 | else |
| 109 | { |
| 110 | return basyx::object::make_error(basyx::object::error::MalformedRequest); |
| 111 | }; |
| 112 | }; |