@@ -277,6 +277,8 @@ std::string_view getenv_sv(const char* name) noexcept
277277 return {};
278278}
279279
280+ const static std::string g_baas_coid_header_name (" x-appservices-request-id" );
281+
280282} // namespace
281283
282284app::Response do_http_request (const app::Request& request)
@@ -354,7 +356,7 @@ app::Response do_http_request(const app::Request& request)
354356 }
355357 if (logger->would_log (util::Logger::Level::trace)) {
356358 std::string coid = [&] {
357- auto coid_header = response_headers.find (" X-Appservices-Request-Id " );
359+ auto coid_header = response_headers.find (g_baas_coid_header_name );
358360 if (coid_header == response_headers.end ()) {
359361 return std::string{};
360362 }
@@ -401,8 +403,21 @@ class Baasaas {
401403 logger->info (" Starting baasaas container" );
402404 }
403405
404- auto resp = do_request (std::move (url_path), app::HttpMethod::post );
406+ auto [resp, baas_coid] = do_request (std::move (url_path), app::HttpMethod::post );
407+ if (!resp[" id" ].is_string ()) {
408+ throw RuntimeError (
409+ ErrorCodes::RuntimeError,
410+ util::format (
411+ " Failed to start baas container, got response without container ID: \" %1\" (baas coid: %2)" ,
412+ resp.dump (), baas_coid));
413+ }
405414 m_container_id = resp[" id" ].get <std::string>();
415+ if (m_container_id.empty ()) {
416+ throw RuntimeError (
417+ ErrorCodes::InvalidArgument,
418+ util::format (" Failed to start baas container, got response with empty container ID (baas coid: %1)" ,
419+ baas_coid));
420+ }
406421 logger->info (" Baasaas container started with id \" %1\" " , m_container_id);
407422 auto lock_file = util::File (std::string{s_baasaas_lock_file_name}, util::File::mode_Write);
408423 lock_file.write (m_container_id);
@@ -442,9 +457,15 @@ class Baasaas {
442457 while (std::chrono::system_clock::now () - poll_start_at < std::chrono::minutes (2 ) &&
443458 m_http_endpoint.empty ()) {
444459 if (http_endpoint.empty ()) {
445- auto status_obj =
460+ auto [ status_obj, baas_coid] =
446461 do_request (util::format (" containerStatus?id=%1" , m_container_id), app::HttpMethod::get);
447462 if (!status_obj[" httpUrl" ].is_null ()) {
463+ if (!status_obj[" httpUrl" ].is_string () || !status_obj[" mongoUrl" ].is_string ()) {
464+ throw RuntimeError (ErrorCodes::RuntimeError,
465+ util::format (" Error polling for baasaas instance. httpUrl or mongoUrl is "
466+ " the wrong format: \" %1\" (baas coid: %2)" ,
467+ status_obj.dump (), baas_coid));
468+ }
448469 http_endpoint = status_obj[" httpUrl" ].get <std::string>();
449470 mongo_endpoint = status_obj[" mongoUrl" ].get <std::string>();
450471 }
@@ -507,7 +528,7 @@ class Baasaas {
507528 }
508529
509530private:
510- nlohmann::json do_request (std::string api_path, app::HttpMethod method)
531+ std::pair< nlohmann::json, std::string> do_request (std::string api_path, app::HttpMethod method)
511532 {
512533 app::Request request;
513534
@@ -516,10 +537,29 @@ class Baasaas {
516537 request.headers .insert_or_assign (" apiKey" , m_api_key);
517538 request.headers .insert_or_assign (" Content-Type" , " application/json" );
518539 auto response = do_http_request (request);
519- REALM_ASSERT_EX (response.http_status_code >= 200 && response.http_status_code < 300 ,
520- util::format (" Baasaas api response code: %1 Response body: %2" , response.http_status_code ,
521- response.body ));
522- return nlohmann::json::parse (response.body );
540+ if (response.http_status_code < 200 || response.http_status_code >= 300 ) {
541+ throw RuntimeError (ErrorCodes::HTTPError,
542+ util::format (" Baasaas api response code: %1 Response body: %2, Baas coid: %3" ,
543+ response.http_status_code , response.body ,
544+ baas_coid_from_response (response)));
545+ }
546+ try {
547+ return {nlohmann::json::parse (response.body ), baas_coid_from_response (response)};
548+ }
549+ catch (const nlohmann::json::exception& e) {
550+ throw RuntimeError (
551+ ErrorCodes::MalformedJson,
552+ util::format (" Error making baasaas request to %1 (baas coid %2): Invalid json returned \" %3\" (%4)" ,
553+ request.url , baas_coid_from_response (response), response.body , e.what ()));
554+ }
555+ }
556+
557+ std::string baas_coid_from_response (const app::Response& resp)
558+ {
559+ if (auto it = resp.headers .find (g_baas_coid_header_name); it != resp.headers .end ()) {
560+ return it->second ;
561+ }
562+ return " <not found>" ;
523563 }
524564
525565 static std::string get_baasaas_base_url ()
0 commit comments