go-macaron / binding

Package binding is a middleware that provides request data binding and validation for Macaron.
Apache License 2.0
23 stars 17 forks source link

修改ErrorHandler 的方法签名,可以接受指针类型 #6

Closed cgyy closed 8 years ago

cgyy commented 8 years ago

比如,我想在错误处理中实现这样的逻辑: flash.Error(err.Error()) 目前的方法不能实现

ErrorHandler interface { // Error handles validation errors with custom process. Error(*macaron.Context, Errors) }

可否修改为 Error(...interface{})

unknwon commented 8 years ago

ErrorHandler 是用来直接响应 error 的,不是用来返回 error 类型的,否则就不叫 Handler 了。

cgyy commented 8 years ago

你好,没写明白,我的意思是这样: 想在自定义的错误中这样处理: 在flash中显示错误消息,并调转到其他页面:

func (cf ContactForm) Error(ctx *macaron.Context, errs binding.Errors) {
   flash.Error("表单填写不完整") // 目前的方法签名不能实现这样的功能
   ctx.Redirect("/")
}

是否方法签名可以是这样

       //binding.go 第641行
    ErrorHandler interface {
        // Error handles validation errors with custom process.
        Error(...interface{})
    }

```go

这样一来,自定义的错误处理就可以使用其他参数了
unknwon commented 8 years ago

你可以使用以下代码手工获得注入的服务:

flash := m.GetVal(reflect.TypeOf(&session.Flash{})).Interface().(*session.Flash)
cgyy commented 8 years ago

你好,这样可以了

flash := ctx.GetVal(reflect.TypeOf(&session.Flash{})).Interface().(*session.Flash)

谢谢!

unknwon commented 8 years ago

好的~