Browse Source

[DocumentUpdaterManager] use a new DocumentUpdaterRequestFailedError

Jakob Ackermann 6 năm trước cách đây
mục cha
commit
8abfdb87ff

+ 12 - 17
services/real-time/app/js/DocumentUpdaterManager.js

@@ -6,7 +6,11 @@ const _ = require('underscore')
 const logger = require('logger-sharelatex')
 const settings = require('settings-sharelatex')
 const metrics = require('metrics-sharelatex')
-const { NullBytesInOpError, UpdateTooLargeError } = require('./Errors')
+const {
+  DocumentUpdaterRequestFailedError,
+  NullBytesInOpError,
+  UpdateTooLargeError
+} = require('./Errors')
 
 const rclient = require('redis-sharelatex').createClient(
   settings.redis.documentupdater
@@ -51,15 +55,9 @@ const DocumentUpdaterManager = {
         )
         callback(err)
       } else {
-        err = new Error(
-          `doc updater returned a non-success status code: ${res.statusCode}`
+        callback(
+          new DocumentUpdaterRequestFailedError('getDocument', res.statusCode)
         )
-        err.statusCode = res.statusCode
-        logger.error(
-          { err, project_id, doc_id, url },
-          `doc updater returned a non-success status code: ${res.statusCode}`
-        )
-        callback(err)
       }
     })
   },
@@ -89,15 +87,12 @@ const DocumentUpdaterManager = {
         logger.log({ project_id }, 'deleted project from document updater')
         callback(null)
       } else {
-        err = new Error(
-          `document updater returned a failure status code: ${res.statusCode}`
+        callback(
+          new DocumentUpdaterRequestFailedError(
+            'flushProjectToMongoAndDelete',
+            res.statusCode
+          )
         )
-        err.statusCode = res.statusCode
-        logger.error(
-          { err, project_id },
-          `document updater returned failure status code: ${res.statusCode}`
-        )
-        callback(err)
       }
     })
   },

+ 10 - 0
services/real-time/app/js/Errors.js

@@ -21,6 +21,15 @@ class DataTooLargeToParseError extends OError {
   }
 }
 
+class DocumentUpdaterRequestFailedError extends OError {
+  constructor(action, statusCode) {
+    super('doc updater returned a non-success status code', {
+      action,
+      statusCode
+    })
+  }
+}
+
 class MissingSessionError extends OError {
   constructor() {
     super('could not look up session by key')
@@ -55,6 +64,7 @@ module.exports = {
   CodedError,
   CorruptedJoinProjectResponseError,
   DataTooLargeToParseError,
+  DocumentUpdaterRequestFailedError,
   MissingSessionError,
   NotAuthorizedError,
   NullBytesInOpError,

+ 23 - 13
services/real-time/test/unit/js/DocumentUpdaterManagerTests.js

@@ -163,13 +163,18 @@ describe('DocumentUpdaterManager', function () {
 
       return it('should return the callback with an error', function () {
         this.callback.called.should.equal(true)
-        const err = this.callback.getCall(0).args[0]
-        err.should.have.property('statusCode', 500)
-        err.should.have.property(
-          'message',
-          'doc updater returned a non-success status code: 500'
-        )
-        return this.logger.error.called.should.equal(true)
+        this.callback
+          .calledWith(
+            sinon.match({
+              message: 'doc updater returned a non-success status code',
+              info: {
+                action: 'getDocument',
+                statusCode: 500
+              }
+            })
+          )
+          .should.equal(true)
+        this.logger.error.called.should.equal(false)
       })
     })
   })
@@ -234,12 +239,17 @@ describe('DocumentUpdaterManager', function () {
 
       return it('should return the callback with an error', function () {
         this.callback.called.should.equal(true)
-        const err = this.callback.getCall(0).args[0]
-        err.should.have.property('statusCode', 500)
-        return err.should.have.property(
-          'message',
-          'document updater returned a failure status code: 500'
-        )
+        this.callback
+          .calledWith(
+            sinon.match({
+              message: 'doc updater returned a non-success status code',
+              info: {
+                action: 'flushProjectToMongoAndDelete',
+                statusCode: 500
+              }
+            })
+          )
+          .should.equal(true)
       })
     })
   })