Skip to content

Commit

Permalink
ERROR_MSG
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Mar 28, 2024
1 parent 9c93574 commit 0c37af4
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions kong/plugins/ai-proxy/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ local ai_shared = require("kong.llm.drivers.shared")
local llm = require("kong.llm")
local cjson = require("cjson.safe")
local kong_utils = require("kong.tools.gzip")
local kong_meta = require "kong.meta"
local kong_meta = require("kong.meta")
--


_M.PRIORITY = 770
_M.VERSION = kong_meta.version


-- reuse this table for error message response
local ERROR_MSG = { error = { message = "" } }


local function bad_request(msg)
kong.log.warn(msg)
return kong.response.exit(400, { error = { message = msg } })
ERROR_MSG.error.message = msg

return kong.response.exit(400, ERROR_MSG)
end


local function internal_server_error(msg)
kong.log.err(msg)
return kong.response.exit(500, { error = { message = msg } })
ERROR_MSG.error.message = msg

return kong.response.exit(500, ERROR_MSG)
end


Expand Down Expand Up @@ -60,13 +68,9 @@ function _M:header_filter(conf)
kong.ctx.plugin.ai_parser_error = true

ngx.status = 500
local message = {
error = {
message = err,
},
}
ERROR_MSG.error.message = err

kong.ctx.plugin.parsed_response = cjson.encode(message)
kong.ctx.plugin.parsed_response = cjson.encode(ERROR_MSG)

elseif new_response_string then
-- preserve the same response content type; assume the from_format function
Expand Down Expand Up @@ -137,7 +141,7 @@ function _M:access(conf)
end

-- check the incoming format is the same as the configured LLM format
local compatible, err = llm.is_compatible(request_table, conf.route_type)
local compatible, err = llm.is_compatible(request_table, route_type)
if not compatible then
kong.ctx.shared.skip_response_transformer = true
return bad_request(err)
Expand Down

0 comments on commit 0c37af4

Please sign in to comment.