Skip to content

Commit 580b6cb

Browse files
committed
clarify code
1 parent a578e3a commit 580b6cb

File tree

5 files changed

+23
-28
lines changed

5 files changed

+23
-28
lines changed

modules/context/api.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,15 @@ func (ctx *APIContext) HasAPIError() bool {
378378
return hasErr.(bool)
379379
}
380380

381+
// GetErrMsg returns error message in form validation.
382+
func (ctx *APIContext) GetErrMsg() string {
383+
msg, _ := ctx.Data["ErrorMsg"].(string)
384+
if msg == "" {
385+
msg = "invalid form data"
386+
}
387+
return msg
388+
}
389+
381390
// NotFoundOrServerError use error check function to determine if the error
382391
// is about not found. It responds with 404 status code for not found error,
383392
// or error context description for logging purpose of 500 server error.

modules/context/base.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ type Base struct {
3434

3535
Resp ResponseWriter
3636
Req *http.Request
37+
Data middleware.ContextData // data used by MVC templates, it's also used to pass data between middlewares/handler
3738

3839
Locale translation.Locale
39-
40-
Data middleware.ContextData // data used by MVC templates
4140
}
4241

4342
func (b *Base) Deadline() (deadline time.Time, ok bool) {
@@ -75,15 +74,6 @@ func (b *Base) GetData() middleware.ContextData {
7574
return b.Data
7675
}
7776

78-
func (b *Base) GetErrMsg() string {
79-
return b.Data["ErrorMsg"].(string)
80-
}
81-
82-
func (b *Base) HasValue(name string) bool {
83-
_, ok := b.Data[name]
84-
return ok
85-
}
86-
8777
// AppendAccessControlExposeHeaders append headers by name to "Access-Control-Expose-Headers" header
8878
func (b *Base) AppendAccessControlExposeHeaders(names ...string) {
8979
val := b.RespHeader().Get("Access-Control-Expose-Headers")

modules/context/context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,12 @@ func (ctx *Context) HasError() bool {
190190
ctx.Data["Flash"] = ctx.Flash
191191
return hasErr.(bool)
192192
}
193+
194+
// GetErrMsg returns error message in form validation.
195+
func (ctx *Context) GetErrMsg() string {
196+
msg, _ := ctx.Data["ErrorMsg"].(string)
197+
if msg == "" {
198+
msg = "invalid form data"
199+
}
200+
return msg
201+
}

modules/context/response.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import (
1010
// ResponseWriter represents a response writer for HTTP
1111
type ResponseWriter interface {
1212
http.ResponseWriter
13-
Flush()
13+
http.Flusher
1414
Status() int
1515
Before(func(ResponseWriter))
16-
Size() int
1716
}
1817

1918
var _ ResponseWriter = &Response{}
@@ -27,11 +26,6 @@ type Response struct {
2726
beforeExecuted bool
2827
}
2928

30-
// Size return written size
31-
func (r *Response) Size() int {
32-
return r.written
33-
}
34-
3529
// Write writes bytes to HTTP endpoint
3630
func (r *Response) Write(bs []byte) (int, error) {
3731
if !r.beforeExecuted {
@@ -65,7 +59,7 @@ func (r *Response) WriteHeader(statusCode int) {
6559
}
6660
}
6761

68-
// Flush flush cached data
62+
// Flush flushes cached data
6963
func (r *Response) Flush() {
7064
if f, ok := r.ResponseWriter.(http.Flusher); ok {
7165
f.Flush()

routers/install/install.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,8 @@ func SubmitInstall(ctx *context.Context) {
247247
ctx.Data["CurDbType"] = form.DbType
248248

249249
if ctx.HasError() {
250-
if ctx.HasValue("Err_SMTPUser") {
251-
ctx.Data["Err_SMTP"] = true
252-
}
253-
if ctx.HasValue("Err_AdminName") ||
254-
ctx.HasValue("Err_AdminPasswd") ||
255-
ctx.HasValue("Err_AdminEmail") {
256-
ctx.Data["Err_Admin"] = true
257-
}
258-
250+
ctx.Data["Err_SMTP"] = ctx.Data["Err_SMTPUser"] != nil
251+
ctx.Data["Err_Admin"] = ctx.Data["Err_AdminName"] != nil || ctx.Data["Err_AdminPasswd"] != nil || ctx.Data["Err_AdminEmail"] != nil
259252
ctx.HTML(http.StatusOK, tplInstall)
260253
return
261254
}

0 commit comments

Comments
 (0)