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

return parsing exception 400 for parsing errors #1593

Merged
merged 2 commits into from
Nov 6, 2023

Conversation

Zhangxunmt
Copy link
Collaborator

@Zhangxunmt Zhangxunmt commented Nov 4, 2023

Description

Fix the 500 internal service error reported in the pen test. All parsing errors should return 400 error code showing it's coming from users.

Test:

PUT /_plugins/_ml/connectors/zmMCmIsBaumhjugAg9cu
{
  "description": null
}
Response:
{
  "error": {
    "root_cause": [
      {
        "type": "parse_exception",
        "reason": "Can't get text on a VALUE_NULL at 2:18"
      }
    ],
    "type": "parse_exception",
    "reason": "Can't get text on a VALUE_NULL at 2:18"
  },
  "status": 400
}
PUT /_plugins/_ml/connectors/zmMCmIsBaumhjugAg9cu
{
  "description": "Update description"
}
Response:
{
  "_index": ".plugins-ml-connector",
  "_id": "zmMCmIsBaumhjugAg9cu",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 2
}

Issues Resolved

[List any issues this PR will resolve]

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Xun Zhang <xunzh@amazon.com>
Copy link

codecov bot commented Nov 4, 2023

Codecov Report

Merging #1593 (c9aa968) into 2.x (d8e8ff2) will increase coverage by 0.02%.
The diff coverage is 100.00%.

@@             Coverage Diff              @@
##                2.x    #1593      +/-   ##
============================================
+ Coverage     80.64%   80.66%   +0.02%     
- Complexity     4177     4178       +1     
============================================
  Files           399      399              
  Lines         16816    16818       +2     
  Branches       1815     1815              
============================================
+ Hits          13561    13566       +5     
+ Misses         2540     2537       -3     
  Partials        715      715              
Flag Coverage Δ
ml-commons 80.66% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...pensearch/ml/rest/RestMLUpdateConnectorAction.java 90.47% <100.00%> (+1.00%) ⬆️

... and 1 file with indirect coverage changes

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
return MLUpdateConnectorRequest.parse(parser, connectorId);
} catch (IllegalStateException illegalStateException) {
throw new OpenSearchParseException(illegalStateException.getMessage());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can confirm in line 67 IOException could also throw a 500 status code, would you mind changing the throw new IOException to throw new OpenSearchParseException?

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 on previous comment. Don't we need to add corresponding test as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a good catch. Already updated in the new commit. The UT tests are added too.

Signed-off-by: Xun Zhang <xunzh@amazon.com>
@@ -114,8 +114,15 @@ public void testUpdateConnectorRequest() throws Exception {
assertEquals("2", updateConnectorRequest.getUpdateContent().getVersion());
}

public void testUpdateConnectorRequestWithParsingException() throws Exception {
exceptionRule.expect(OpenSearchParseException.class);
exceptionRule.expectMessage("Can't get text on a VALUE_NULL");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curious where are we setting this message? Can we have more readable message from customer's point of view?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the message from the XContentParser in OpenSearch, which contains the exact line and coordinate where the null comes from. exceptionRule.expectMessage only checks if the real message contains this "Can't get text on a VALUE_NULL". The means the real message returned to customer is more explanatory.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for the clarification? Just out of curiosity, do we know what's the final message we send to customer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, please check the description of the PR which has the exact coordinate of the error. Also please note this is one example of the paring exception.

@Zhangxunmt Zhangxunmt merged commit c46d63c into opensearch-project:2.x Nov 6, 2023
8 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants