refactor: user error system (#40308)

Introduce "FlashErrorAuto" for "XxxErrorAuto" family and simplify the
error handling logic.

* Use ErrorUnwrapForHTML for web error messages 
* Use ErrorUnwrapForUser for api error messages
* Make Flash message can take up to about 3000 bytes

Next step: completely refactor "ctx.ServerError"

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
wxiaoguangandsilverwind authored and GitHub committed 2026-11-10 16:29:36 +00:00
1 parent 518e08c2c7
commit d1d702bea9
20 files changed
+309 -194

No files matched your search

+88 -30
View File
@@ -4,24 +4,29 @@
package middleware
import (
"fmt"
"encoding/base64"
"html/template"
"net/http"
"net/url"
"strings"
"sync"
"gitea.dev/modules/reqctx"
"gitea.dev/modules/util"
)
// Flash represents a one time data transfer between two requests.
type Flash struct {
DataStore reqctx.RequestDataStore
url.Values
msgValues map[string]string
// message is in HTML format, and is sanitized when rendering (ref: RenderFlashMessage)
ErrorMsg, WarningMsg, InfoMsg, SuccessMsg string
}
func (f *Flash) set(name, msg string, current ...bool) {
if f.Values == nil {
f.Values = make(map[string][]string)
if f.msgValues == nil {
f.msgValues = map[string]string{}
}
showInCurrentPage := len(current) > 0 && current[0]
if showInCurrentPage {
@@ -29,59 +34,112 @@ func (f *Flash) set(name, msg string, current ...bool) {
f.DataStore.GetData()["Flash"] = f
} else {
// the message map will be saved into the cookie and be shown in next response (a new page response which decodes the cookie)
f.Set(name, msg)
f.msgValues[name] = msg
}
}
func flashMsgStringOrHTML(msg any) string {
switch v := msg.(type) {
case string:
return v
case template.HTML:
return string(v)
var flashMessageReplacer = sync.OnceValue(func() (ret struct {
encoder, decoder *strings.Replacer
},
) {
ret.encoder = strings.NewReplacer("\\", "\\\\", "\n", "\\n")
ret.decoder = strings.NewReplacer("\\\\", "\\", "\\n", "\n")
return ret
})
func flashMessageEncode(m map[string]string, limit int) string {
if len(m) == 0 {
return ""
}
panic(fmt.Sprintf("unknown type: %T", msg))
buf := strings.Builder{}
buf.Grow(200) // most messages are short, use a good initial size
for k, v := range m {
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(flashMessageReplacer().encoder.Replace(v))
buf.WriteByte('\n')
}
s := buf.String()
n := min(len(s), limit)
return base64.RawURLEncoding.EncodeToString(util.UnsafeStringToBytes(s[:n]))
}
func flashMessageDecode(s string) map[string]string {
if s == "" {
return nil
}
if strings.Contains(s, "=") {
// legacy format, just parse it as query string
vals, err := url.ParseQuery(s)
if err != nil {
return nil
}
m := make(map[string]string, len(vals))
for k, v := range vals {
if len(v) > 0 {
m[k] = v[0]
}
}
return m
}
data, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
return nil
}
m := make(map[string]string)
for line := range strings.SplitSeq(util.UnsafeBytesToString(data), "\n") {
if line == "" {
continue
}
k, v, _ := strings.Cut(line, "=")
if v != "" {
v = flashMessageReplacer().decoder.Replace(v)
m[k] = strings.ToValidUTF8(v, "")
}
}
return m
}
func (f *Flash) EncodeToString() string {
// modern browsers support 4096 bytes for a cookie, we use base64 for encoding, so limit to 3000
return flashMessageEncode(f.msgValues, 3000)
}
// Error sets error message
func (f *Flash) Error(msg any, current ...bool) {
f.ErrorMsg = flashMsgStringOrHTML(msg)
func (f *Flash) Error[T string | template.HTML](msg T, current ...bool) {
f.ErrorMsg = string(msg)
f.set("error", f.ErrorMsg, current...)
}
// Warning sets warning message
func (f *Flash) Warning(msg any, current ...bool) {
f.WarningMsg = flashMsgStringOrHTML(msg)
func (f *Flash) Warning[T string | template.HTML](msg T, current ...bool) {
f.WarningMsg = string(msg)
f.set("warning", f.WarningMsg, current...)
}
// Info sets info message
func (f *Flash) Info(msg any, current ...bool) {
f.InfoMsg = flashMsgStringOrHTML(msg)
func (f *Flash) Info[T string | template.HTML](msg T, current ...bool) {
f.InfoMsg = string(msg)
f.set("info", f.InfoMsg, current...)
}
// Success sets success message
func (f *Flash) Success(msg any, current ...bool) {
f.SuccessMsg = flashMsgStringOrHTML(msg)
func (f *Flash) Success[T string | template.HTML](msg T, current ...bool) {
f.SuccessMsg = string(msg)
f.set("success", f.SuccessMsg, current...)
}
func ParseCookieFlashMessage(val string) *Flash {
if vals, _ := url.ParseQuery(val); len(vals) > 0 {
return &Flash{
Values: vals,
ErrorMsg: vals.Get("error"),
SuccessMsg: vals.Get("success"),
InfoMsg: vals.Get("info"),
WarningMsg: vals.Get("warning"),
}
m := flashMessageDecode(val)
if len(m) == 0 {
return nil
}
return nil
return &Flash{ErrorMsg: m["error"], SuccessMsg: m["success"], InfoMsg: m["info"], WarningMsg: m["warning"]}
}
func GetSiteCookieFlashMessage(dataStore reqctx.RequestDataStore, req *http.Request, cookieName string) (string, *Flash) {
// Get the last flash message from cookie
lastFlashCookie := GetSiteCookie(req, cookieName)
lastFlashMsg := ParseCookieFlashMessage(lastFlashCookie)
if lastFlashMsg != nil {
+29
View File
@@ -0,0 +1,29 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package middleware
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFlashMessageEncode(t *testing.T) {
assert.Empty(t, flashMessageEncode(nil, 100))
messages := map[string]string{"error": "<div>\n<div>", "success": "🌞"}
assert.Equal(t, messages, flashMessageDecode(flashMessageEncode(messages, 100)))
assert.Equal(t, map[string]string{"error": "删除分支失败"}, flashMessageDecode(flashMessageEncode(map[string]string{"error": "删除分支\xff失败"}, 100)))
const limit = 99
encoded := flashMessageEncode(map[string]string{"error": strings.Repeat("x", 200)}, limit)
assert.Len(t, encoded, limit/3*4)
decoded := flashMessageDecode(encoded)
assert.Equal(t, map[string]string{"error": strings.Repeat("x", limit-len("error="))}, decoded)
// also support legacy format
decoded = flashMessageDecode("error=invalid%20message")
assert.Equal(t, map[string]string{"error": "invalid message"}, decoded)
}