File size: 5,058 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict'

const { test } = require('tap')
const createError = require('..')

test('Create error with zero parameter', t => {
  t.plan(6)

  const NewError = createError('CODE', 'Not available')
  const err = new NewError()
  t.ok(err instanceof Error)
  t.equal(err.name, 'FastifyError')
  t.equal(err.message, 'Not available')
  t.equal(err.code, 'CODE')
  t.equal(err.statusCode, 500)
  t.ok(err.stack)
})

test('Create error with 1 parameter', t => {
  t.plan(6)

  const NewError = createError('CODE', 'hey %s')
  const err = new NewError('alice')
  t.ok(err instanceof Error)
  t.equal(err.name, 'FastifyError')
  t.equal(err.message, 'hey alice')
  t.equal(err.code, 'CODE')
  t.equal(err.statusCode, 500)
  t.ok(err.stack)
})

test('Create error with 1 parameter set to undefined', t => {
  t.plan(1)

  const NewError = createError('CODE', 'hey %s')
  const err = new NewError(undefined)
  t.equal(err.message, 'hey undefined')
})

test('Create error with 2 parameters', (t) => {
  t.plan(6)

  const NewError = createError('CODE', 'hey %s, I like your %s')
  const err = new NewError('alice', 'attitude')
  t.ok(err instanceof Error)
  t.equal(err.name, 'FastifyError')
  t.equal(err.message, 'hey alice, I like your attitude')
  t.equal(err.code, 'CODE')
  t.equal(err.statusCode, 500)
  t.ok(err.stack)
})

test('Create error with 2 parameters set to undefined', t => {
  t.plan(1)

  const NewError = createError('CODE', 'hey %s, I like your %s')
  const err = new NewError(undefined, undefined)
  t.equal(err.message, 'hey undefined, I like your undefined')
})

test('Create error with 3 parameters', t => {
  t.plan(6)

  const NewError = createError('CODE', 'hey %s, I like your %s %s')
  const err = new NewError('alice', 'attitude', 'see you')
  t.ok(err instanceof Error)
  t.equal(err.name, 'FastifyError')
  t.equal(err.message, 'hey alice, I like your attitude see you')
  t.equal(err.code, 'CODE')
  t.equal(err.statusCode, 500)
  t.ok(err.stack)
})

test('Create error with 3 parameters set to undefined', t => {
  t.plan(4)

  const NewError = createError('CODE', 'hey %s, I like your %s %s')
  const err = new NewError(undefined, undefined, undefined)
  t.equal(err.message, 'hey undefined, I like your undefined undefined')
  t.equal(err.code, 'CODE')
  t.equal(err.statusCode, 500)
  t.ok(err.stack)
})

test('Create error with 4 parameters set to undefined', t => {
  t.plan(4)

  const NewError = createError('CODE', 'hey %s, I like your %s %s and %s')
  const err = new NewError(undefined, undefined, undefined, undefined)
  t.equal(err.message, 'hey undefined, I like your undefined undefined and undefined')
  t.equal(err.code, 'CODE')
  t.equal(err.statusCode, 500)
  t.ok(err.stack)
})

test('Create error with no statusCode property', t => {
  t.plan(6)

  const NewError = createError('CODE', 'hey %s', 0)
  const err = new NewError('dude')
  t.ok(err instanceof Error)
  t.equal(err.name, 'FastifyError')
  t.equal(err.message, 'hey dude')
  t.equal(err.code, 'CODE')
  t.equal(err.statusCode, undefined)
  t.ok(err.stack)
})

test('Should throw when error code has no fastify code', t => {
  t.plan(1)

  t.throws(() => createError(), new Error('Fastify error code must not be empty'))
})

test('Should throw when error code has no message', t => {
  t.plan(1)

  t.throws(() => createError('code'), new Error('Fastify error message must not be empty'))
})

test('Create error with different base', t => {
  t.plan(7)

  const NewError = createError('CODE', 'hey %s', 500, TypeError)
  const err = new NewError('dude')
  t.ok(err instanceof Error)
  t.ok(err instanceof TypeError)
  t.equal(err.name, 'FastifyError')
  t.equal(err.message, 'hey dude')
  t.equal(err.code, 'CODE')
  t.equal(err.statusCode, 500)
  t.ok(err.stack)
})

test('FastifyError.toString returns code', t => {
  t.plan(1)

  const NewError = createError('CODE', 'foo')
  const err = new NewError()
  t.equal(err.toString(), 'FastifyError [CODE]: foo')
})

test('Create the error without the new keyword', t => {
  t.plan(6)

  const NewError = createError('CODE', 'Not available')
  const err = NewError()
  t.ok(err instanceof Error)
  t.equal(err.name, 'FastifyError')
  t.equal(err.message, 'Not available')
  t.equal(err.code, 'CODE')
  t.equal(err.statusCode, 500)
  t.ok(err.stack)
})

test('Create an error with cause', t => {
  t.plan(2)
  const cause = new Error('HEY')
  const NewError = createError('CODE', 'Not available')
  const err = NewError({ cause })

  t.ok(err instanceof Error)
  t.equal(err.cause, cause)
})

test('Create an error with cause and message', t => {
  t.plan(2)
  const cause = new Error('HEY')
  const NewError = createError('CODE', 'Not available: %s')
  const err = NewError('foo', { cause })

  t.ok(err instanceof Error)
  t.equal(err.cause, cause)
})

test('Create an error with last argument null', t => {
  t.plan(2)
  const cause = new Error('HEY')
  const NewError = createError('CODE', 'Not available')
  const err = NewError({ cause }, null)

  t.ok(err instanceof Error)
  t.notOk(err.cause)
})