Skip to content

Commit

Permalink
add: model-provider errors
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 committed Dec 23, 2024
1 parent a140cbd commit b479edc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
20 changes: 18 additions & 2 deletions pkg/controller/handlers/toolreference/toolreference.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package toolreference
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"net/url"
"os"
"path"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -385,8 +387,22 @@ func (h *Handler) BackPopulateModels(req router.Request, _ router.Response) erro
if err != nil {
// Don't error and retry because it will likely fail again. Log the error, and the user can re-sync manually.
// Also, the toolRef.Status.Error field will bubble up to the user in the UI.
toolRef.Status.Error = err.Error()
log.Errorf(err.Error())

// Check if the model provider returned a properly formatted error message and set it as status
re := regexp.MustCompile(`\{.*"error":.*}`)
match := re.FindString(err.Error())
if match != "" {
toolRef.Status.Error = match
type errorResponse struct {
Error string `json:"error"`
}
var eR errorResponse
if err := json.Unmarshal([]byte(match), &eR); err == nil {
toolRef.Status.Error = eR.Error
}
}

log.Errorf("%v", err)
return nil
}

Expand Down
10 changes: 9 additions & 1 deletion ui/admin/app/components/model-providers/ModelProviderForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,15 @@ export function ModelProviderForm({
<AlertDescription>
Your configuration was saved, but we were not able
to connect to the model provider. Please check your
configuration and try again.
configuration and try again:{" "}
<strong>
{(typeof fetchAvailableModels.error ===
"object" &&
"message" in fetchAvailableModels.error &&
(fetchAvailableModels.error
.message as string)) ??
"Unknown error"}
</strong>
</AlertDescription>
</Alert>
</div>
Expand Down
14 changes: 14 additions & 0 deletions ui/admin/app/hooks/useAsync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ export function useAsync<TData, TParams extends unknown[]>(
onSuccess?.(data, params);
})
.catch((error) => {
if (
error.response &&
typeof error.response.data === "string"
) {
const errorMessageMatch =
error.response.data.match(/{"error":"(.*?)"}/);
if (errorMessageMatch) {
const errorMessage = JSON.parse(
errorMessageMatch[0]
).error;
console.log("Error: ", errorMessage);
error.message = errorMessage;
}
}
setError(error);
onError?.(error, params);
})
Expand Down

0 comments on commit b479edc

Please sign in to comment.