overleaf/libraries/o-error/index.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-03-07 11:42:39 -05:00
'use strict'
var util = require('util')
/**
2018-04-21 18:24:23 -04:00
* Make custom error types that pass `instanceof` checks, have stack traces and
* support custom messages and properties.
2018-03-07 11:42:39 -05:00
* @module
*/
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)
}
exports.extend = extendErrorType
exports.define = defineErrorType
exports.extendIn = extendErrorTypeIn
exports.defineIn = defineErrorTypeIn