fix: process error content type
This commit is contained in:
14
main.go
14
main.go
@@ -105,12 +105,14 @@ func main() {
|
|||||||
Authorization: c.Request.Header.Get("Authorization"),
|
Authorization: c.Request.Header.Get("Authorization"),
|
||||||
UserAgent: c.Request.Header.Get("User-Agent"),
|
UserAgent: c.Request.Header.Get("User-Agent"),
|
||||||
}
|
}
|
||||||
defer func() {
|
/*
|
||||||
if err := recover(); err != nil {
|
defer func() {
|
||||||
log.Println("Error:", err)
|
if err := recover(); err != nil {
|
||||||
c.AbortWithError(500, fmt.Errorf("%s", err))
|
log.Println("Error:", err)
|
||||||
}
|
c.AbortWithError(500, fmt.Errorf("%s", err))
|
||||||
}()
|
}
|
||||||
|
}()
|
||||||
|
*/
|
||||||
|
|
||||||
// check authorization header
|
// check authorization header
|
||||||
if !*noauth {
|
if !*noauth {
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
|
|||||||
// reverse proxy
|
// reverse proxy
|
||||||
remote, err := url.Parse(upstream.Endpoint)
|
remote, err := url.Parse(upstream.Endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(500, errors.New("can't parse reverse proxy remote URL"))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,6 +37,7 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
|
|||||||
proxy.Director = nil
|
proxy.Director = nil
|
||||||
var inBody []byte
|
var inBody []byte
|
||||||
proxy.Rewrite = func(proxyRequest *httputil.ProxyRequest) {
|
proxy.Rewrite = func(proxyRequest *httputil.ProxyRequest) {
|
||||||
|
|
||||||
in := proxyRequest.In
|
in := proxyRequest.In
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
@@ -48,7 +48,7 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
|
|||||||
// read request body
|
// read request body
|
||||||
inBody, err = io.ReadAll(in.Body)
|
inBody, err = io.ReadAll(in.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(502, errors.New("reverse proxy middleware failed to read request body "+err.Error()))
|
errCtx = errors.New("reverse proxy middleware failed to read request body " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,6 +80,7 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
|
|||||||
log.Println("Timeout upstream", upstream.Endpoint)
|
log.Println("Timeout upstream", upstream.Endpoint)
|
||||||
errCtx = errors.New("timeout")
|
errCtx = errors.New("timeout")
|
||||||
if shouldResponse {
|
if shouldResponse {
|
||||||
|
c.Header("Content-Type", "application/json")
|
||||||
c.AbortWithError(502, errCtx)
|
c.AbortWithError(502, errCtx)
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
@@ -138,6 +139,7 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
|
|||||||
|
|
||||||
// abort to error handle
|
// abort to error handle
|
||||||
if shouldResponse {
|
if shouldResponse {
|
||||||
|
c.Header("Content-Type", "application/json")
|
||||||
c.AbortWithError(502, err)
|
c.AbortWithError(502, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user