Skip to content

Commit 9517b51

Browse files
committed
feat: ai-proxy support custom error handler by cover DefaultErrorHandler
1 parent d609497 commit 9517b51

File tree

6 files changed

+25
-19
lines changed

6 files changed

+25
-19
lines changed

plugins/wasm-go/extensions/ai-proxy/main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func onHttpRequestHeader(ctx wrapper.HttpContext, pluginConfig config.PluginConf
114114
return action
115115
}
116116

117-
_ = util.SendResponse(500, "ai-proxy.proc_req_headers_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to process request headers: %v", err))
117+
util.ErrorHandler("ai-proxy.proc_req_headers_failed", fmt.Errorf("failed to process request headers: %v", err))
118118
return types.ActionContinue
119119
}
120120

@@ -136,7 +136,10 @@ func onHttpRequestBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfig
136136

137137
newBody, settingErr := pluginConfig.GetProviderConfig().ReplaceByCustomSettings(body)
138138
if settingErr != nil {
139-
_ = util.SendResponse(500, "ai-proxy.proc_req_body_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to rewrite request body by custom settings: %v", settingErr))
139+
util.ErrorHandler(
140+
"ai-proxy.proc_req_body_failed",
141+
fmt.Errorf("failed to replace request body by custom settings: %v", settingErr),
142+
)
140143
return types.ActionContinue
141144
}
142145

@@ -146,7 +149,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfig
146149
if err == nil {
147150
return action
148151
}
149-
_ = util.SendResponse(500, "ai-proxy.proc_req_body_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to process request body: %v", err))
152+
util.ErrorHandler("ai-proxy.proc_req_body_failed", fmt.Errorf("failed to process request body: %v", err))
150153
}
151154
return types.ActionContinue
152155
}
@@ -193,7 +196,7 @@ func onHttpResponseHeaders(ctx wrapper.HttpContext, pluginConfig config.PluginCo
193196
checkStream(&ctx, log)
194197
return action
195198
}
196-
_ = util.SendResponse(500, "ai-proxy.proc_resp_headers_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to process response headers: %v", err))
199+
util.ErrorHandler("ai-proxy.proc_resp_headers_failed", fmt.Errorf("failed to process response headers: %v", err))
197200
return types.ActionContinue
198201
}
199202

@@ -248,7 +251,7 @@ func onHttpResponseBody(ctx wrapper.HttpContext, pluginConfig config.PluginConfi
248251
if err == nil {
249252
return action
250253
}
251-
_ = util.SendResponse(500, "ai-proxy.proc_resp_body_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to process response body: %v", err))
254+
util.ErrorHandler("ai-proxy.proc_resp_body_failed", fmt.Errorf("failed to process response body: %v", err))
252255
return types.ActionContinue
253256
}
254257
return types.ActionContinue

