From 21ddd99d557d750185b130076607aa8dcf819e8e Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Thu, 19 Jun 2025 16:14:01 +0200 Subject: [PATCH] Properly handle error output. --- profiler/src/profiler/TracyLlmApi.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/profiler/src/profiler/TracyLlmApi.cpp b/profiler/src/profiler/TracyLlmApi.cpp index eebdc80b..6ea55708 100644 --- a/profiler/src/profiler/TracyLlmApi.cpp +++ b/profiler/src/profiler/TracyLlmApi.cpp @@ -122,15 +122,26 @@ static size_t StreamFn( void* _data, size_t size, size_t num, void* ptr ) { if( strncmp( v.str.c_str(), "data: [DONE]", 12 ) == 0 ) return sz; - auto pos = v.str.find( "data: " ); - if( pos == std::string::npos ) break; - pos += 6; - auto end = v.str.find( "\n\n", pos ); - if( end == std::string::npos ) break; + auto err = v.str.find( "error: " ); + if( err != std::string::npos ) + { + err += 7; + auto end = v.str.find( "\n\n", err ); + if( end == std::string::npos ) break; + throw std::runtime_error( v.str.substr( err, end - err ) ); + } + else + { + auto pos = v.str.find( "data: " ); + if( pos == std::string::npos ) break; + pos += 6; + auto end = v.str.find( "\n\n", pos ); + if( end == std::string::npos ) break; - nlohmann::json json = nlohmann::json::parse( v.str.c_str() + pos, v.str.c_str() + end ); - if( !v.callback( json ) ) return CURL_WRITEFUNC_ERROR; - v.str.erase( 0, end + 2 ); + nlohmann::json json = nlohmann::json::parse( v.str.c_str() + pos, v.str.c_str() + end ); + if( !v.callback( json ) ) return CURL_WRITEFUNC_ERROR; + v.str.erase( 0, end + 2 ); + } } return sz; }