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

send back HTTP 400 where there is a client error #1131

Merged
merged 4 commits into from
Jul 4, 2018
Merged

Conversation

gregwebs
Copy link
Contributor

What have you changed? (required)

pd server

We often send a 500 error where it should be 400 or 404.
It is important to get these right.
A client will assume it did nothing wrong when a 500 is received
and perhaps automatically retry the request.

This is a conservative change in that
there are many more places that should be changed from 500 to 400 or 404.
Mostly this is due to not investing more time to understand the entire code base.
In some cases, functions are called which don't differentiate error types properly.
A forthcoming PR will provide some help in this case.

A helper function jsonRespondError is added.

What are the type of the changes (required)?

  • Breaking change

I can only assume that most client code is not differentiating between 400 and 500 error codes. In that case there will not be any breakage.

How has this PR been tested (required)?

There is a new test suite for the jsonRespondError helper.
Otherwise the test suite was ran and I was surprised that no tests broke.

Does this PR affect documentation (docs/docs-cn) update? (optional)

There is a PR to add an API specification. That would need to be updated to add more 400 entries.

@CLAassistant
Copy link

CLAassistant commented Jun 29, 2018

CLA assistant check
All committers have signed the CLA.


type testUtilSuite struct{}

type nopCloser struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use ioutil.NopCloser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -0,0 +1,73 @@
// Copyright 2016 PingCAP, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2016 -> 2018

r.JSON(w, http.StatusBadRequest, err.Error())
default:
r.JSON(w, http.StatusInternalServerError, err.Error())

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the extra empty line.

)

func jsonRespondError(r *render.Render, w http.ResponseWriter, body io.ReadCloser, data interface{}) (err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name jsonRespondError conceals the fact that it reads json from body. Maybe something like readJSONRespondError?


}
}
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the function will never return an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will in fact return the error: it has a named return parameter. I am beginning to think though that these generally only have potential to create confusion and will get rid of it.

We often send a 500 error where it should be 400 or 404.
It is important to get these right.
A client will assume it did nothing wrong when a 500 is received
and perhaps automatically retry the request.

This is a conservative change:
there are many more places that should be changed from 500.
A helper function jsonRespondError is added
@gregwebs gregwebs force-pushed the gregwebs/http-400 branch from 53438ca to e549647 Compare July 2, 2018 13:20
@gregwebs
Copy link
Contributor Author

gregwebs commented Jul 2, 2018

@disksing PTAL

@disksing
Copy link
Contributor

disksing commented Jul 4, 2018

You don't have to update the year in all files, only new files need to use '2018'. The rest LGTM.
PTAL /cc @nolouch

@@ -56,11 +57,23 @@ func (h *tableNamespaceHandler) Get(w http.ResponseWriter, r *http.Request) {
h.rd.JSON(w, http.StatusOK, nsInfo)
}

func readJSONRespondError(r *render.Render, w http.ResponseWriter, body io.ReadCloser, data interface{}) (err error) {
Copy link
Contributor

@nolouch nolouch Jul 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this function twice times, can we move it to apiutil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to start creating new imports. I would appreciate the advise of the maintainers here if that is what is desired. I also didn't know why this handler is under /table instead of /server and why it uses the apiutil package instead of api.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the duplicate code here is ok. The package table was originally designed to be independent of the other modules of the project (like a plugin for pd-server), so it is not dependent on other modules of pd as much as possible.

@gregwebs gregwebs force-pushed the gregwebs/http-400 branch from f14b2dd to 7d49f45 Compare July 4, 2018 04:21
@gregwebs gregwebs force-pushed the gregwebs/http-400 branch from 7d49f45 to f5727d1 Compare July 4, 2018 04:41
Copy link
Contributor

@nolouch nolouch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@nolouch nolouch merged commit 312d131 into master Jul 4, 2018
@nolouch nolouch deleted the gregwebs/http-400 branch July 4, 2018 06:02
nolouch pushed a commit to nolouch/pd that referenced this pull request Aug 15, 2018
* server: use HTTP 400 where there is a client error

We often send a 500 error where it should be 400 or 404.
It is important to get these right.
A client will assume it did nothing wrong when a 500 is received
and perhaps automatically retry the request.

This is a conservative change:
there are many more places that should be changed from 500.
A helper function jsonRespondError is added
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

Successfully merging this pull request may close these issues.

4 participants