savsgio/atreugo

In viewFn, if setStatusCode must run afterfilters

imxxiv opened this issue · 1 comments

In viewFn
if setStatusCode in response func must run afterfilters
if return err, statusCode must be set fasthttp.StatusInternalServerError

example: in login handler, after check username, if error, i hope return 401 and not run afterfilers

  1. use response to retrun, will run afterfilers
    return ctx.TextResponse("Unauthorized", 401)
  2. return err, but statucode is fasthttp.StatusInternalServerError
    return errors.New("Unauthorized")

My suggestion can modify the following function

if statusCode, err = execMiddlewares(actx, before); err == nil {
	if err = viewFn(actx); err != nil {
		statusCode = fasthttp.StatusInternalServerError
	} else {
		statusCode, err = execMiddlewares(actx, after)
	}
}

to

if statusCode, err = execMiddlewares(actx, before); err == nil {
	if err = viewFn(actx); err != nil {
	    if actx.Response.StatusCode != 0 {
			statusCode = actx.Response.StatusCode
		}
		statusCode = fasthttp.StatusInternalServerError
	} else {
		statusCode, err = execMiddlewares(actx, after)
	}
}

I can

ctx.SetStatusCode(401)
return errors.New("Unauthorized")

Implement direct return error code, but do not run afterfilters

I've just added it.

Thanks. 👍