1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 | const { createError } = require('../../lib/errors')
|
6 |
|
7 | test('Create error with zero parameter', t => {
|
8 | t.plan(6)
|
9 | const NewError = createError('CODE', 'Not available')
|
10 | const err = new NewError()
|
11 | t.type(err, Error)
|
12 | t.equal(err.name, 'FastifyError [CODE]')
|
13 | t.equal(err.message, 'CODE: Not available')
|
14 | t.equal(err.code, 'CODE')
|
15 | t.equal(err.statusCode, 500)
|
16 | t.ok(err.stack)
|
17 | })
|
18 |
|
19 | test('Create error with 1 parameter', t => {
|
20 | t.plan(6)
|
21 | const NewError = createError('CODE', 'hey %s')
|
22 | const err = new NewError('alice')
|
23 | t.type(err, Error)
|
24 | t.equal(err.name, 'FastifyError [CODE]')
|
25 | t.equal(err.message, 'CODE: hey alice')
|
26 | t.equal(err.code, 'CODE')
|
27 | t.equal(err.statusCode, 500)
|
28 | t.ok(err.stack)
|
29 | })
|
30 |
|
31 | test('Create error with 2 parameters', t => {
|
32 | t.plan(6)
|
33 | const NewError = createError('CODE', 'hey %s, I like your %s')
|
34 | const err = new NewError('alice', 'attitude')
|
35 | t.type(err, Error)
|
36 | t.equal(err.name, 'FastifyError [CODE]')
|
37 | t.equal(err.message, 'CODE: hey alice, I like your attitude')
|
38 | t.equal(err.code, 'CODE')
|
39 | t.equal(err.statusCode, 500)
|
40 | t.ok(err.stack)
|
41 | })
|
42 |
|
43 | test('Create error with 3 parameters', t => {
|
44 | t.plan(6)
|
45 | const NewError = createError('CODE', 'hey %s, I like your %s %s')
|
46 | const err = new NewError('alice', 'attitude', 'see you')
|
47 | t.type(err, Error)
|
48 | t.equal(err.name, 'FastifyError [CODE]')
|
49 | t.equal(err.message, 'CODE: hey alice, I like your attitude see you')
|
50 | t.equal(err.code, 'CODE')
|
51 | t.equal(err.statusCode, 500)
|
52 | t.ok(err.stack)
|
53 | })
|
54 |
|
55 | test('Create error with no statusCode property', t => {
|
56 | t.plan(6)
|
57 | const NewError = createError('CODE', 'hey %s', 0)
|
58 | const err = new NewError('dude')
|
59 | t.type(err, Error)
|
60 | t.equal(err.name, 'FastifyError [CODE]')
|
61 | t.equal(err.message, 'CODE: hey dude')
|
62 | t.equal(err.code, 'CODE')
|
63 | t.notOk(err.statusCode)
|
64 | t.ok(err.stack)
|
65 | })
|
66 |
|
67 | test('Should throw when error code has no fastify code', t => {
|
68 | t.plan(1)
|
69 | try {
|
70 | createError()
|
71 | } catch (err) {
|
72 | t.equal(err.message, 'Fastify error code must not be empty')
|
73 | }
|
74 | })
|
75 |
|
76 | test('Should throw when error code has no message', t => {
|
77 | t.plan(1)
|
78 | try {
|
79 | createError('code')
|
80 | } catch (err) {
|
81 | t.equal(err.message, 'Fastify error message must not be empty')
|
82 | }
|
83 | })
|
84 |
|
85 | test('Create error with different base', t => {
|
86 | t.plan(7)
|
87 | const NewError = createError('CODE', 'hey %s', 500, TypeError)
|
88 | const err = new NewError('dude')
|
89 | t.type(err, Error)
|
90 | t.type(err, TypeError)
|
91 | t.equal(err.name, 'FastifyError [CODE]')
|
92 | t.equal(err.message, 'CODE: hey dude')
|
93 | t.equal(err.code, 'CODE')
|
94 | t.equal(err.statusCode, 500)
|
95 | t.ok(err.stack)
|
96 | })
|
97 |
|
98 | test('Error has appropriate string tag', t => {
|
99 | t.plan(1)
|
100 | const NewError = createError('CODE', 'foo')
|
101 | const err = new NewError()
|
102 | const str = Object.prototype.toString.call(err)
|
103 | t.equal(str, '[object Error]')
|
104 | })
|