From 4aac0ef42e8bc2fadc305fa25190e77e140f9b6a Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Fri, 10 Oct 2025 17:17:12 +0200 Subject: [PATCH] chore(api): return json errors (#6428) Signed-off-by: Ettore Di Giacinto --- core/http/middleware/auth.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/http/middleware/auth.go b/core/http/middleware/auth.go index 23141d4cf..ceb8b68d3 100644 --- a/core/http/middleware/auth.go +++ b/core/http/middleware/auth.go @@ -3,12 +3,14 @@ package middleware import ( "crypto/subtle" "errors" + "strings" "github.com/dave-gray101/v2keyauth" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/keyauth" "github.com/mudler/LocalAI/core/config" "github.com/mudler/LocalAI/core/http/utils" + "github.com/mudler/LocalAI/core/schema" ) // This file contains the configuration generators and handler functions that are used along with the fiber/keyauth middleware @@ -40,6 +42,19 @@ func getApiKeyErrorHandler(applicationConfig *config.ApplicationConfig) fiber.Er if applicationConfig.OpaqueErrors { return ctx.SendStatus(401) } + + // Check if the request content type is JSON + contentType := string(ctx.Context().Request.Header.ContentType()) + if strings.Contains(contentType, "application/json") { + return ctx.Status(401).JSON(schema.ErrorResponse{ + Error: &schema.APIError{ + Message: "An authentication key is required", + Code: 401, + Type: "invalid_request_error", + }, + }) + } + return ctx.Status(401).Render("views/login", fiber.Map{ "BaseURL": utils.BaseURL(ctx), })