一个return处理的问题 #35

Closed
opened 2016-08-26 06:24:11 +00:00 by lunny · 1 comment
Owner

Author: @qjebbs

在文件return.go中有这样一段:

var result = ctx.Result
if res, ok := ctx.Result.(*StatusResult); ok {
    ctx.WriteHeader(res.Code) //这里写了一次状态码
    result = res.Result
}

...

switch res := result.(type) {
case AbortError, error:
    ctx.HandleError()
case []byte:
    ctx.WriteHeader(http.StatusOK)
    ctx.Write(res)
case string:
    ctx.WriteHeader(http.StatusOK) //这里写了第二次状态码
    ctx.Write([]byte(res))
}

如果有个action 是这样写的:

func (a *Action) Get() (int, interface{}){
    return 201,"Created"
}

返回的却是200,而终端里会提示http: multiple response.WriteHeader calls

Author: @qjebbs 在文件`return.go`中有这样一段: ``` var result = ctx.Result if res, ok := ctx.Result.(*StatusResult); ok { ctx.WriteHeader(res.Code) //这里写了一次状态码 result = res.Result } ... switch res := result.(type) { case AbortError, error: ctx.HandleError() case []byte: ctx.WriteHeader(http.StatusOK) ctx.Write(res) case string: ctx.WriteHeader(http.StatusOK) //这里写了第二次状态码 ctx.Write([]byte(res)) } ``` 如果有个action 是这样写的: ``` func (a *Action) Get() (int, interface{}){ return 201,"Created" } ``` 返回的却是`200`,而终端里会提示`http: multiple response.WriteHeader calls`。
Author
Owner

Author: @qjebbs

上一段描述的是没有嵌入tango.Ctx的情况,如果嵌入了tango.Ctx,在action里return err(没有状态码),返回的是200 OK。这好像也不合理

Author: @qjebbs 上一段描述的是没有嵌入`tango.Ctx`的情况,如果嵌入了`tango.Ctx`,在action里`return err`(没有状态码),返回的是`200 OK`。这好像也不合理
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: lunny/tango#35
No description provided.