test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var test = require('tape')
  2. , inherits = require('inherits')
  3. , ErrorStackParser = require('error-stack-parser')
  4. , errno = require('./')
  5. test('sanity checks', function (t) {
  6. t.ok(errno.all, 'errno.all not found')
  7. t.ok(errno.errno, 'errno.errno not found')
  8. t.ok(errno.code, 'errno.code not found')
  9. t.equal(errno.all.length, 60, 'found ' + errno.all.length + ', expected 60')
  10. t.equal(errno.errno['-1'], errno.all[1], 'errno -1 not second element')
  11. t.equal(errno.code['UNKNOWN'], errno.all[1], 'code UNKNOWN not second element')
  12. t.equal(errno.errno[1], errno.all[3], 'errno 1 not fourth element')
  13. t.equal(errno.code['EOF'], errno.all[3], 'code EOF not fourth element')
  14. t.end()
  15. })
  16. test('custom errors', function (t) {
  17. const Cust = errno.create('FooNotBarError')
  18. const cust = new Cust('foo is not bar')
  19. t.equal(cust.name, 'FooNotBarError', 'correct custom name')
  20. t.equal(cust.type, 'FooNotBarError', 'correct custom type')
  21. t.equal(cust.message, 'foo is not bar', 'correct custom message')
  22. t.notOk(cust.cause, 'no cause')
  23. t.end()
  24. })
  25. test('callstack', function (t) {
  26. const MyError = errno.create('MyError')
  27. function lastFunction (ErrorType, cb) {
  28. process.nextTick(cb, new ErrorType('oh noes!'))
  29. }
  30. function secondLastFunction (ErrorType, cb) {
  31. lastFunction(ErrorType, cb)
  32. }
  33. function testFrames (t) {
  34. return function (err) {
  35. const stack = ErrorStackParser.parse(err)
  36. t.same(stack[0].functionName, 'lastFunction', 'last stack frame ok')
  37. t.same(stack[1].functionName, 'secondLastFunction', 'second last stack frame ok')
  38. t.end()
  39. }
  40. }
  41. t.test('custom error, default prototype', function (t) {
  42. secondLastFunction(MyError, testFrames(t))
  43. })
  44. t.test('custom error, custom prototype', function (t) {
  45. const MyError2 = errno.create('MyError2', MyError)
  46. secondLastFunction(MyError2, testFrames(t))
  47. })
  48. t.test('custom error, using inheritance', function (t) {
  49. const CustomError = errno.custom.CustomError
  50. function MyError3 (message, cause) {
  51. CustomError.call(this, message, cause)
  52. }
  53. inherits(MyError3, CustomError)
  54. secondLastFunction(MyError3, testFrames(t))
  55. })
  56. })
  57. test('error without message', function (t) {
  58. const Cust = errno.create('WriteError')
  59. const cust = new Cust({
  60. code: 22,
  61. message: '',
  62. name: 'QuotaExceededError'
  63. })
  64. t.equal(cust.name, 'WriteError', 'correct custom name')
  65. t.equal(cust.type, 'WriteError', 'correct custom type')
  66. t.equal(cust.message, 'QuotaExceededError', 'message is the name')
  67. t.notOk(cust.cause, 'no cause')
  68. t.end()
  69. })