Blame view

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