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

Wider catch for exceptions during applying patch. #2192

Merged
merged 3 commits into from
Sep 1, 2021

Conversation

Ivanidzo4ka
Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka commented Aug 27, 2021

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

  • Update the title of the PR to be succinct and less than 50 characters
  • Add a milestone to the PR for the sprint that it is merged (i.e. add S47)
  • Tag the PR with the type of update: Bug, Dependencies, Enhancement, or New-Feature
  • Tag the PR with Azure API for FHIR if this will release to the Azure API for FHIR managed service (CosmosDB or common code related to service)
  • Tag the PR with Azure Healthcare APIs if this will release to the Azure Healthcare APIs managed service (Sql server or common code related to service)
  • Review squash-merge requirements

Semver Change (docs)

Patch

@Ivanidzo4ka Ivanidzo4ka requested a review from a team as a code owner August 27, 2021 17:45
@Ivanidzo4ka
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Ivanidzo4ka
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

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)
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

@LTA-Thinking LTA-Thinking Aug 30, 2021

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.

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 revert widening, and add validation instead.

throw new BadRequestException($"{operation.op} is invalid.");
}

if (operation.path == null)
Copy link
Member

Choose a reason for hiding this comment

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

Should this be?

Suggested change
if (operation.path == null)
if (string.IsNullOrWhiteSpace(operation.path))

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 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.

Comment on lines 59 to 66
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\"}]";
Copy link
Member

Choose a reason for hiding this comment

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

Convert to Theory?

@Ivanidzo4ka Ivanidzo4ka added this to the S70 milestone Sep 1, 2021
@Ivanidzo4ka Ivanidzo4ka added Bug Bug bug bug. Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs labels Sep 1, 2021
@Ivanidzo4ka Ivanidzo4ka merged commit 8cbcdcc into microsoft:main Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Bug Bug bug bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants