Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging middleware: res.status_code is empty #305

Closed
wonktondI opened this issue Jun 30, 2023 · 9 comments
Closed

logging middleware: res.status_code is empty #305

wonktondI opened this issue Jun 30, 2023 · 9 comments

Comments

@wonktondI
Copy link

image

日志中间件中,res.status_code 是 None。不能正常返回 code

@chrislearn
Copy link
Member

默认情况下 response 确实 status_code = None;
如果你设置过 status_code, 或者有 res.render(StatusError) 则这里的 status_code 应该就不是 None 了.

@infdahai
Copy link

infdahai commented Jul 1, 2023

这里要返回个默认值吗?

@chrislearn
Copy link
Member

默认值就是none.没啥意义,所以大概做了个判断。

@wonktondI
Copy link
Author

image
Uploading image.png…
在 handler 返回时,res.status_code(StatusCode::CREATED) (or StatusCode::OK );,可以在中间件中获取到 status_code了。
在返回成功时,能默认给一个 res.status_code(StatusCode::OK) 吗?

@chrislearn
Copy link
Member

做了一点修改, 其实现在的 None 就当作 StatusCode::OK. 不过我仍然想明确区分用户有没有显式设置状态码. 所以, 这里的 status_code 类型是 Option<StatusCode>.

@infdahai
Copy link

infdahai commented Jul 1, 2023

嗯。我觉得这样会好点。

@wonktondI
Copy link
Author

在 handler 里, res.render() 这个方法里,返回成功时,设置一下状态码,这样可行吗?

@chrislearn
Copy link
Member

状态码是会设置的,只是是在所有的handler执行完毕。而log中间件也是handler 所以设置晚于这个handler的执行。

@wonktondI
Copy link
Author

明白了🌹

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants