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

[#11001] Handle datastore exceptions using the new Google-provided SDK #11025

Conversation

jianhandev
Copy link
Contributor

@jianhandev jianhandev commented Mar 10, 2021

Part of #11001

In the new Google-provided SDK used by Objectify 6 to access the datastore, exceptions are handled using DatastoreException which classifies errors based on canonical error codes (e.g. NOT_FOUNDDEADLINE_EXCEEDED etc).

Considerations made:

  • Previously, DatastoreTimeoutException (provided by the old appengine api) is thrown if a timeout is encountered when trying to access the the datastore. Now the Google-provided SDK used by Objectify 6 does not have such a specific class to handle timeouts, canonical error codes are used instead to specify error type.

Solution:

  • Handle DatastoreException separately from DeadlineExceededException to throw more specific datastore error messages.

@teammates-bot
Copy link

Hi @jianhandev, these parts of your pull request do not appear to follow our contributing guidelines:

  1. PR Title
    • Issue Reference (#<issue-number>) missing.

@jianhandev jianhandev changed the title Handle datastore exceptions using the new Google-provided SDK [#11001] Handle datastore exceptions using the new Google-provided SDK Mar 10, 2021
Copy link
Contributor

@Derek-Hardy Derek-Hardy left a comment

Choose a reason for hiding this comment

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

I think it is necessary to getCode() or getReason() to indicate the specific kind.

if (error.equals(DatastoreTimeoutException.class.getSimpleName())) {
throw new DatastoreTimeoutException("DatastoreTimeoutException testing");
if (error.equals(DatastoreException.class.getSimpleName())) {
throw new DatastoreException(4, "DatastoreException testing", "DEADLINE_EXCEEDED");
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can assign a variable name instead of hardcoding the HTTP status code here.

Besides, according to documentation:

DEADLINE_EXCEEDED: A deadline was exceeded on the server.

It seems to be a server-side error and therefore status code should be 5xx?

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 seems to be a server-side error and therefore status code should be 5xx?

I perceived the code to be one of the canonical error codes here. I'm not 100% sure whether it refers to the canonical or the HTTP status code (if it is, it would be 504 Gateway Timeout). 'DEADLINE_EXCEEDED' has error code 4 which I'm assuming corresponds to a timeout exception.

  // The deadline expired before the operation could complete. For operations
  // that change the state of the system, this error may be returned
  // even if the operation has completed successfully.  For example, a
  // successful response from a server could have been delayed long
  // enough for the deadline to expire.
  //
  // HTTP Mapping: 504 Gateway Timeout
  DEADLINE_EXCEEDED = 4;

Copy link
Contributor

Choose a reason for hiding this comment

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

@jianhandev OK, I think 4 should be correct.

Their devs seem to be confused as well.
googleapis/google-cloud-java#2442 (comment)

@@ -105,7 +105,7 @@ private void invokeServlet(HttpServletRequest req, HttpServletResponse resp) thr
log.warning(enfe.getClass().getSimpleName() + " caught by WebApiServlet: "
+ TeammatesException.toStringWithStackTrace(enfe));
throwError(resp, HttpStatus.SC_NOT_FOUND, enfe.getMessage());
} catch (DeadlineExceededException | DatastoreTimeoutException e) {
} catch (DeadlineExceededException | DatastoreException e) {
Copy link
Member

Choose a reason for hiding this comment

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

This block translates the exception to 504 Gateway Timeout error, but I believe only a small subset of DatastoreException fits into that.

Copy link
Contributor Author

@jianhandev jianhandev Mar 11, 2021

Choose a reason for hiding this comment

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

Based on this file only DEADLINE_EXCEEDED maps to 504 Gateway Timeout. I'm thinking we can handle DatastoreException separately in a new catch clause and throw an error with HttpStatus .SC_INTERNAL_SERVER_ERROR and the specific error message depending on the type of datastore error.

@Derek-Hardy Derek-Hardy added the s.Ongoing The PR is being worked on by the author(s) label Mar 11, 2021
Copy link
Member

@wkurniawan07 wkurniawan07 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@Derek-Hardy Derek-Hardy left a comment

Choose a reason for hiding this comment

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

LGTM

@Derek-Hardy Derek-Hardy added s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging and removed s.Ongoing The PR is being worked on by the author(s) labels Mar 14, 2021
@madanalogy madanalogy modified the milestone: V7.12.0 Mar 15, 2021
@wkurniawan07 wkurniawan07 merged commit 0bf194b into TEAMMATES:objectify-v6-migration Mar 18, 2021
wkurniawan07 pushed a commit that referenced this pull request Mar 22, 2021
#11025)

* Remove usage of DatastoreTimeoutException from old api

* Get WebApiServlet to throw specific datastore error messages

* Replace hard-coded values with enum values of google.rpc.Code
wkurniawan07 pushed a commit that referenced this pull request Mar 22, 2021
#11025)

* Remove usage of DatastoreTimeoutException from old api

* Get WebApiServlet to throw specific datastore error messages

* Replace hard-coded values with enum values of google.rpc.Code
wkurniawan07 pushed a commit that referenced this pull request Mar 23, 2021
#11025)

* Remove usage of DatastoreTimeoutException from old api

* Get WebApiServlet to throw specific datastore error messages

* Replace hard-coded values with enum values of google.rpc.Code
@wkurniawan07 wkurniawan07 added this to the V8.0.0 milestone Apr 25, 2021
wkurniawan07 pushed a commit that referenced this pull request Apr 28, 2021
#11025)

* Remove usage of DatastoreTimeoutException from old api

* Get WebApiServlet to throw specific datastore error messages

* Replace hard-coded values with enum values of google.rpc.Code
wkurniawan07 pushed a commit that referenced this pull request May 3, 2021
#11025)

* Remove usage of DatastoreTimeoutException from old api

* Get WebApiServlet to throw specific datastore error messages

* Replace hard-coded values with enum values of google.rpc.Code
wkurniawan07 pushed a commit that referenced this pull request Jun 2, 2021
#11025)

* Remove usage of DatastoreTimeoutException from old api

* Get WebApiServlet to throw specific datastore error messages

* Replace hard-coded values with enum values of google.rpc.Code
wkurniawan07 pushed a commit that referenced this pull request Jun 6, 2021
#11025)

* Remove usage of DatastoreTimeoutException from old api

* Get WebApiServlet to throw specific datastore error messages

* Replace hard-coded values with enum values of google.rpc.Code
@wkurniawan07 wkurniawan07 added the c.Task Other non-user-facing works, e.g. refactoring, adding tests label Jun 6, 2021
wkurniawan07 pushed a commit that referenced this pull request Jun 6, 2021
#11025)

* Remove usage of DatastoreTimeoutException from old api

* Get WebApiServlet to throw specific datastore error messages

* Replace hard-coded values with enum values of google.rpc.Code
daongochieu2810 pushed a commit to daongochieu2810/teammates that referenced this pull request Jul 5, 2021
…ovided SDK (TEAMMATES#11025)

* Remove usage of DatastoreTimeoutException from old api

* Get WebApiServlet to throw specific datastore error messages

* Replace hard-coded values with enum values of google.rpc.Code
@jianhandev jianhandev deleted the 11001-handle-datastore-exceptions-with-new-api branch August 4, 2023 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c.Task Other non-user-facing works, e.g. refactoring, adding tests s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants