Make constructor take only message and info

This commit is contained in:
John Lees-Miller 2020-04-17 09:00:28 +01:00
parent 0acf1d338d
commit c46075aaab
4 changed files with 30 additions and 20 deletions

View file

@ -1,8 +1,8 @@
const OError = require('./index') const OError = require('./index')
class HttpError extends OError { class HttpError extends OError {
constructor(options) { constructor({ message, info, ...options }) {
super(options) super(message, info)
this.statusCode = options.statusCode || 500 this.statusCode = options.statusCode || 500
} }
} }

View file

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

View file

@ -1,12 +1,22 @@
const OError = require('..')
const HttpErrors = require('../http') const HttpErrors = require('../http')
const { expectError } = require('./support')
describe('OError/http', function () { describe('OError/http', function () {
it('is instance of OError', function () { it('is a valid OError', function () {
try { function foo() {
throw new HttpErrors.ConflictError() throw new HttpErrors.ConflictError()
} catch (e) { }
expect(e).to.be.instanceof(OError)
try {
foo()
} catch (error) {
expectError(error, {
name: 'ConflictError',
klass: HttpErrors.ConflictError,
message: 'ConflictError: Conflict',
firstFrameRx: /foo/,
})
} }
}) })

View file

@ -2,14 +2,14 @@ const OError = require('..')
const { expectError } = require('./support') const { expectError } = require('./support')
class CustomError1 extends OError { class CustomError1 extends OError {
constructor(options) { constructor(info) {
super({ message: 'failed to foo', ...options }) super('failed to foo', info)
} }
} }
class CustomError2 extends OError { class CustomError2 extends OError {
constructor(options) { constructor(customMessage, info) {
super({ message: 'failed to bar', ...options }) super(customMessage || 'failed to bar', info)
} }
} }
@ -23,7 +23,7 @@ describe('OError', function () {
try { try {
doSomethingBadInternally() doSomethingBadInternally()
} catch (err) { } catch (err) {
throw new CustomError1({ info: { userId: 123 } }).withCause(err) throw new CustomError1({ userId: 123 }).withCause(err)
} }
} }
@ -55,7 +55,7 @@ describe('OError', function () {
try { try {
doSomethingBadInternally() doSomethingBadInternally()
} catch (err) { } catch (err) {
throw new CustomError2({ info: { database: 'a' } }).withCause(err) throw new CustomError2('failed to bar!', { inner: 'a' }).withCause(err)
} }
} }
@ -63,7 +63,7 @@ describe('OError', function () {
try { try {
doBar() doBar()
} catch (err) { } catch (err) {
throw new CustomError1({ info: { userId: 123 } }).withCause(err) throw new CustomError1({ userId: 123 }).withCause(err)
} }
} }
@ -74,19 +74,19 @@ describe('OError', function () {
expectError(e, { expectError(e, {
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', inner: 'a',
}) })
const fullStack = OError.getFullStack(e) const fullStack = OError.getFullStack(e)
expect(fullStack).to.match( expect(fullStack).to.match(
/^CustomError1: failed to foo: failed to bar: internal error$/m /^CustomError1: failed to foo: failed to bar!: internal error$/m
) )
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)
} }
@ -94,7 +94,7 @@ describe('OError', function () {
it('handles a custom error without info', function () { 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({})
const infoKey = Object.keys(e).find((k) => k === 'info') const infoKey = Object.keys(e).find((k) => k === 'info')