@@ -6,7 +6,7 @@ package context
66
77import (
88 "context"
9- "html "
9+ "fmt "
1010 "html/template"
1111 "io"
1212 "net/http"
@@ -71,16 +71,6 @@ func init() {
7171 })
7272}
7373
74- // TrHTMLEscapeArgs runs ".Locale.Tr()" but pre-escapes all arguments with html.EscapeString.
75- // This is useful if the locale message is intended to only produce HTML content.
76- func (ctx * Context ) TrHTMLEscapeArgs (msg string , args ... string ) string {
77- trArgs := make ([]any , len (args ))
78- for i , arg := range args {
79- trArgs [i ] = html .EscapeString (arg )
80- }
81- return ctx .Locale .Tr (msg , trArgs ... )
82- }
83-
8474type webContextKeyType struct {}
8575
8676var WebContextKey = webContextKeyType {}
@@ -253,6 +243,12 @@ func (ctx *Context) JSONOK() {
253243 ctx .JSON (http .StatusOK , map [string ]any {"ok" : true }) // this is only a dummy response, frontend seldom uses it
254244}
255245
256- func (ctx * Context ) JSONError (msg string ) {
257- ctx .JSON (http .StatusBadRequest , map [string ]any {"errorMessage" : msg })
246+ func (ctx * Context ) JSONError (msg any ) {
247+ switch v := msg .(type ) {
248+ case string :
249+ ctx .JSON (http .StatusBadRequest , map [string ]any {"errorMessage" : v , "renderFormat" : "text" })
250+ case template.HTML :
251+ ctx .JSON (http .StatusBadRequest , map [string ]any {"errorMessage" : v , "renderFormat" : "html" })
252+ }
253+ panic (fmt .Sprintf ("unsupported type: %T" , msg ))
258254}
0 commit comments