From 0131b30f1781497452d6a2435e4a2918c08ef143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ba=C5=99tip=C3=A1n?= Date: Thu, 27 Jun 2024 10:43:53 +0200 Subject: [PATCH] Send to connect: trying to extract server error message for nicer error dialog --- src/slic3r/Utils/PrusaConnect.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/slic3r/Utils/PrusaConnect.cpp b/src/slic3r/Utils/PrusaConnect.cpp index c85eb054aae..2fad12c76a8 100644 --- a/src/slic3r/Utils/PrusaConnect.cpp +++ b/src/slic3r/Utils/PrusaConnect.cpp @@ -46,6 +46,24 @@ std::string escape_path_by_element(const boost::filesystem::path& path) } return ret_val; } + +boost::optional get_error_message_from_response_body(const std::string& body) +{ + boost::optional message; + std::stringstream ss(body); + pt::ptree ptree; + try + { + pt::read_json(ss, ptree); + message = ptree.get_optional("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;