From 4b903626b4ce460af0efb1a0ec15c3a033239027 Mon Sep 17 00:00:00 2001 From: John Lees-Miller Date: Fri, 17 Apr 2020 08:44:50 +0100 Subject: [PATCH] Apply linting rules --- libraries/o-error/README.md | 11 +++--- libraries/o-error/http.js | 2 +- libraries/o-error/index.js | 12 +++---- libraries/o-error/test/http.test.js | 6 ++-- libraries/o-error/test/o-error-util.test.js | 28 +++++++-------- libraries/o-error/test/o-error.test.js | 38 +++++++++------------ libraries/o-error/test/support/index.js | 2 +- 7 files changed, 48 insertions(+), 51 deletions(-) diff --git a/libraries/o-error/README.md b/libraries/o-error/README.md index 210c17f11d..50a6600214 100644 --- a/libraries/o-error/README.md +++ b/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) Make custom error classes that: + - pass `instanceof` checks, - have stack traces, - 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 const OError = require('@overleaf/o-error') -function doSomethingBad () { +function doSomethingBad() { throw new OError({ message: 'did something bad', - info: { thing: 'foo' } + info: { thing: 'foo' }, }) } doSomethingBad() @@ -35,12 +36,12 @@ doSomethingBad() ```js class FooError extends OError { - constructor (options) { + constructor(options) { super({ message: 'failed to foo', ...options }) } } -function doFoo () { +function doFoo() { throw new FooError({ info: { foo: 'bar' } }) } doFoo() @@ -54,7 +55,7 @@ doFoo() ### Wrapping an inner error (cause) ```js -function doFoo2 () { +function doFoo2() { try { throw new Error('bad') } catch (err) { diff --git a/libraries/o-error/http.js b/libraries/o-error/http.js index 21b65b5d79..e6c1bd5a0b 100644 --- a/libraries/o-error/http.js +++ b/libraries/o-error/http.js @@ -85,5 +85,5 @@ module.exports = { NotAcceptableError, ConflictError, UnprocessableEntityError, - TooManyRequestsError + TooManyRequestsError, } diff --git a/libraries/o-error/index.js b/libraries/o-error/index.js index b6cf0362b1..f7259b177e 100644 --- a/libraries/o-error/index.js +++ b/libraries/o-error/index.js @@ -21,7 +21,7 @@ class OError extends Error { * @param {string} message as for built-in Error * @param {?object} info extra data to attach to the error */ - constructor ({ message, info }) { + constructor({ message, info }) { super(message) this.name = this.constructor.name if (info) { @@ -35,7 +35,7 @@ class OError extends Error { * @param {Error} cause * @return {this} */ - withCause (cause) { + withCause(cause) { this.cause = cause if (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 * @return {Object} */ -function getFullInfo (error) { +function getFullInfo(error) { if (!error) return {} const info = getFullInfo(error.cause) 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 * @return {string} */ -function getFullStack (error) { +function getFullStack(error) { if (!error) return '' const causeStack = getFullStack(error.cause) - if (causeStack) return (error.stack + '\ncaused by: ' + causeStack) + if (causeStack) return error.stack + '\ncaused by: ' + causeStack return error.stack } @@ -81,7 +81,7 @@ function getFullStack (error) { * @param {function} klass * @return {Boolean} */ -function hasCauseInstanceOf (error, klass) { +function hasCauseInstanceOf(error, klass) { if (!error) return false return error instanceof klass || hasCauseInstanceOf(error.cause, klass) } diff --git a/libraries/o-error/test/http.test.js b/libraries/o-error/test/http.test.js index e6c16a68d4..1d1bac0035 100644 --- a/libraries/o-error/test/http.test.js +++ b/libraries/o-error/test/http.test.js @@ -1,8 +1,8 @@ const OError = require('..') const HttpErrors = require('../http') -describe('OError/http', () => { - it('is instance of OError', () => { +describe('OError/http', function () { + it('is instance of OError', function () { try { throw new HttpErrors.ConflictError() } catch (e) { @@ -10,7 +10,7 @@ describe('OError/http', () => { } }) - it('has status code', () => { + it('has status code', function () { try { throw new HttpErrors.ConflictError() } catch (e) { diff --git a/libraries/o-error/test/o-error-util.test.js b/libraries/o-error/test/o-error-util.test.js index 80083a79b6..f803a7feda 100644 --- a/libraries/o-error/test/o-error-util.test.js +++ b/libraries/o-error/test/o-error-util.test.js @@ -1,18 +1,18 @@ 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') - 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') err.info = { 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 err2 = new Error('bar') err1.cause = err2 @@ -20,14 +20,14 @@ describe('OError.getFullInfo', () => { 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 err2 = new Error('bar') err1.cause = err2 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 err2 = new Error('bar') err1.cause = err2 @@ -36,22 +36,22 @@ describe('OError.getFullInfo', () => { 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') err.info = 'test' 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 fullStack = getFullStack(err) expect(fullStack).to.match(/^Error: foo$/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 err2 = new Error('bar') 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') expect(hasCauseInstanceOf(null, Error)).to.be.false expect(hasCauseInstanceOf(err, Error)).to.be.true 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 err2 = new RangeError('bar') err1.cause = err2 diff --git a/libraries/o-error/test/o-error.test.js b/libraries/o-error/test/o-error.test.js index 0fd5020528..2a4f319223 100644 --- a/libraries/o-error/test/o-error.test.js +++ b/libraries/o-error/test/o-error.test.js @@ -2,24 +2,24 @@ const OError = require('..') const { expectError } = require('./support') class CustomError1 extends OError { - constructor (options) { + constructor(options) { super({ message: 'failed to foo', ...options }) } } class CustomError2 extends OError { - constructor (options) { + constructor(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') } - function doSomethingBad () { + function doSomethingBad() { try { doSomethingBadInternally() } catch (err) { @@ -35,25 +35,23 @@ describe('OError', () => { name: 'CustomError1', klass: CustomError1, message: 'CustomError1: failed to foo: internal error', - firstFrameRx: /doSomethingBad/ + firstFrameRx: /doSomethingBad/, }) expect(OError.getFullInfo(e)).to.deep.equal({ userId: 123 }) const fullStack = OError.getFullStack(e) expect(fullStack).to.match( /^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') } - function doBar () { + function doBar() { try { doSomethingBadInternally() } catch (err) { @@ -61,7 +59,7 @@ describe('OError', () => { } } - function doFoo () { + function doFoo() { try { doBar() } catch (err) { @@ -77,11 +75,11 @@ describe('OError', () => { name: 'CustomError1', klass: CustomError1, message: 'CustomError1: failed to foo: failed to bar: internal error', - firstFrameRx: /doFoo/ + firstFrameRx: /doFoo/, }) expect(OError.getFullInfo(e)).to.deep.equal({ userId: 123, - database: 'a' + database: 'a', }) const fullStack = OError.getFullStack(e) expect(fullStack).to.match( @@ -90,18 +88,16 @@ describe('OError', () => { expect(fullStack).to.match( /^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 { throw new CustomError1({}) } catch (e) { 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 } }) diff --git a/libraries/o-error/test/support/index.js b/libraries/o-error/test/support/index.js index 127c171c38..2bce3236c6 100644 --- a/libraries/o-error/test/support/index.js +++ b/libraries/o-error/test/support/index.js @@ -4,7 +4,7 @@ var chai = require('chai') 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 expect(e.name).to.equal(expected.name)