custom.js 1.6 KB

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