From 96ab30883a88e9957d5c7aa7abf13b18cbfea8b7 Mon Sep 17 00:00:00 2001 From: John Lees-Miller Date: Mon, 8 Jul 2019 11:52:42 +0100 Subject: [PATCH] Remove ES5 interface --- libraries/o-error/index.js | 47 ------- libraries/o-error/test/error-type.test.js | 115 ------------------ ...type-util.test.js => o-error-util.test.js} | 0 ...ror-type-class.test.js => o-error.test.js} | 0 libraries/o-error/test/support/index.js | 2 +- 5 files changed, 1 insertion(+), 163 deletions(-) delete mode 100644 libraries/o-error/test/error-type.test.js rename libraries/o-error/test/{error-type-util.test.js => o-error-util.test.js} (100%) rename libraries/o-error/test/{error-type-class.test.js => o-error.test.js} (100%) diff --git a/libraries/o-error/index.js b/libraries/o-error/index.js index 0f00eb0600..b6cf0362b1 100644 --- a/libraries/o-error/index.js +++ b/libraries/o-error/index.js @@ -1,7 +1,5 @@ 'use strict' -var util = require('util') - /** * Make custom error types that pass `instanceof` checks, have stack traces, * support custom messages and properties, and support wrapping errors (causes). @@ -9,10 +7,6 @@ var util = require('util') * @module */ -// -// For ES6+ Classes -// - /** * A base class for custom errors that handles: * @@ -96,45 +90,4 @@ OError.getFullInfo = getFullInfo OError.getFullStack = getFullStack OError.hasCauseInstanceOf = hasCauseInstanceOf -// -// For ES5 -// - -function extendErrorType (base, name, builder) { - var errorConstructor = function () { - Error.captureStackTrace && Error.captureStackTrace(this, this.constructor) - if (builder) builder.apply(this, arguments) - this.name = name - } - - util.inherits(errorConstructor, base) - - errorConstructor.prototype.withCause = function (cause) { - this.cause = cause - if (this.message && cause.message) { - this.message += ': ' + cause.message - } - return this - } - - return errorConstructor -} - -function defineErrorType (name, builder) { - return extendErrorType(Error, name, builder) -} - -function extendErrorTypeIn (container, base, name, builder) { - container[name] = extendErrorType(base, name, builder) -} - -function defineErrorTypeIn (container, name, builder) { - extendErrorTypeIn(container, Error, name, builder) -} - -OError.extend = extendErrorType -OError.define = defineErrorType -OError.extendIn = extendErrorTypeIn -OError.defineIn = defineErrorTypeIn - module.exports = OError diff --git a/libraries/o-error/test/error-type.test.js b/libraries/o-error/test/error-type.test.js deleted file mode 100644 index 9526201639..0000000000 --- a/libraries/o-error/test/error-type.test.js +++ /dev/null @@ -1,115 +0,0 @@ -'use strict' - -var OError = require('..') -const { expectError } = require('./support') - -describe('OError', function () { - it('defines a custom error type', function () { - var CustomError = OError.define('CustomError') - - function doSomethingBad () { - throw new CustomError() - } - - try { - doSomethingBad() - expect.fail('should have thrown') - } catch (e) { - expectError(e, { - name: 'CustomError', - klass: CustomError, - message: 'CustomError', - firstFrameRx: /doSomethingBad/ - }) - } - }) - - it('defines a custom error type with a message', function () { - var CustomError = OError.define('CustomError', function (x) { - this.message = 'x=' + x - this.x = x - }) - - function doSomethingBad () { - throw new CustomError('foo') - } - - try { - doSomethingBad() - expect.fail('should have thrown') - } catch (e) { - expect(e.toString()).to.equal('CustomError: x=foo') - expect(e.message).to.equal('x=foo') - expect(e.x).to.equal('foo') - } - }) - - it('defines extended error type', function () { - var BaseError = OError.define('BaseError') - var DerivedError = OError.extend(BaseError, 'DerivedError') - - function doSomethingBad () { - throw new DerivedError() - } - - try { - doSomethingBad() - expect.fail('should have thrown') - } catch (e) { - expect(e.toString()).to.equal('DerivedError') - } - }) - - it('defines error types in a container object', function () { - var SomeClass = {} - OError.defineIn(SomeClass, 'CustomError') - - function doSomethingBad () { - throw new SomeClass.CustomError() - } - - try { - doSomethingBad() - expect.fail('should have thrown') - } catch (e) { - expect(e.toString()).to.equal('CustomError') - } - }) - - it('extends error types in a container object', function () { - var SomeClass = {} - OError.defineIn(SomeClass, 'CustomError', function (payload) { - this.message = 'custom error' - this.payload = payload - }) - OError.extendIn(SomeClass, SomeClass.CustomError, 'DerivedCustomError', - function (payload) { - SomeClass.CustomError.call(this, payload) - this.message = 'derived custom error' - }) - - function doSomethingBad () { - throw new SomeClass.CustomError(123) - } - - try { - doSomethingBad() - expect.fail('should have thrown') - } catch (e) { - expect(e.toString()).to.equal('CustomError: custom error') - expect(e.payload).to.equal(123) - } - - function doSomethingBadWithDerived () { - throw new SomeClass.DerivedCustomError(456) - } - - try { - doSomethingBadWithDerived() - expect.fail('should have thrown') - } catch (e) { - expect(e.toString()).to.equal('DerivedCustomError: derived custom error') - expect(e.payload).to.equal(456) - } - }) -}) diff --git a/libraries/o-error/test/error-type-util.test.js b/libraries/o-error/test/o-error-util.test.js similarity index 100% rename from libraries/o-error/test/error-type-util.test.js rename to libraries/o-error/test/o-error-util.test.js diff --git a/libraries/o-error/test/error-type-class.test.js b/libraries/o-error/test/o-error.test.js similarity index 100% rename from libraries/o-error/test/error-type-class.test.js rename to libraries/o-error/test/o-error.test.js diff --git a/libraries/o-error/test/support/index.js b/libraries/o-error/test/support/index.js index 24cbc45270..502348acdf 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 errorTypeExpectError (e, expected) { +exports.expectError = function OErrorExpectError (e, expected) { // should set the name to the error's name expect(e.name).to.equal(expected.name)