Send to connect: trying to extract server error message for nicer error dialog

This commit is contained in:
Jan Bařtipán 2024-06-27 10:43:53 +02:00 committed by Lukas Matena
parent 510bb208cb
commit 0131b30f17

View File

@ -46,6 +46,24 @@ std::string escape_path_by_element(const boost::filesystem::path& path)
}
return ret_val;
}
boost::optional<std::string> get_error_message_from_response_body(const std::string& body)
{
boost::optional<std::string> message;
std::stringstream ss(body);
pt::ptree ptree;
try
{
pt::read_json(ss, ptree);
message = ptree.get_optional<std::string>("message");
}
// ignore possible errors if body is not valid JSON
catch (std::exception&)
{}
return message;
}
}
PrusaConnectNew::PrusaConnectNew(DynamicPrintConfig *config)
@ -125,7 +143,9 @@ bool PrusaConnectNew::init_upload(PrintHostUpload upload_data, std::string& out)
BOOST_LOG_TRIVIAL(error) << body;
BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error registering file: %2%, HTTP %3%, body: `%4%`") % name % error % status % body;
res = false;
out = GUI::into_u8(format_error(body, error, status));
out = get_error_message_from_response_body(body).value_or_eval([&](){
return GUI::into_u8(format_error(body, error, status));
});
})
.perform_sync();
return res;