Uploaded image for project: 'Nuxeo Platform'
  1. Nuxeo Platform
  2. NXP-2008

Make CoreSession.save/createDocument raise helpful ClientExceptions instead of NullPointerExceptions

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 5.1.2
    • Fix Version/s: 5.1.3, 5.2 M1
    • Component/s: None

      Description

      The following change will help "customers" of the CoreSession API to better spot the origin of their mistake when using the CoreSession methods with invalid argument:

      Index: nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/DocumentResolver.java
      ===================================================================
      — nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/DocumentResolver.java (revision 29620)
      +++ nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/DocumentResolver.java (working copy)
      @@ -54,6 +54,9 @@
      */
      public static Document resolveReference(Session session, DocumentRef docRef)
      throws DocumentException {
      + if (docRef == null)

      { + throw new DocumentException("Invalid reference (null)"); + }

      int type = docRef.type();
      Object ref = docRef.reference();
      if (ref == null) {
      Index: nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/AbstractSession.java
      ===================================================================
      — nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/AbstractSession.java (revision 29620)
      +++ nuxeo-core/src/main/java/org/nuxeo/ecm/core/api/AbstractSession.java (working copy)
      @@ -39,7 +39,6 @@
      import org.nuxeo.common.collections.ScopedMap;
      import org.nuxeo.common.utils.IdUtils;
      import org.nuxeo.common.utils.Path;
      -import org.nuxeo.common.utils.StringUtils;
      import org.nuxeo.ecm.core.NXCore;
      import org.nuxeo.ecm.core.api.event.CoreEvent;
      import org.nuxeo.ecm.core.api.event.CoreEventConstants;
      @@ -593,8 +592,18 @@
      String name = docModel.getName();
      String typeName = docModel.getType();
      DocumentRef parentRef = docModel.getParentRef();

      • assert typeName != null;
      • assert parentRef != null;
        + if (typeName == null) { + throw new ClientException( + String.format( + "cannot create document '%s' with undefined type name", + docModel.getTitle())); + }

        + if (parentRef == null)

        { + throw new ClientException( + String.format( + "cannot create document '%s' with undefined reference to parent document", + docModel.getTitle())); + }

        try {
        Document folder = resolveReference(parentRef);
        checkPermission(folder, ADD_CHILDREN);
        @@ -1304,6 +1313,14 @@
        public DocumentModel saveDocument(DocumentModel docModel)
        throws ClientException {
        try {
        + if (docModel.getRef() == null)

        { + throw new ClientException(String.format( + "cannot save document '%s' with null reference: " + + "document has probably not yet been created " + + "in the repository with " + + "'CoreSession.createDocument(docModel)'", + docModel.getTitle())); + }

        Document doc = resolveReference(docModel.getRef());
        checkPermission(doc, WRITE);

        Attachments

          Activity

            People

            • Assignee:
              ogrisel Olivier Grisel
              Reporter:
              ogrisel Olivier Grisel
              Participants:
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 2 hours
                2h
                Remaining:
                Remaining Estimate - 2 hours
                2h
                Logged:
                Time Spent - Not Specified
                Not Specified