-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make error rendering context-aware #21803
Conversation
- Currently the error handling in `modules/context/context.go` always use `ctx.HTML`(indirectly) to render the error, however it is possible that the context doesn't have any HTML render set(e.g. `APIContext`). - Let the context creator provide the error handling, currently web will still use the current functions and API will roughly do the same but using `ctx.JSON`.
Co-authored-by: zeripath <art27@cantab.net>
For a long-term target, |
Without looking more into the code, did you make sure normal users dont see the stacktrace in prod mode? |
Stacktraces were never exposed to begin with and conditions were copies (ctrl + f |
For web routers, sometimes it will return JSON, sometimes return HTML and for RSS it's XML. |
That would require a very broad refactor, which I won't contribute. If that's the consensus to fix this problem, feel free to close my PR. |
We need to decouple the Gitea's contexts first. Otherwise more things are mixed together. -> Decouple WebContext/APIContext/PrivateContext #24786 |
Replace #16455 Close #21803 Mixing different Gitea contexts together causes some problems: 1. Unable to respond proper content when error occurs, eg: Web should respond HTML while API should respond JSON 2. Unclear dependency, eg: it's unclear when Context is used in APIContext, which fields should be initialized, which methods are necessary. To make things clear, this PR introduces a Base context, it only provides basic Req/Resp/Data features. This PR mainly moves code. There are still many legacy problems and TODOs in code, leave unrelated changes to future PRs.
modules/context/context.go
always usectx.HTML
(indirectly) to render the error, however it is possible that the context doesn't have any HTML render set(e.g.APIContext
).ctx.JSON
.dummyRender
for the API/Package Context to not see a vague NPE error.For clarity purposes, this panic was found via:
gitea/routers/api/v1/api.go
Line 152 in 6f3efdf
gitea/modules/context/context.go
Line 144 in 6f3efdf
gitea/modules/context/context.go
Line 305 in 6f3efdf
gitea/modules/context/context.go
Line 227 in 6f3efdf
ContextAPI doesn't set the
Render
field, which caused an NPE.