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