Remove ES5 interface

This commit is contained in:
John Lees-Miller 2019-07-08 11:52:42 +01:00
parent bdde7fac65
commit 96ab30883a
5 changed files with 1 additions and 163 deletions

View file

@ -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

View file

@ -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)
}
})
})

View file

@ -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)