|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* eslint no-prototype-builtins: 0 */ |
| 4 | + |
| 5 | +const test = require('tap').test |
| 6 | +const createError = require('..') |
| 7 | + |
| 8 | +test('Create error with zero parameter', t => { |
| 9 | + t.plan(6) |
| 10 | + |
| 11 | + const NewError = createError('CODE', 'Not available') |
| 12 | + const err = new NewError() |
| 13 | + t.ok(err instanceof Error) |
| 14 | + t.equal(err.name, 'FastifyError') |
| 15 | + t.equal(err.message, 'Not available') |
| 16 | + t.equal(err.code, 'CODE') |
| 17 | + t.equal(err.statusCode, 500) |
| 18 | + t.ok(err.stack) |
| 19 | +}) |
| 20 | + |
| 21 | +test('Create error with 1 parameter', t => { |
| 22 | + t.plan(6) |
| 23 | + |
| 24 | + const NewError = createError('CODE', 'hey %s') |
| 25 | + const err = new NewError('alice') |
| 26 | + t.ok(err instanceof Error) |
| 27 | + t.equal(err.name, 'FastifyError') |
| 28 | + t.equal(err.message, 'hey alice') |
| 29 | + t.equal(err.code, 'CODE') |
| 30 | + t.equal(err.statusCode, 500) |
| 31 | + t.ok(err.stack) |
| 32 | +}) |
| 33 | + |
| 34 | +test('Create error with 1 parameter set to undefined', t => { |
| 35 | + t.plan(1) |
| 36 | + |
| 37 | + const NewError = createError('CODE', 'hey %s') |
| 38 | + const err = new NewError(undefined) |
| 39 | + t.equal(err.message, 'hey undefined') |
| 40 | +}) |
| 41 | + |
| 42 | +test('Create error with 2 parameters', (t) => { |
| 43 | + t.plan(6) |
| 44 | + |
| 45 | + const NewError = createError('CODE', 'hey %s, I like your %s') |
| 46 | + const err = new NewError('alice', 'attitude') |
| 47 | + t.ok(err instanceof Error) |
| 48 | + t.equal(err.name, 'FastifyError') |
| 49 | + t.equal(err.message, 'hey alice, I like your attitude') |
| 50 | + t.equal(err.code, 'CODE') |
| 51 | + t.equal(err.statusCode, 500) |
| 52 | + t.ok(err.stack) |
| 53 | +}) |
| 54 | + |
| 55 | +test('Create error with 2 parameters set to undefined', t => { |
| 56 | + t.plan(1) |
| 57 | + |
| 58 | + const NewError = createError('CODE', 'hey %s, I like your %s') |
| 59 | + const err = new NewError(undefined, undefined) |
| 60 | + t.equal(err.message, 'hey undefined, I like your undefined') |
| 61 | +}) |
| 62 | + |
| 63 | +test('Create error with 3 parameters', t => { |
| 64 | + t.plan(6) |
| 65 | + |
| 66 | + const NewError = createError('CODE', 'hey %s, I like your %s %s') |
| 67 | + const err = new NewError('alice', 'attitude', 'see you') |
| 68 | + t.ok(err instanceof Error) |
| 69 | + t.equal(err.name, 'FastifyError') |
| 70 | + t.equal(err.message, 'hey alice, I like your attitude see you') |
| 71 | + t.equal(err.code, 'CODE') |
| 72 | + t.equal(err.statusCode, 500) |
| 73 | + t.ok(err.stack) |
| 74 | +}) |
| 75 | + |
| 76 | +test('Create error with 3 parameters set to undefined', t => { |
| 77 | + t.plan(4) |
| 78 | + |
| 79 | + const NewError = createError('CODE', 'hey %s, I like your %s %s') |
| 80 | + const err = new NewError(undefined, undefined, undefined) |
| 81 | + t.equal(err.message, 'hey undefined, I like your undefined undefined') |
| 82 | + t.equal(err.code, 'CODE') |
| 83 | + t.equal(err.statusCode, 500) |
| 84 | + t.ok(err.stack) |
| 85 | +}) |
| 86 | + |
| 87 | +test('Create error with 4 parameters set to undefined', t => { |
| 88 | + t.plan(4) |
| 89 | + |
| 90 | + const NewError = createError('CODE', 'hey %s, I like your %s %s and %s') |
| 91 | + const err = new NewError(undefined, undefined, undefined, undefined) |
| 92 | + t.equal(err.message, 'hey undefined, I like your undefined undefined and undefined') |
| 93 | + t.equal(err.code, 'CODE') |
| 94 | + t.equal(err.statusCode, 500) |
| 95 | + t.ok(err.stack) |
| 96 | +}) |
| 97 | + |
| 98 | +test('Create error with no statusCode property', t => { |
| 99 | + t.plan(6) |
| 100 | + |
| 101 | + const NewError = createError('CODE', 'hey %s', 0) |
| 102 | + const err = new NewError('dude') |
| 103 | + t.ok(err instanceof Error) |
| 104 | + t.equal(err.name, 'FastifyError') |
| 105 | + t.equal(err.message, 'hey dude') |
| 106 | + t.equal(err.code, 'CODE') |
| 107 | + t.equal(err.statusCode, undefined) |
| 108 | + t.ok(err.stack) |
| 109 | +}) |
| 110 | + |
| 111 | +test('Should throw when error code has no fastify code', t => { |
| 112 | + t.plan(1) |
| 113 | + |
| 114 | + t.throws(() => createError(), 'Fastify error code must not be empty') |
| 115 | +}) |
| 116 | + |
| 117 | +test('Should throw when error code has no message', t => { |
| 118 | + t.plan(1) |
| 119 | + |
| 120 | + t.throws(() => createError('code'), 'Fastify error message must not be empty') |
| 121 | +}) |
| 122 | + |
| 123 | +test('Create error with different base', t => { |
| 124 | + t.plan(7) |
| 125 | + |
| 126 | + const NewError = createError('CODE', 'hey %s', 500, TypeError) |
| 127 | + const err = new NewError('dude') |
| 128 | + t.ok(err instanceof Error) |
| 129 | + t.ok(err instanceof TypeError) |
| 130 | + t.equal(err.name, 'FastifyError') |
| 131 | + t.equal(err.message, 'hey dude') |
| 132 | + t.equal(err.code, 'CODE') |
| 133 | + t.equal(err.statusCode, 500) |
| 134 | + t.ok(err.stack) |
| 135 | +}) |
| 136 | + |
| 137 | +test('FastifyError.toString returns code', t => { |
| 138 | + t.plan(1) |
| 139 | + |
| 140 | + const NewError = createError('CODE', 'foo') |
| 141 | + const err = new NewError() |
| 142 | + t.equal(err.toString(), 'FastifyError [CODE]: foo') |
| 143 | +}) |
| 144 | + |
| 145 | +test('Create the error without the new keyword', t => { |
| 146 | + t.plan(6) |
| 147 | + |
| 148 | + const NewError = createError('CODE', 'Not available') |
| 149 | + const err = NewError() |
| 150 | + t.ok(err instanceof Error) |
| 151 | + t.equal(err.name, 'FastifyError') |
| 152 | + t.equal(err.message, 'Not available') |
| 153 | + t.equal(err.code, 'CODE') |
| 154 | + t.equal(err.statusCode, 500) |
| 155 | + t.ok(err.stack) |
| 156 | +}) |
0 commit comments