From 22ccdd74c2a74e2955d2842e96642e78c05fd184 Mon Sep 17 00:00:00 2001 From: Jeffrey Morgan Date: Thu, 18 Sep 2025 19:40:31 -0300 Subject: [PATCH] server: add unauthorized error to remote chat handler (#12338) --- server/routes.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/server/routes.go b/server/routes.go index c020453181..a08a728987 100644 --- a/server/routes.go +++ b/server/routes.go @@ -259,7 +259,10 @@ func (s *Server) GenerateHandler(c *gin.Context) { c.JSON(http.StatusUnauthorized, gin.H{"error": "error getting public key"}) return } - c.JSON(http.StatusUnauthorized, gin.H{"public_key": pk}) + c.JSON(http.StatusUnauthorized, gin.H{ + "error": "unauthorized", + "public_key": pk, + }) return } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) @@ -1810,6 +1813,20 @@ func (s *Server) ChatHandler(c *gin.Context) { client := api.NewClient(remoteURL, http.DefaultClient) err = client.Chat(c, &req, fn) if err != nil { + var sErr api.AuthorizationError + if errors.As(err, &sErr) && sErr.StatusCode == http.StatusUnauthorized { + pk, pkErr := auth.GetPublicKey() + if pkErr != nil { + slog.Error("couldn't get public key", "error", pkErr) + c.JSON(http.StatusUnauthorized, gin.H{"error": "error getting public key"}) + return + } + c.JSON(http.StatusUnauthorized, gin.H{ + "error": "unauthorized", + "public_key": pk, + }) + return + } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return }