/src/com/google/appengine/datanucleus/DatastoreExceptionTranslator.java

http://datanucleus-appengine.googlecode.com/ · Java · 68 lines · 34 code · 10 blank · 24 comment · 0 complexity · 732bf07fbd3668ada1ddff6910846625 MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus;
  14. import com.google.appengine.api.datastore.DatastoreFailureException;
  15. import com.google.appengine.api.datastore.DatastoreTimeoutException;
  16. import com.google.appengine.api.datastore.EntityNotFoundException;
  17. import com.google.appengine.api.datastore.Key;
  18. import org.datanucleus.exceptions.NucleusDataStoreException;
  19. import org.datanucleus.exceptions.NucleusException;
  20. import org.datanucleus.exceptions.NucleusObjectNotFoundException;
  21. import org.datanucleus.exceptions.NucleusFatalUserException;
  22. import org.datanucleus.store.query.QueryTimeoutException;
  23. import java.util.ConcurrentModificationException;
  24. /**
  25. * Utility class that knows how to translate exceptions thrown by the datastore
  26. * api into the corresponding {@link NucleusException}.
  27. *
  28. * @author Max Ross <maxr@google.com>
  29. */
  30. public final class DatastoreExceptionTranslator {
  31. private DatastoreExceptionTranslator() {
  32. }
  33. public static NucleusException wrapIllegalArgumentException(IllegalArgumentException e) {
  34. // Bad input, so mark fatal to let user know not to retry.
  35. return new NucleusFatalUserException("Illegal argument", e);
  36. }
  37. public static NucleusDataStoreException wrapDatastoreFailureException(
  38. DatastoreFailureException e) {
  39. // could be a transient error so don't mark fatal
  40. return new NucleusDataStoreException("Datastore Failure", e);
  41. }
  42. static NucleusDataStoreException wrapConcurrentModificationException(
  43. ConcurrentModificationException e) {
  44. // do not mark fatal
  45. return new NucleusDataStoreException("Concurrent Modification", e);
  46. }
  47. static NucleusObjectNotFoundException wrapEntityNotFoundException(
  48. EntityNotFoundException e, Key key) {
  49. return new NucleusObjectNotFoundException(
  50. "Could not retrieve entity of kind " + key.getKind() + " with key " + key);
  51. }
  52. public static QueryTimeoutException wrapDatastoreTimeoutExceptionForQuery(final DatastoreTimeoutException e) {
  53. return new QueryTimeoutException(e.getMessage(), e);
  54. }
  55. }