Beego: why filter can not be terminal a request and then write formatted response ?

Created on 9 Mar 2016  路  5Comments  路  Source: astaxie/beego

In beego, filter have a parameter called ctx. ctx has method Abort , but it is different to Abort of controller. I must format my error info to json, but ctx.Abort() not call my customize error method.

All 5 comments

@Jamlee
there is a way to do this with ErrorController

add a line in your routers/router.go
beego.ErrorController(&controllers.ErrorController{})
create the file controllers/errors.go and type in the code

package controllers

import "github.com/astaxie/beego"

type ErrorController struct {
    beego.Controller
}

func (this *ErrorController) Error404() {
    this.Data["json"] = map[string]interface{}{"errorCode": 404, "errorStr": "NOT FOUND"}
    this.ServeJSON()
}

if you call Abort("404") you will get the json output

@JessonChan ctx's Abort is not controller's Abort method , it must provide a error code and error message. I

In fact,it's the same

Hi @JessonChan. I'm trying to do what you mention but. I'm always receiving the following error.

* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server`

The debug output shows this line

2016/07/07 17:28:54 http: panic serving 127.0.0.1:51609: can't find templatefile in the path:errorcontroller/error404.tpl

How can I fix it?

Thanks

@Jamlee have you found a way to do catch ctx.Abort?

Was this page helpful?
0 / 5 - 0 ratings