John Lees-Miller 6 жил өмнө
parent
commit
4b903626b4

+ 6 - 5
libraries/o-error/README.md

@@ -3,6 +3,7 @@
 [![CircleCI](https://circleci.com/gh/overleaf/o-error.svg?style=svg)](https://circleci.com/gh/overleaf/o-error)
 [![CircleCI](https://circleci.com/gh/overleaf/o-error.svg?style=svg)](https://circleci.com/gh/overleaf/o-error)
 
 
 Make custom error classes that:
 Make custom error classes that:
+
 - pass `instanceof` checks,
 - pass `instanceof` checks,
 - have stack traces,
 - have stack traces,
 - support custom messages and properties (`info`), and
 - support custom messages and properties (`info`), and
@@ -17,10 +18,10 @@ ES6 classes make it easy to define custom errors by subclassing `Error`. Subclas
 ```js
 ```js
 const OError = require('@overleaf/o-error')
 const OError = require('@overleaf/o-error')
 
 
-function doSomethingBad () {
+function doSomethingBad() {
   throw new OError({
   throw new OError({
     message: 'did something bad',
     message: 'did something bad',
-    info: { thing: 'foo' }
+    info: { thing: 'foo' },
   })
   })
 }
 }
 doSomethingBad()
 doSomethingBad()
@@ -35,12 +36,12 @@ doSomethingBad()
 
 
 ```js
 ```js
 class FooError extends OError {
 class FooError extends OError {
-  constructor (options) {
+  constructor(options) {
     super({ message: 'failed to foo', ...options })
     super({ message: 'failed to foo', ...options })
   }
   }
 }
 }
 
 
-function doFoo () {
+function doFoo() {
   throw new FooError({ info: { foo: 'bar' } })
   throw new FooError({ info: { foo: 'bar' } })
 }
 }
 doFoo()
 doFoo()
@@ -54,7 +55,7 @@ doFoo()
 ### Wrapping an inner error (cause)
 ### Wrapping an inner error (cause)
 
 
 ```js
 ```js
-function doFoo2 () {
+function doFoo2() {
   try {
   try {
     throw new Error('bad')
     throw new Error('bad')
   } catch (err) {
   } catch (err) {

+ 1 - 1
libraries/o-error/http.js

@@ -85,5 +85,5 @@ module.exports = {
   NotAcceptableError,
   NotAcceptableError,
   ConflictError,
   ConflictError,
   UnprocessableEntityError,
   UnprocessableEntityError,
-  TooManyRequestsError
+  TooManyRequestsError,
 }
 }

+ 6 - 6
libraries/o-error/index.js

@@ -21,7 +21,7 @@ class OError extends Error {
    * @param {string} message as for built-in Error
    * @param {string} message as for built-in Error
    * @param {?object} info extra data to attach to the error
    * @param {?object} info extra data to attach to the error
    */
    */
-  constructor ({ message, info }) {
+  constructor({ message, info }) {
     super(message)
     super(message)
     this.name = this.constructor.name
     this.name = this.constructor.name
     if (info) {
     if (info) {
@@ -35,7 +35,7 @@ class OError extends Error {
    * @param {Error} cause
    * @param {Error} cause
    * @return {this}
    * @return {this}
    */
    */
-  withCause (cause) {
+  withCause(cause) {
     this.cause = cause
     this.cause = cause
     if (this.message && cause.message) {
     if (this.message && cause.message) {
       this.message += ': ' + cause.message
       this.message += ': ' + cause.message
@@ -53,7 +53,7 @@ class OError extends Error {
  * @param {?Error} error assumed not to have circular causes
  * @param {?Error} error assumed not to have circular causes
  * @return {Object}
  * @return {Object}
  */
  */
-function getFullInfo (error) {
+function getFullInfo(error) {
   if (!error) return {}
   if (!error) return {}
   const info = getFullInfo(error.cause)
   const info = getFullInfo(error.cause)
   if (typeof error.info === 'object') Object.assign(info, error.info)
   if (typeof error.info === 'object') Object.assign(info, error.info)
@@ -67,10 +67,10 @@ function getFullInfo (error) {
  * @param {?Error} error assumed not to have circular causes
  * @param {?Error} error assumed not to have circular causes
  * @return {string}
  * @return {string}
  */
  */
-function getFullStack (error) {
+function getFullStack(error) {
   if (!error) return ''
   if (!error) return ''
   const causeStack = getFullStack(error.cause)
   const causeStack = getFullStack(error.cause)
-  if (causeStack) return (error.stack + '\ncaused by: ' + causeStack)
+  if (causeStack) return error.stack + '\ncaused by: ' + causeStack
   return error.stack
   return error.stack
 }
 }
 
 
@@ -81,7 +81,7 @@ function getFullStack (error) {
  * @param  {function} klass
  * @param  {function} klass
  * @return {Boolean}
  * @return {Boolean}
  */
  */
-function hasCauseInstanceOf (error, klass) {
+function hasCauseInstanceOf(error, klass) {
   if (!error) return false
   if (!error) return false
   return error instanceof klass || hasCauseInstanceOf(error.cause, klass)
   return error instanceof klass || hasCauseInstanceOf(error.cause, klass)
 }
 }

+ 3 - 3
libraries/o-error/test/http.test.js

@@ -1,8 +1,8 @@
 const OError = require('..')
 const OError = require('..')
 const HttpErrors = require('../http')
 const HttpErrors = require('../http')
 
 
-describe('OError/http', () => {
-  it('is instance of OError', () => {
+describe('OError/http', function () {
+  it('is instance of OError', function () {
     try {
     try {
       throw new HttpErrors.ConflictError()
       throw new HttpErrors.ConflictError()
     } catch (e) {
     } catch (e) {
@@ -10,7 +10,7 @@ describe('OError/http', () => {
     }
     }
   })
   })
 
 
-  it('has status code', () => {
+  it('has status code', function () {
     try {
     try {
       throw new HttpErrors.ConflictError()
       throw new HttpErrors.ConflictError()
     } catch (e) {
     } catch (e) {

+ 14 - 14
libraries/o-error/test/o-error-util.test.js

@@ -1,18 +1,18 @@
 const { getFullInfo, getFullStack, hasCauseInstanceOf } = require('..')
 const { getFullInfo, getFullStack, hasCauseInstanceOf } = require('..')
 
 
-describe('OError.getFullInfo', () => {
-  it('works on a normal error', () => {
+describe('OError.getFullInfo', function () {
+  it('works on a normal error', function () {
     const err = new Error('foo')
     const err = new Error('foo')
-    expect(getFullInfo(err)).to.deep.equal({ })
+    expect(getFullInfo(err)).to.deep.equal({})
   })
   })
 
 
-  it('works on an error with .info', () => {
+  it('works on an error with .info', function () {
     const err = new Error('foo')
     const err = new Error('foo')
     err.info = { userId: 123 }
     err.info = { userId: 123 }
     expect(getFullInfo(err)).to.deep.equal({ userId: 123 })
     expect(getFullInfo(err)).to.deep.equal({ userId: 123 })
   })
   })
 
 
-  it('merges info from a cause chain', () => {
+  it('merges info from a cause chain', function () {
     const err1 = new Error('foo')
     const err1 = new Error('foo')
     const err2 = new Error('bar')
     const err2 = new Error('bar')
     err1.cause = err2
     err1.cause = err2
@@ -20,14 +20,14 @@ describe('OError.getFullInfo', () => {
     expect(getFullInfo(err1)).to.deep.equal({ userId: 123 })
     expect(getFullInfo(err1)).to.deep.equal({ userId: 123 })
   })
   })
 
 
-  it('merges info from a cause chain with no info', () => {
+  it('merges info from a cause chain with no info', function () {
     const err1 = new Error('foo')
     const err1 = new Error('foo')
     const err2 = new Error('bar')
     const err2 = new Error('bar')
     err1.cause = err2
     err1.cause = err2
     expect(getFullInfo(err1)).to.deep.equal({})
     expect(getFullInfo(err1)).to.deep.equal({})
   })
   })
 
 
-  it('merges info from a cause chain with duplicate keys', () => {
+  it('merges info from a cause chain with duplicate keys', function () {
     const err1 = new Error('foo')
     const err1 = new Error('foo')
     const err2 = new Error('bar')
     const err2 = new Error('bar')
     err1.cause = err2
     err1.cause = err2
@@ -36,22 +36,22 @@ describe('OError.getFullInfo', () => {
     expect(getFullInfo(err1)).to.deep.equal({ userId: 123 })
     expect(getFullInfo(err1)).to.deep.equal({ userId: 123 })
   })
   })
 
 
-  it('works on an error with .info set to a string', () => {
+  it('works on an error with .info set to a string', function () {
     const err = new Error('foo')
     const err = new Error('foo')
     err.info = 'test'
     err.info = 'test'
     expect(getFullInfo(err)).to.deep.equal({})
     expect(getFullInfo(err)).to.deep.equal({})
   })
   })
 })
 })
 
 
-describe('OError.getFullStack', () => {
-  it('works on a normal error', () => {
+describe('OError.getFullStack', function () {
+  it('works on a normal error', function () {
     const err = new Error('foo')
     const err = new Error('foo')
     const fullStack = getFullStack(err)
     const fullStack = getFullStack(err)
     expect(fullStack).to.match(/^Error: foo$/m)
     expect(fullStack).to.match(/^Error: foo$/m)
     expect(fullStack).to.match(/^\s+at /m)
     expect(fullStack).to.match(/^\s+at /m)
   })
   })
 
 
-  it('works on an error with a cause', () => {
+  it('works on an error with a cause', function () {
     const err1 = new Error('foo')
     const err1 = new Error('foo')
     const err2 = new Error('bar')
     const err2 = new Error('bar')
     err1.cause = err2
     err1.cause = err2
@@ -63,15 +63,15 @@ describe('OError.getFullStack', () => {
   })
   })
 })
 })
 
 
-describe('OError.hasCauseInstanceOf', () => {
-  it('works on a normal error', () => {
+describe('OError.hasCauseInstanceOf', function () {
+  it('works on a normal error', function () {
     const err = new Error('foo')
     const err = new Error('foo')
     expect(hasCauseInstanceOf(null, Error)).to.be.false
     expect(hasCauseInstanceOf(null, Error)).to.be.false
     expect(hasCauseInstanceOf(err, Error)).to.be.true
     expect(hasCauseInstanceOf(err, Error)).to.be.true
     expect(hasCauseInstanceOf(err, RangeError)).to.be.false
     expect(hasCauseInstanceOf(err, RangeError)).to.be.false
   })
   })
 
 
-  it('works on an error with a cause', () => {
+  it('works on an error with a cause', function () {
     const err1 = new Error('foo')
     const err1 = new Error('foo')
     const err2 = new RangeError('bar')
     const err2 = new RangeError('bar')
     err1.cause = err2
     err1.cause = err2

+ 17 - 21
libraries/o-error/test/o-error.test.js

@@ -2,24 +2,24 @@ const OError = require('..')
 const { expectError } = require('./support')
 const { expectError } = require('./support')
 
 
 class CustomError1 extends OError {
 class CustomError1 extends OError {
-  constructor (options) {
+  constructor(options) {
     super({ message: 'failed to foo', ...options })
     super({ message: 'failed to foo', ...options })
   }
   }
 }
 }
 
 
 class CustomError2 extends OError {
 class CustomError2 extends OError {
-  constructor (options) {
+  constructor(options) {
     super({ message: 'failed to bar', ...options })
     super({ message: 'failed to bar', ...options })
   }
   }
 }
 }
 
 
-describe('OError', () => {
-  it('handles a custom error type with a cause', () => {
-    function doSomethingBadInternally () {
+describe('OError', function () {
+  it('handles a custom error type with a cause', function () {
+    function doSomethingBadInternally() {
       throw new Error('internal error')
       throw new Error('internal error')
     }
     }
 
 
-    function doSomethingBad () {
+    function doSomethingBad() {
       try {
       try {
         doSomethingBadInternally()
         doSomethingBadInternally()
       } catch (err) {
       } catch (err) {
@@ -35,25 +35,23 @@ describe('OError', () => {
         name: 'CustomError1',
         name: 'CustomError1',
         klass: CustomError1,
         klass: CustomError1,
         message: 'CustomError1: failed to foo: internal error',
         message: 'CustomError1: failed to foo: internal error',
-        firstFrameRx: /doSomethingBad/
+        firstFrameRx: /doSomethingBad/,
       })
       })
       expect(OError.getFullInfo(e)).to.deep.equal({ userId: 123 })
       expect(OError.getFullInfo(e)).to.deep.equal({ userId: 123 })
       const fullStack = OError.getFullStack(e)
       const fullStack = OError.getFullStack(e)
       expect(fullStack).to.match(
       expect(fullStack).to.match(
         /^CustomError1: failed to foo: internal error$/m
         /^CustomError1: failed to foo: internal error$/m
       )
       )
-      expect(fullStack).to.match(
-        /^caused by: Error: internal error$/m
-      )
+      expect(fullStack).to.match(/^caused by: Error: internal error$/m)
     }
     }
   })
   })
 
 
-  it('handles a custom error type with nested causes', () => {
-    function doSomethingBadInternally () {
+  it('handles a custom error type with nested causes', function () {
+    function doSomethingBadInternally() {
       throw new Error('internal error')
       throw new Error('internal error')
     }
     }
 
 
-    function doBar () {
+    function doBar() {
       try {
       try {
         doSomethingBadInternally()
         doSomethingBadInternally()
       } catch (err) {
       } catch (err) {
@@ -61,7 +59,7 @@ describe('OError', () => {
       }
       }
     }
     }
 
 
-    function doFoo () {
+    function doFoo() {
       try {
       try {
         doBar()
         doBar()
       } catch (err) {
       } catch (err) {
@@ -77,11 +75,11 @@ describe('OError', () => {
         name: 'CustomError1',
         name: 'CustomError1',
         klass: CustomError1,
         klass: CustomError1,
         message: 'CustomError1: failed to foo: failed to bar: internal error',
         message: 'CustomError1: failed to foo: failed to bar: internal error',
-        firstFrameRx: /doFoo/
+        firstFrameRx: /doFoo/,
       })
       })
       expect(OError.getFullInfo(e)).to.deep.equal({
       expect(OError.getFullInfo(e)).to.deep.equal({
         userId: 123,
         userId: 123,
-        database: 'a'
+        database: 'a',
       })
       })
       const fullStack = OError.getFullStack(e)
       const fullStack = OError.getFullStack(e)
       expect(fullStack).to.match(
       expect(fullStack).to.match(
@@ -90,18 +88,16 @@ describe('OError', () => {
       expect(fullStack).to.match(
       expect(fullStack).to.match(
         /^caused by: CustomError2: failed to bar: internal error$/m
         /^caused by: CustomError2: failed to bar: internal error$/m
       )
       )
-      expect(fullStack).to.match(
-        /^caused by: Error: internal error$/m
-      )
+      expect(fullStack).to.match(/^caused by: Error: internal error$/m)
     }
     }
   })
   })
 
 
-  it('handles a custom error without info', () => {
+  it('handles a custom error without info', function () {
     try {
     try {
       throw new CustomError1({})
       throw new CustomError1({})
     } catch (e) {
     } catch (e) {
       expect(OError.getFullInfo(e)).to.deep.equal({})
       expect(OError.getFullInfo(e)).to.deep.equal({})
-      let infoKey = Object.keys(e).find(k => k === 'info')
+      let infoKey = Object.keys(e).find((k) => k === 'info')
       expect(infoKey).to.not.exist
       expect(infoKey).to.not.exist
     }
     }
   })
   })

+ 1 - 1
libraries/o-error/test/support/index.js

@@ -4,7 +4,7 @@ var chai = require('chai')
 
 
 global.expect = chai.expect
 global.expect = chai.expect
 
 
-exports.expectError = function OErrorExpectError (e, expected) {
+exports.expectError = function OErrorExpectError(e, expected) {
   // should set the name to the error's name
   // should set the name to the error's name
   expect(e.name).to.equal(expected.name)
   expect(e.name).to.equal(expected.name)