Repository navigation
Conversation
smklein
left a comment
There was a problem hiding this comment.
This looks great (and wow, that was a quick turnaround)!
Couple comments in-line, most of which can probably be followed-up after this patch.
@davepacheco this PR LGTM, and functionally I approve, but I wanna give you a chance to peruse some of the questions brought up here.
| })?; | ||
|
|
||
| if updated_rows == 0 { | ||
| return Err(Error::InvalidRequest { |
There was a problem hiding this comment.
(Caveat: Your usage here looks right, but the Error enum itself seems weird)
Now that I'm looking at it, the description vs name for Error::InvalidRequest seems... weird.
The request was well-formed, but the operation cannot be completed given the current state of the system.
Your usage? 100% in agreement with this description. But it seems odd to call that an InavlidRequest, right? Seems like the request is valid, but the system itself isn't prepped to deal with it.
From a usability point-of-view, it seems like:
- Either we should be explicit that the caller accessing Nexus' API should retry, or
- Nexus itself should retry the operation if the issue was concurrent modification
There was a problem hiding this comment.
I agree the naming is rather odd. InvalidRequest reads like HTTP 400; I was hoping for something named PreconditionFailed or something. Agreed that we need to address some usability around this. I think option 1 is unavoidable to some degree if we want to avoid unbounded work in a request on the server (since in the case of option 2, we have to decide on how many times we're willing to try or else be unbounded)
There was a problem hiding this comment.
Looking back at this, it looks like the 412 Precondition Failed error code is meant for header mismatches, so not sure what would be more appropriate. Our code currently translates this error into a 400 Bad Request
There was a problem hiding this comment.
But it seems odd to call that an InavlidRequest, right? Seems like the request is valid, but the system itself isn't prepped to deal with it.
I don't think that's right. An example: you send "DELETE /foo/bar/baz" to an HTTP server, and /foo/bar does not exist. The request is well-formed. The server understands exactly what you were trying to do. But it was not valid for the current state of the system. Nor is that the system's fault. This is a client error.
Our InvalidRequest variant corresponds to what HTTP calls status code 400, which is "Bad Request". I felt "Invalid" was more descriptive than "bad".
Is that the right status code here? In the case where you're trying to remove something that's non-empty, I'd call that a validation error. There are loads of debates about what to use for the status code for validation errors. (409 and 422 are sometimes used. I stumbled upon this summary -- I have no idea if it's accurate or up to date -- but it suggests neither of these is that popular.) I picked 400 because I think it's not wrong (it's a client error); I'm not sure there's a more appropriate, idiomatic one; and I'm not sure a distinction from 400 would matter. If there's a more appropriate status code that we feel clients will expect because it's popular in other APIs, we can use that instead. But we should audit existing uses of InvalidError so that we're consistent about it.
From a usability point-of-view, it seems like:
1. Either we should be explicit that the caller accessing Nexus' API should retry, or 2. Nexus itself should retry the operation if the issue was concurrent modification
Neither of those is right. I think you're forgetting about the code that belongs where the // TODO is, where we check whether the Organization is empty. If a user attempts to delete an organization they thought was empty, and the system tells them it's not empty, they probably should look at what's in the Organization now and decide if deleting it is still what they want to do. Or, if this is some automation that deletes empty Organizations, then they should ignore the error and move on. We can't know what the right response is. But they definitely shouldn't blindly retry nor should we retry for them -- there's no reason to expect a retry to work because, well, the Organization is non-empty.
There was a problem hiding this comment.
I do think either 409 or 422 would be better. I see a few places saying 400 is not appropriate if the request is coherent enough to be understood by the server.
11.2. 422 Unprocessable Entity
The 422 (Unprocessable Entity) status code means the server
understands the content type of the request entity (hence a
415(Unsupported Media Type) status code is inappropriate), and the
syntax of the request entity is correct (thus a 400 (Bad Request)
status code is inappropriate) but was unable to process the contained
instructions. For example, this error condition may occur if an XML
request body contains well-formed (i.e., syntactically correct), but
semantically erroneous, XML instructions.
A couple of examples:
- Azure uses 409 on attempts to delete a non-empty directory.
- Ambiguous but Stripe probably uses 409 for this situation
There was a problem hiding this comment.
In terms of 400 being appropriate: that's definitely fair, but I don't think 400 is too wrong. I hate looking to SO for guidance, but in this case I don't think there's a super clear specification-provided answer and so the determination is influenced by convention. This popular SO question has a top answer that said to use 400 for 5+ years. It was edited in 2016 to say that 422 is "very reasonable" (but also not in HTTP 1.1). Is it worth surveying other APIs? (Is it worth spending much time on this?)
Edit: I noticed the Stripe page you linked to says they use 400 when you leave out a required parameter 🤷
There was a problem hiding this comment.
"is it worth spending much time on this" is self-answering I think 😁
I don't think I would call these errors validation errors, maybe that needs clarification. To me validation is usually something that can be done by looking at the request alone, not by checking stuff in the DB — like leaving out a required parameter. I would not call trying to delete a non-empty container a validation error.
Edit: The examples in the SO link support my definition:
Update: "Validation failure" above means an application level data validation failure, i.e., incorrectly specified datetime, bogus email address etc.
If "validation failure" means that there is some client error in the request, then use HTTP 400 (Bad Request). For instance if the URI is supposed to have an ISO-8601 date and you find that it's in the wrong format or refers to February 31st, then you would return an HTTP 400. Ditto if you expect well-formed XML in an entity body and it fails to parse.
There was a problem hiding this comment.
Hopefully Sean or Tess can weigh in about whether this mismatch was why they thought InvalidRequest / 400 was a weird fit.
There was a problem hiding this comment.
Yeah, that's certainly why I thought it was a weird fit. 400 isn't wrong, but it's difficult to diagnose that the thing that makes it invalid is the state of the things the request refers to rather than the request being malformed
| * TODO-correctness: Is it valid for PUT to accept application/json that's a | ||
| * subset of what the resource actually represents? If not, is that a problem? | ||
| * (HTTP may require that this be idempotent.) If so, can we get around that | ||
| * having this be a slightly different content-type (e.g., | ||
| * "application/json-patch")? We should see what other APIs do. |
There was a problem hiding this comment.
I think the original motivation for preferring PUT was described here in RFD 4, but you bring up a good point - this behavior basically is PATCH, right? We're describing a set up updates to the organization, rather than replacing the organization object itself.
@david-crespo , @davepacheco , do y'all have opinions here?
There was a problem hiding this comment.
FYI this is a copy of the existing comment on projects. IIRC last time I checked I couldn't find anything conclusive about this online, but I rarely see APIs using PATCH and I'm pretty sure I've seen plenty using PUT with partial bodies or even just using POST for updates.
There was a problem hiding this comment.
This comment is actually copied from an existing function below, since it also applies. This is an area of specs that I am not particularly familiar with
There was a problem hiding this comment.
On the other hand Github does use PATCH
https://docs.github.com/en/rest/reference/gists#update-a-gist
https://docs.github.com/en/rest/reference/projects#update-a-project
It's strictly more correct to use PATCH and doesn't seem to have much downside besides a small amount of work to change existing endpoints. We're using generated clients for everything anyway.
There was a problem hiding this comment.
Yeah, I think this code comment predates that section of RFD 4 that @smklein linked to. That section summarizes my thinking on this, but we've never gone back and updated Nexus to match it.
I think this (that is, the issue of which verb to use for updates and what semantics it has) has historically been kind of a wild west. This comment points out that PUT wouldn't have made sense for the Stripe API's semantics and PATCH (or its widespread use) may predate their API. (I haven't fact-checked that -- I thought PATCH was pretty old, part of WebDAV.)
There was a problem hiding this comment.
So, we can just use PATCH, but then the question is what format to use? What we're doing here is basically RFC 7396. It's intuitive, but it doesn't handle some cases well (like fields that can be null, or deleting fields, or arrays). It would be more rigorous to adopt RFC 6902. But that seems likely to be more work on the client than just sending a PUT with the whole resource.
There's nothing to stop us from supporting any combination of PUT/PATCH and RFC 7396/RFC 6902 -- all at the same time. We can use the content type to distinguish the format and we could even treat the verbs equivalently if we really wanted to. I think it's a matter of choosing which one we want to support first, among:
- PUT + RFC 7936: basically what we're doing today: intuitive, not unprecedented, but violates expectations about HTTP "PUT", and we're not actually checking the content type so we might surprise some people who think it's a standard PUT
- PATCH + RFC 7936 + require the right content type: similar to 1, not much more work, more consistent with standards, but handles fewer cases than options 3 or 4
- PUT + whole resource + check content type: clear, supports all cases, consistent with standards, maybe somewhat annoying on the client
- PATCH + RFC 6902 + check content type: also clear, supports all cases, consistent with standards, and probably more annoying on the client
I was trying to suggest (3) in that section of RFD 4, though I didn't mention checking the content type. We probably should make sure that the content is "application/json" and definitely not one of the 7396 or 6902 content types.
There was a problem hiding this comment.
We should probably move this discussion into a dedicated issue since resolving it is not part of this PR.
I do find RFC 6902 onerous, so I'd go with 2 or 3. 3 shouldn't really be annoying for the client, though I can imagine some situations where you'd want to update a resource without having a full copy of it on hand. I guess for those few cases you could add a dedicated post endpoint instead like we've done with instance stop/start/reboot.
| })?; | ||
|
|
||
| if updated_rows == 0 { | ||
| return Err(Error::InvalidRequest { |
There was a problem hiding this comment.
But it seems odd to call that an InavlidRequest, right? Seems like the request is valid, but the system itself isn't prepped to deal with it.
I don't think that's right. An example: you send "DELETE /foo/bar/baz" to an HTTP server, and /foo/bar does not exist. The request is well-formed. The server understands exactly what you were trying to do. But it was not valid for the current state of the system. Nor is that the system's fault. This is a client error.
Our InvalidRequest variant corresponds to what HTTP calls status code 400, which is "Bad Request". I felt "Invalid" was more descriptive than "bad".
Is that the right status code here? In the case where you're trying to remove something that's non-empty, I'd call that a validation error. There are loads of debates about what to use for the status code for validation errors. (409 and 422 are sometimes used. I stumbled upon this summary -- I have no idea if it's accurate or up to date -- but it suggests neither of these is that popular.) I picked 400 because I think it's not wrong (it's a client error); I'm not sure there's a more appropriate, idiomatic one; and I'm not sure a distinction from 400 would matter. If there's a more appropriate status code that we feel clients will expect because it's popular in other APIs, we can use that instead. But we should audit existing uses of InvalidError so that we're consistent about it.
From a usability point-of-view, it seems like:
1. Either we should be explicit that the caller accessing Nexus' API should retry, or 2. Nexus itself should retry the operation if the issue was concurrent modification
Neither of those is right. I think you're forgetting about the code that belongs where the // TODO is, where we check whether the Organization is empty. If a user attempts to delete an organization they thought was empty, and the system tells them it's not empty, they probably should look at what's in the Organization now and decide if deleting it is still what they want to do. Or, if this is some automation that deletes empty Organizations, then they should ignore the error and move on. We can't know what the right response is. But they definitely shouldn't blindly retry nor should we retry for them -- there's no reason to expect a retry to work because, well, the Organization is non-empty.
This introduces basic management of Organizations (creating, getting, listing, updating, deleting), but does not include the parts that relate Projects to Organizations. That will require substantial changes to the existing Projects code, so it seems best to keep it separate.
This work is part of #299.