-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlib.error-handler.spec.ts
91 lines (83 loc) · 2.34 KB
/
lib.error-handler.spec.ts
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
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { errorHandler, HTTPError } from '../src/lib/core.js'
import { throwsAsync } from './utils/throws-async.js'
describe('lib/error-handler', function () {
it('should parse json error response', async function () {
const res: Response = {
ok: false,
statusText: 'test',
headers: new Headers({
'Content-Type': 'application/json'
}),
json: async () => Promise.resolve({
Message: 'boom',
Code: 0,
Type: 'error'
}),
status: 500,
redirected: false,
url: '',
type: 'basic',
clone: (): any => {},
body: null,
bodyUsed: false,
arrayBuffer: (): any => {},
blob: (): any => {},
formData: (): any => {},
text: (): any => {}
}
const err = await throwsAsync<any>(async () => {
await errorHandler(res)
})
expect(err instanceof HTTPError).to.be.true()
expect(err.message).to.eql('boom')
expect(err.response.status).to.eql(500)
})
it('should gracefully fail on parse json', async function () {
const res: Response = {
ok: false,
statusText: 'test',
headers: new Headers({
'Content-Type': 'application/json'
}),
json: async () => 'boom', // not valid json!
status: 500,
redirected: false,
url: '',
type: 'basic',
clone: (): any => {},
body: null,
bodyUsed: false,
arrayBuffer: (): any => {},
blob: (): any => {},
formData: (): any => {},
text: (): any => {}
}
const err = await throwsAsync<any>(errorHandler(res))
expect(err instanceof HTTPError).to.be.true()
})
it('should gracefully fail on read text', async function () {
const res: Response = {
ok: false,
statusText: 'test',
headers: new Headers({
'Content-Type': 'application/json'
}),
text: async () => Promise.reject(new Error('boom')),
status: 500,
json: async () => 'boom', // not valid json!
redirected: false,
url: '',
type: 'basic',
clone: (): any => {},
body: null,
bodyUsed: false,
arrayBuffer: (): any => {},
blob: (): any => {},
formData: (): any => {}
}
const err = await throwsAsync<any>(errorHandler(res))
expect(err instanceof HTTPError).to.be.true()
})
})