[6a3a178] | 1 | var prr = require('prr')
|
---|
| 2 |
|
---|
| 3 | function init (type, message, cause) {
|
---|
| 4 | if (!!message && typeof message != 'string') {
|
---|
| 5 | message = message.message || message.name
|
---|
| 6 | }
|
---|
| 7 | prr(this, {
|
---|
| 8 | type : type
|
---|
| 9 | , name : type
|
---|
| 10 | // can be passed just a 'cause'
|
---|
| 11 | , cause : typeof message != 'string' ? message : cause
|
---|
| 12 | , message : message
|
---|
| 13 | }, 'ewr')
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | // generic prototype, not intended to be actually used - helpful for `instanceof`
|
---|
| 17 | function CustomError (message, cause) {
|
---|
| 18 | Error.call(this)
|
---|
| 19 | if (Error.captureStackTrace)
|
---|
| 20 | Error.captureStackTrace(this, this.constructor)
|
---|
| 21 | init.call(this, 'CustomError', message, cause)
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | CustomError.prototype = new Error()
|
---|
| 25 |
|
---|
| 26 | function createError (errno, type, proto) {
|
---|
| 27 | var err = function (message, cause) {
|
---|
| 28 | init.call(this, type, message, cause)
|
---|
| 29 | //TODO: the specificity here is stupid, errno should be available everywhere
|
---|
| 30 | if (type == 'FilesystemError') {
|
---|
| 31 | this.code = this.cause.code
|
---|
| 32 | this.path = this.cause.path
|
---|
| 33 | this.errno = this.cause.errno
|
---|
| 34 | this.message =
|
---|
| 35 | (errno.errno[this.cause.errno]
|
---|
| 36 | ? errno.errno[this.cause.errno].description
|
---|
| 37 | : this.cause.message)
|
---|
| 38 | + (this.cause.path ? ' [' + this.cause.path + ']' : '')
|
---|
| 39 | }
|
---|
| 40 | Error.call(this)
|
---|
| 41 | if (Error.captureStackTrace)
|
---|
| 42 | Error.captureStackTrace(this, err)
|
---|
| 43 | }
|
---|
| 44 | err.prototype = !!proto ? new proto() : new CustomError()
|
---|
| 45 | return err
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | module.exports = function (errno) {
|
---|
| 49 | var ce = function (type, proto) {
|
---|
| 50 | return createError(errno, type, proto)
|
---|
| 51 | }
|
---|
| 52 | return {
|
---|
| 53 | CustomError : CustomError
|
---|
| 54 | , FilesystemError : ce('FilesystemError')
|
---|
| 55 | , createError : ce
|
---|
| 56 | }
|
---|
| 57 | }
|
---|