-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Return json on 500 error from API #11574
Conversation
just noticed this behaviour - it was not documented at all - of coures if you do a code lookup you can see it too :) |
@techknowlogick can we backport this? |
whoops. label didn't apply at first. It's there now:) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better error logging
Bit of a shame that this needs to be done in every route. The way I'd handle this in JS routing is to let errors throw in the routes and then process them in a "catch-all" route but I guess with missing exceptions, this is not possible in Golang. |
The other option is for the API context to provide its own internalservererror function |
we should keep it simple ... |
It would be simpler... in modules/context/api.go add the following function: // InternalServerError responds with an error message to the client with the error as a message and the file and line of the caller.
func (ctx *APIContext) InternalServerError(err error) {
log.ErrorWithSkip(1, "InternalServerError: %v", err)
ctx.JSON(http.StatusInternalServerError, APIError{
Message: err.Error(),
URL: setting.API.SwaggerURL,
})
} And whilst you're at it change the function above it to: // Error responds with an error message to client with given obj as the message.
// If status is 500, also it prints error to log.
func (ctx *APIContext) Error(status int, title string, obj interface{}) {
var message string
if err, ok := obj.(error); ok {
message = err.Error()
} else {
message = obj.(string)
}
if status == http.StatusInternalServerError {
log.ErrorWithSkip(1, "%s: %s", title, message)
}
ctx.JSON(status, APIError{
Message: message,
URL: setting.API.SwaggerURL,
})
} |
9dfb324
to
360083a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much simpler
Co-authored-by: zeripath <art27@cantab.net>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But won't this reveal internal error messages in api? That would be bad from security point of view
What secrets would they reveal? Does it matter in an open-source application? |
@lafriks |
I don't think actual error message is returned currently, if it is it should be fixed not to be. Even returning things like paths where Gitea is installed can be dangerous as it could give out information about where files are located. Also knowing what exact unexpected error is happening is so much easier to use it. |
@lafriks the actual error message is returned currently - but yeah we shouldn't do that. gitea/modules/context/context.go Line 180 in 02a52d6
Before setting the message. |
@lafriks I think to hide 500 errors on API will need its own pull to correct 500 errors witch should have other http status codes ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shame about the api.Error endpoint - but if you don't have time to fix those right now - at least we can get a fix in for the ostensible reason for this PR.
These changes now at least don't make the situation worse.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see where InternalServerError function is used anymore :)
? |
This PR provides a An example of a piece of code affected is in routers/api/v1/misc/markdown.go:79: Where // APIContext is a specific macaron context for API service
type APIContext struct {
*Context
Org *APIOrganization
} As there was previously no |
sorry my bad :) |
I would say ready to merge |
Please send backport. Can you check whether the "Allow maintainers to edit this post" checkbox shows and is checked up before posting? It's an ongoing mistery.... 🕵️ |
I agree It's an ongoing mistery! |
* add API specific InternalServerError() Co-authored-by: zeripath <art27@cantab.net> * return 500 error msg only if not Production mode * Revert "return 500 error msg only if not Production mode" This reverts commit 8467b2c. * InternalServerError Co-authored-by: zeripath <art27@cantab.net>
* add API specific InternalServerError() Co-authored-by: zeripath <art27@cantab.net> * return 500 error msg only if not Production mode * Revert "return 500 error msg only if not Production mode" This reverts commit 8467b2c. * InternalServerError Co-authored-by: zeripath <art27@cantab.net>
have created backport to v1.11 too - old untuched code allowed it easely ;) |
* add API specific InternalServerError() Co-authored-by: zeripath <art27@cantab.net> * return 500 error msg only if not Production mode * Revert "return 500 error msg only if not Production mode" This reverts commit 8467b2c. * InternalServerError Co-authored-by: zeripath <art27@cantab.net>
if ctx.InternalServerError(err) is used it will return the default 500 html page
on API a json Error is expected