Просмотр исходного кода

handle custom errors created without info

Tim Alby 7 лет назад
Родитель
Сommit
7bb8a5bd11
2 измененных файлов с 14 добавлено и 1 удалено
  1. 3 1
      libraries/o-error/index.js
  2. 11 0
      libraries/o-error/test/error-type-class.test.js

+ 3 - 1
libraries/o-error/index.js

@@ -30,7 +30,9 @@ class ErrorTypeError extends Error {
   constructor ({ message, info }) {
     super(message)
     this.name = this.constructor.name
-    this.info = info
+    if (info) {
+      this.info = info
+    }
   }
 
   /**

+ 11 - 0
libraries/o-error/test/error-type-class.test.js

@@ -95,6 +95,17 @@ describe('errorType.Error', () => {
       )
     }
   })
+
+  it('handles a custom error without info', () => {
+    try {
+      throw new CustomError1({})
+      expect.fail('should have thrown')
+    } catch (e) {
+      expect(errorType.getFullInfo(e)).to.deep.equal({})
+      let infoKey = Object.keys(e).find(k => k === 'info')
+      expect(infoKey).to.not.exist
+    }
+  })
 })
 
 describe('errorType.ErrorWithStatusCode', () => {