-
Notifications
You must be signed in to change notification settings - Fork 490
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
Wider catch for exceptions during applying patch. #2192
Wider catch for exceptions during applying patch. #2192
Conversation
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
@@ -82,7 +82,7 @@ private Resource GetPatchedJsonResource(FhirJsonNode node, JsonPatchDocument ope | |||
{ | |||
operations.ApplyTo(node.JsonObject); | |||
} | |||
catch (JsonPatchException e) | |||
catch (Exception e) |
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.
What is the exception case that the general catch will cover that the JsonPatchException wont? I'm concerned about this catching and passing over an exception we would want logged.
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.
ArgmentNullException is one of them.
I also concern with widening catch net, but there is no way to validate JsonPatchDocument and we wrap only one string of code which we have not much control over.
I can add logging if you think it would be useful.
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.
My thought would be to have the specific exceptions (JsonPatch, ArgumentNull, etc) just throw the request not valid exception, and have a second catch for the general exception that still throws the request not valid exception but also logs the exception. That way we are aware of what is causing failures and we can either add it to the list of exceptions that don't log or fix it if it is a real bug.
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 revert widening, and add validation instead.
throw new BadRequestException($"{operation.op} is invalid."); | ||
} | ||
|
||
if (operation.path == null) |
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.
Should this be?
if (operation.path == null) | |
if (string.IsNullOrWhiteSpace(operation.path)) |
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 think empty path is apply to whole resource, so I think it's valid thing.
Also I don't want to write my own validation for operations, it should come from library, so I'll just try to catch ANE during patching instead of this.
string patchDocument = | ||
"[{\"op\":\"replace\",\"value\":\"female\"}, {\"op\":\"remove\",\"path\":\"/address\"}]"; | ||
|
||
var exception = await Assert.ThrowsAsync<FhirException>(() => _client.PatchAsync(response.Resource, patchDocument)); | ||
Assert.Equal(HttpStatusCode.BadRequest, exception.Response.StatusCode); | ||
|
||
patchDocument = | ||
"[{\"op\":\"coo\",\"path\":\"/gender\",\"value\":\"female\"}, {\"op\":\"remove\",\"path\":\"/address\"}]"; |
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.
Convert to Theory?
Description
JsonPatchDocument can be formatted in a way it would be accepted by server, but during patch it would throw error.
One of the known cases: absence of
path
in operation.I'm making wide catch statement to catch exceptions and return bad request rather than 500.
Related issues
Addresses [#84831].
Testing
Unit test added.
FHIR Team Checklist
Semver Change (docs)
Patch