Files
cursor-byok/internal/backend/server/middleware.go
T
leookun a5437e60fe feat(cursor): add contributor profile link and improve UI for Cursor account card
- Integrated a tooltip with a button to open the contributor's GitHub profile in the CursorAccountCard component.
- Updated the layout of the account card for better visual organization.
- Removed routing mode options from the configuration view and adjusted related translations in multiple languages.
- Cleaned up unused routing mode logic in the app state management.
2026-08-02 22:53:57 +08:00

86 lines
1.9 KiB
Go

package server
import (
"errors"
"fmt"
"net/http"
"runtime/debug"
"strings"
legacyruntime "cursor/internal/runtime"
)
func Recover() Middleware {
return func(next HandlerFunc) HandlerFunc {
return func(ctx *Context) (err error) {
defer func() {
if recovered := recover(); recovered != nil {
err = fmt.Errorf("panic: %v\n%s", recovered, string(debug.Stack()))
}
}()
return next(ctx)
}
}
}
func ServerContext() Middleware {
return func(next HandlerFunc) HandlerFunc {
return func(ctx *Context) error {
if ctx == nil {
return fmt.Errorf("server context is nil")
}
if err := ctx.ParseUpstreamURL(); err != nil {
return err
}
return next(ctx)
}
}
}
func ErrorEncoder() Middleware {
return func(next HandlerFunc) HandlerFunc {
return func(ctx *Context) error {
if ctx != nil {
ctx.LastError = nil
}
if err := next(ctx); err != nil {
if ctx != nil {
ctx.LastError = err
}
if ctx == nil || ctx.Writer == nil {
return err
}
writeServerError(ctx.Writer, err)
return nil
}
return nil
}
}
}
func writeServerError(writer http.ResponseWriter, err error) {
if responseWriterHasWrittenHeader(writer) {
return
}
status := http.StatusBadGateway
message := "bad gateway"
switch {
case err == nil:
status = http.StatusOK
message = ""
case strings.TrimSpace(err.Error()) == "empty raw url":
status = http.StatusBadRequest
message = "invalid raw url"
case errors.Is(err, ErrInvalidBidiAppendPayload):
status = http.StatusBadRequest
message = "invalid bidi append payload"
case errors.Is(err, legacyruntime.ErrInvalidSystemSetting):
status = http.StatusInternalServerError
message = "invalid system setting"
case errors.Is(err, legacyruntime.ErrChannelNotAvailable):
status = http.StatusServiceUnavailable
message = "no available channel"
}
http.Error(writer, message, status)
}