Skip to content

Commit 9b76083

Browse files
authored
fix bug in error message (#11)
1 parent ee15252 commit 9b76083

File tree

8 files changed

+60
-14
lines changed

8 files changed

+60
-14
lines changed

.lintr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
linters: linters_with_defaults(line_length_linter = line_length_linter(100))

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug Active R Script",
6+
"type": "R-Debugger",
7+
"request": "launch",
8+
"args": [],
9+
"cwd": "${workspaceFolder}",
10+
"env": {},
11+
"console": "integratedTerminal"
12+
},
13+
]
14+
}

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: llmModule
22
Type: Package
33
Title: R Interface for Large Language Model APIs
4-
Version: 25.06.5
4+
Version: 25.11.0
55
Authors@R: c(
66
person("Ricardo", "Fernandes", email = "ldv1452@gmail.com", role = c("aut", "cre")),
77
person("Antonia", "Runge", email = "antonia.runge@inwt-statistics.de", role = c("aut"))

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# llmModule 25.11.0
2+
3+
## Bug Fixes
4+
- Fixed an issue with the error message in the `LocalLlmApi` class which would have thrown an error itself.
5+
16
# llmModule 25.06.5
27

38
## Updates

R/01-LocalLlmApi-class.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ new_LocalLlmApi <- function(
2222

2323
if (!is_ollama_running(url = base_url)) {
2424
api <- list()
25-
attr(api, "error") <- sprintf("Ollama server does not appear to be running at the specified base URL: '%s%'.", base_url)
25+
attr(api, "error") <- sprintf("Ollama server does not appear to be running at the specified base URL: '%s'.", base_url)
2626
return(api)
2727
}
2828

R/01-RemoteLlmApi-class.R

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ new_RemoteLlmApi <- function(api_key_path, provider, no_internet = NULL, exclude
9696

9797
if (inherits(is_valid, "error")) {
9898
# set error message
99-
err_msg <- is_valid$message |> clean_error_message()
99+
err_msg <- is_valid$message |> clean_error_message(
100+
replace_text = c("HTTP 401 Unauthorized" = "Unauthorized: API key is invalid or expired")
101+
)
100102

101103
# test the other provider
102104
other_provider <- ifelse(provider == "OpenAI", "DeepSeek", "OpenAI")
@@ -238,8 +240,10 @@ try_send_request <- function(request) {
238240
response <- tryCatch(
239241
req_perform(request),
240242
error = function(e) {
241-
return(structure(list(), error = paste("API request failed:",
242-
clean_error_message(e$message))))
243+
msg <- e$message |> clean_error_message(
244+
replace_text = c("HTTP 401 Unauthorized" = "Unauthorized: API key is invalid or expired")
245+
)
246+
return(structure(list(), error = paste("API request failed:", msg)))
243247
}
244248
)
245249

@@ -259,9 +263,10 @@ try_send_request <- function(request) {
259263
if (!is.null(response$status_code) && response$status_code != 200) {
260264
warning(sprintf("API returned HTTP %s: %s", response$status_code,
261265
parsed$error$message %||% "Unknown error"))
262-
attr(parsed, "error") <- clean_error_message(
263-
parsed$error$message %||% paste("HTTP", response$status_code)
264-
)
266+
attr(parsed, "error") <- parsed$error$message %||% paste("HTTP", response$status_code) |>
267+
clean_error_message(
268+
replace_text = c("HTTP 401 Unauthorized" = "Unauthorized: API key is invalid or expired")
269+
)
265270
}
266271

267272
return(parsed)
@@ -284,12 +289,16 @@ validate_api_key <- function(api_key, url_models) {
284289
}
285290

286291

287-
clean_error_message <- function(msg) {
292+
clean_error_message <- function(msg, replace_text = c()) {
288293
# Remove ANSI escape sequences
289294
msg <- gsub("\033\\[[0-9;]*m", "", msg)
290295

291-
# Replace known HTTP codes with friendly text
292-
msg <- gsub("HTTP 401 Unauthorized", "Unauthorized: API key is invalid or expired", msg)
296+
# Replace specified patterns
297+
if (length(replace_text) > 0) {
298+
for (pattern in names(replace_text)) {
299+
msg <- gsub(pattern, replace_text[[pattern]], msg)
300+
}
301+
}
293302

294303
return(trimws(msg))
295304
}

inst/app/app.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
library(llmModule)
22

33

4-
ui <- fluidPage(
4+
ui <- shiny::fluidPage(
55
shinyjs::useShinyjs(),
6-
titlePanel("LLM Prompt Module Test"),
6+
shiny::titlePanel("LLM Prompt Module Test"),
77
llm_generate_prompt_ui("single_prompt", output_response = TRUE)
88
)
99

1010
server <- function(input, output, session) {
1111
llm_generate_prompt_server("single_prompt")
1212
}
1313

14-
shinyApp(ui, server)
14+
shiny::shinyApp(ui, server)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
test_that("clean_error_message removes ANSI and trims whitespace", {
2+
msg <- "\033[31mError: Something went wrong\033[0m "
3+
cleaned <- clean_error_message(msg)
4+
expect_equal(cleaned, "Error: Something went wrong")
5+
})
6+
7+
test_that("clean_error_message replaces patterns", {
8+
msg <- "HTTP 401 Unauthorized: Invalid key"
9+
cleaned <- clean_error_message(msg, replace_text = c("HTTP 401 Unauthorized" = "Unauthorized: API key is invalid or expired"))
10+
expect_equal(cleaned, "Unauthorized: API key is invalid or expired: Invalid key")
11+
})
12+
13+
test_that("clean_error_message handles empty replace_text", {
14+
msg <- "Some error occurred"
15+
cleaned <- clean_error_message(msg)
16+
expect_equal(cleaned, "Some error occurred")
17+
})

0 commit comments

Comments
 (0)