Browse Source

Merge pull request #11974 from overleaf/jk-catch-errors-in-rename-file

[web] add logging to catch an error in renameEntity

GitOrigin-RevId: 74a942e87150a4fa94f7b1b46732de3df8b36389
ilkin-overleaf 3 years ago
parent
commit
eb32081585

+ 13 - 0
services/web/app/src/Features/Project/ProjectEntityUpdateHandler.js

@@ -1302,6 +1302,19 @@ const ProjectEntityUpdateHandler = {
     source,
     callback
   ) {
+    if (!newName || typeof newName !== 'string') {
+      const err = new OError('invalid newName value', {
+        value: newName,
+        type: typeof newName,
+        projectId,
+        entityId,
+        entityType,
+        userId,
+        source,
+      })
+      logger.error({ err }, 'Invalid newName passed to renameEntity')
+      return callback(err)
+    }
     if (!SafePath.isCleanFilename(newName)) {
       return callback(new Errors.InvalidNameError('invalid element name'))
     }

+ 37 - 0
services/web/test/unit/src/Project/ProjectEntityUpdateHandlerTests.js

@@ -1913,6 +1913,43 @@ describe('ProjectEntityUpdateHandler', function () {
         this.callback.calledWithMatch(errorMatcher).should.equal(true)
       })
     })
+
+    describe('renaming an entity with a non-string value', function () {
+      beforeEach(function () {
+        this.project_name = 'project name'
+        this.startPath = '/folder/a.tex'
+        this.endPath = '/folder/b.tex'
+        this.rev = 2
+        this.changes = { newDocs: ['old-doc'], newFiles: ['old-file'] }
+        this.newDocName = ['hello']
+        this.ProjectEntityMongoUpdateHandler.renameEntity.yields(
+          null,
+          this.project,
+          this.startPath,
+          this.endPath,
+          this.rev,
+          this.changes
+        )
+
+        this.ProjectEntityUpdateHandler.renameEntity(
+          projectId,
+          docId,
+          'doc',
+          this.newDocName,
+          userId,
+          this.source,
+          this.callback
+        )
+      })
+
+      it('returns an error', function () {
+        const errorMatcher = sinon.match.instanceOf(Error)
+        this.callback.calledWithMatch(errorMatcher).should.equal(true)
+        expect(
+          this.ProjectEntityMongoUpdateHandler.renameEntity.called
+        ).to.equal(false)
+      })
+    })
   })
 
   describe('resyncProjectHistory', function () {