Something went wrong. Try again.
Lightweight and easily self-hostable Git forge.
Something went wrong. Try again.
1.1 kB · 46 lines
Go
at bugfixes
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647package middleware
import ( "context" "encoding/json" "farga/lib" "net/http" "strings")
func Messages(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { message := r.URL.Query().Get(lib.MessageKey) err := r.URL.Query().Get(lib.ErrorKey) ctx := r.Context()
if message != "" { if m, ok := lib.Messages[message]; ok { ctx = context.WithValue(ctx, lib.MessageKey, m) } }
if err != "" { if e, ok := lib.Errors[err]; ok { ctx = context.WithValue(ctx, lib.ErrorKey, e) } else if err, ok := strings.CutPrefix(err, lib.ValidationErrorKey); ok { decoded := make([]byte, lib.Base64Encoder.DecodedLen(len(err)))
n, err := lib.Base64Encoder.Decode(decoded, []byte(err)) if err == nil { decoded = decoded[:n]
var unmarshalled any if err := json.Unmarshal(decoded, &unmarshalled); err != nil { http.Error(w, "Invalid marshalled JSON inside Base64 errors.", http.StatusBadRequest) return }
ctx = context.WithValue(ctx, lib.ErrorKey, unmarshalled) } } }
next.ServeHTTP(w, r.WithContext(ctx)) })}