Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit c434ac2

Browse files
fix: improve error message when load model failed (#1533)
* fix: error for models start * fix: add unit tests --------- Co-authored-by: vansangpfiev <[email protected]>
1 parent 0fd3c62 commit c434ac2

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

engine/cli/commands/model_start_cmd.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "run_cmd.h"
77
#include "server_start_cmd.h"
88
#include "utils/cli_selection_utils.h"
9+
#include "utils/json_helper.h"
910
#include "utils/logging_utils.h"
1011

1112
namespace commands {
@@ -14,7 +15,7 @@ bool ModelStartCmd::Exec(const std::string& host, int port,
1415
std::optional<std::string> model_id =
1516
SelectLocalModel(model_service_, model_handle);
1617

17-
if(!model_id.has_value()) {
18+
if (!model_id.has_value()) {
1819
return false;
1920
}
2021

@@ -41,7 +42,8 @@ bool ModelStartCmd::Exec(const std::string& host, int port,
4142
<< *model_id << "` for interactive chat shell");
4243
return true;
4344
} else {
44-
CTL_ERR("Model failed to load with status code: " << res->status);
45+
auto root = json_helper::ParseJsonString(res->body);
46+
CLI_LOG(root["message"].asString());
4547
return false;
4648
}
4749
} else {

engine/services/inference_service.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ InferResult InferenceService::LoadModel(
140140
if (IsEngineLoaded(kLlamaRepo) && ne == kTrtLlmRepo) {
141141
// Remove llamacpp dll directory
142142
if (!RemoveDllDirectory(engines_[kLlamaRepo].cookie)) {
143-
LOG_INFO << "Could not remove dll directory: " << kLlamaRepo;
143+
LOG_WARN << "Could not remove dll directory: " << kLlamaRepo;
144144
} else {
145-
LOG_WARN << "Removed dll directory: " << kLlamaRepo;
145+
LOG_INFO << "Removed dll directory: " << kLlamaRepo;
146146
}
147147

148148
add_dll(ne, abs_path);
@@ -158,7 +158,7 @@ InferResult InferenceService::LoadModel(
158158
LOG_ERROR << "Could not load engine: " << e.what();
159159
engines_.erase(ne);
160160

161-
r["message"] = "Could not load engine " + ne;
161+
r["message"] = "Could not load engine " + ne + ": " + e.what();
162162
stt["status_code"] = k500InternalServerError;
163163
return std::make_pair(stt, r);
164164
}

engine/services/model_service.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "utils/logging_utils.h"
1515
#include "utils/result.hpp"
1616
#include "utils/string_utils.h"
17+
#include "utils/json_helper.h"
1718

1819
namespace {
1920
void ParseGguf(const DownloadItem& ggufDownloadItem,
@@ -622,9 +623,9 @@ cpp::result<bool, std::string> ModelService::StartModel(
622623
CTL_INF("Model '" + model_handle + "' is already loaded");
623624
return true;
624625
} else {
626+
auto root = json_helper::ParseJsonString(res->body);
625627
CTL_ERR("Model failed to load with status code: " << res->status);
626-
return cpp::fail("Model failed to load with status code: " +
627-
std::to_string(res->status));
628+
return cpp::fail("Model failed to start: " + root["message"].asString());
628629
}
629630
} else {
630631
auto err = res.error();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <gtest/gtest.h>
2+
#include <json/json.h>
3+
#include <string>
4+
#include "utils/json_helper.h"
5+
6+
// Test Suite
7+
TEST(ParseJsonStringTest, ValidJsonObject) {
8+
std::string json_str = R"({"name": "John", "age": 30})";
9+
Json::Value result = json_helper::ParseJsonString(json_str);
10+
11+
EXPECT_EQ(result["name"].asString(), "John");
12+
EXPECT_EQ(result["age"].asInt(), 30);
13+
}
14+
15+
TEST(ParseJsonStringTest, ValidJsonArray) {
16+
std::string json_str = R"([1, 2, 3, 4, 5])";
17+
Json::Value result = json_helper::ParseJsonString(json_str);
18+
19+
EXPECT_EQ(result.size(), 5);
20+
EXPECT_EQ(result[0].asInt(), 1);
21+
}
22+
23+
TEST(ParseJsonStringTest, InvalidJson) {
24+
std::string json_str = R"({"name": "John", "age": )";
25+
Json::Value result = json_helper::ParseJsonString(json_str);
26+
27+
EXPECT_TRUE(result["age"].isNull());
28+
}
29+
30+
TEST(ParseJsonStringTest, EmptyString) {
31+
std::string json_str = "";
32+
Json::Value result = json_helper::ParseJsonString(json_str);
33+
34+
EXPECT_TRUE(result.isNull());
35+
}

engine/utils/json_helper.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include <json/json.h>
3+
#include <string>
4+
namespace json_helper {
5+
inline Json::Value ParseJsonString(const std::string& json_str) {
6+
Json::Value root;
7+
Json::Reader reader;
8+
reader.parse(json_str, root);
9+
return root;
10+
}
11+
}

0 commit comments

Comments
 (0)