plugins/wasm-go/extensions/ai-proxy/provider/context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func insertContext(provider Provider, content string, err error, body []byte, lo
139139
typ := provider.GetProviderType()
140140
if err != nil {
141141
log.Errorf("failed to load context file: %v", err)
142-
_ = util.SendResponse(500, fmt.Sprintf("ai-proxy.%s.load_ctx_failed", typ), util.MimeTypeTextPlain, fmt.Sprintf("failed to load context file: %v", err))
142+
util.ErrorHandler(fmt.Sprintf("ai-proxy.%s.load_ctx_failed", typ), fmt.Errorf("failed to load context file: %v", err))
143143
}
144144

145145
if inserter, ok := provider.(ContextInserter); ok {
@@ -149,10 +149,10 @@ func insertContext(provider Provider, content string, err error, body []byte, lo
149149
}
150150

151151
if err != nil {
152-
_ = util.SendResponse(500, fmt.Sprintf("ai-proxy.%s.insert_ctx_failed", typ), util.MimeTypeTextPlain, fmt.Sprintf("failed to insert context message: %v", err))
152+
util.ErrorHandler(fmt.Sprintf("ai-proxy.%s.insert_ctx_failed", typ), fmt.Errorf("failed to insert context message: %v", err))
153153
}
154154
if err := replaceHttpJsonRequestBody(body, log); err != nil {
155-
_ = util.SendResponse(500, fmt.Sprintf("ai-proxy.%s.replace_request_body_failed", typ), util.MimeTypeTextPlain, fmt.Sprintf("failed to replace request body: %v", err))
155+
util.ErrorHandler(fmt.Sprintf("ai-proxy.%s.replace_request_body_failed", typ), fmt.Errorf("failed to replace request body: %v", err))
156156
}
157157
}
158158

plugins/wasm-go/extensions/ai-proxy/provider/hunyuan.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func (m *hunyuanProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName
172172

173173
if err != nil {
174174
log.Errorf("failed to load context file: %v", err)
175-
_ = util.SendResponse(500, "ai-proxy.hunyuan.load_ctx_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to load context file: %v", err))
175+
util.ErrorHandler("ai-proxy.hunyuan.load_ctx_failed", fmt.Errorf("failed to load context file: %v", err))
176176
}
177177
m.insertContextMessageIntoHunyuanRequest(request, content)
178178

@@ -182,7 +182,7 @@ func (m *hunyuanProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName
182182
_ = util.OverwriteRequestAuthorization(authorizedValueNew)
183183

184184
if err := replaceJsonRequestBody(request, log); err != nil {
185-
_ = util.SendResponse(500, "ai-proxy.hunyuan.insert_ctx_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to replace request body: %v", err))
185+
util.ErrorHandler("ai-proxy.hunyuan.insert_ctx_failed", fmt.Errorf("failed to replace request body: %v", err))
186186
}
187187
}, log)
188188
if err == nil {
@@ -244,7 +244,7 @@ func (m *hunyuanProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName
244244
}()
245245
if err != nil {
246246
log.Errorf("failed to load context file: %v", err)
247-
_ = util.SendResponse(500, "ai-proxy.hunyuan.load_ctx_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to load context file: %v", err))
247+
util.ErrorHandler("ai-proxy.hunyuan.load_ctx_failed", fmt.Errorf("failed to load context file: %v", err))
248248
return
249249
}
250250
insertContextMessage(request, content)
@@ -256,7 +256,7 @@ func (m *hunyuanProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName
256256
_ = util.OverwriteRequestAuthorization(authorizedValueNew)
257257

258258
if err := replaceJsonRequestBody(hunyuanRequest, log); err != nil {
259-
_ = util.SendResponse(500, "ai-proxy.hunyuan.insert_ctx_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to replace request body: %v", err))
259+
util.ErrorHandler("ai-proxy.hunyuan.insert_ctx_failed", fmt.Errorf("failed to replace request body: %v", err))
260260
}
261261
}, log)
262262
if err == nil {

plugins/wasm-go/extensions/ai-proxy/provider/minimax.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ func (m *minimaxProvider) handleRequestBodyByChatCompletionPro(body []byte, log
141141
}()
142142
if err != nil {
143143
log.Errorf("failed to load context file: %v", err)
144-
_ = util.SendResponse(500, "ai-proxy.minimax.load_ctx_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to load context file: %v", err))
144+
util.ErrorHandler("ai-proxy.minimax.load_ctx_failed", fmt.Errorf("failed to load context file: %v", err))
145145
}
146146
// 由于 minimaxChatCompletionV2(格式和 OpenAI 一致)和 minimaxChatCompletionPro(格式和 OpenAI 不一致)中 insertHttpContextMessage 的逻辑不同,无法做到同一个 provider 统一
147147
// 因此对于 minimaxChatCompletionPro 需要手动处理 context 消息
148148
// minimaxChatCompletionV2 交给默认的 defaultInsertHttpContextMessage 方法插入 context 消息
149149
minimaxRequest := m.buildMinimaxChatCompletionV2Request(request, content)
150150
if err := replaceJsonRequestBody(minimaxRequest, log); err != nil {
151-
_ = util.SendResponse(500, "ai-proxy.minimax.insert_ctx_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to replace Request body: %v", err))
151+
util.ErrorHandler("ai-proxy.minimax.insert_ctx_failed", fmt.Errorf("failed to replace Request body: %v", err))
152152
}
153153
}, log)
154154
if err == nil {

plugins/wasm-go/extensions/ai-proxy/provider/moonshot.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package provider
33
import (
44
"errors"
55
"fmt"
6+
"net/http"
7+
68
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-proxy/util"
79
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
810
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
911
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
1012
"github.com/tidwall/gjson"
11-
"net/http"
1213
)
1314

1415
// moonshotProvider is the provider for Moonshot AI service.
@@ -91,12 +92,12 @@ func (m *moonshotProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiNam
9192
}()
9293
if err != nil {
9394
log.Errorf("failed to load context file: %v", err)
94-
_ = util.SendResponse(500, "ai-proxy.moonshot.load_ctx_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to load context file: %v", err))
95+
util.ErrorHandler("ai-proxy.moonshot.load_ctx_failed", fmt.Errorf("failed to load context file: %v", err))
9596
return
9697
}
9798
err = m.performChatCompletion(ctx, content, request, log)
9899
if err != nil {
99-
_ = util.SendResponse(500, "ai-proxy.moonshot.insert_ctx_failed", util.MimeTypeTextPlain, fmt.Sprintf("failed to perform chat completion: %v", err))
100+
util.ErrorHandler("ai-proxy.moonshot.insert_ctx_failed", fmt.Errorf("failed to perform chat completion: %v", err))
100101
}
101102
}, log)
102103
if err == nil {

plugins/wasm-go/extensions/ai-proxy/util/http.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ const (
1313
MimeTypeApplicationJson = "application/json"
1414
)
1515

16-
func SendResponse(statusCode uint32, statusCodeDetails string, contentType, body string) error {
17-
return proxywasm.SendHttpResponseWithDetail(statusCode, statusCodeDetails, CreateHeaders(HeaderContentType, contentType), []byte(body), -1)
16+
type ErrorHandlerFunc func(statusCodeDetails string, err error) error
17+
18+
var ErrorHandler ErrorHandlerFunc = func(statusCodeDetails string, err error) error {
19+
return proxywasm.SendHttpResponseWithDetail(500, statusCodeDetails, CreateHeaders(HeaderContentType, MimeTypeTextPlain), []byte(err.Error()), -1)
1820
}
1921

2022
func CreateHeaders(kvs ...string) [][2]string {

0 commit comments

Comments
 (0)