-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy patherrors.spec.ts
More file actions
123 lines (112 loc) · 3.83 KB
/
Copy patherrors.spec.ts
File metadata and controls
123 lines (112 loc) · 3.83 KB
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { describe, expect, it } from 'vitest'
import { getParseError, getResourceName, processServerError } from '../errors'
describe('getParseError', () => {
it('extracts nice part of error message', () => {
expect(
getParseError('unable to parse JSON body: hi, you have an error at line 129 column 4')
).toEqual('Hi, you have an error')
})
it('returns undefined if error does not match pattern', () => {
expect(getParseError('some nonsense')).toBeUndefined()
})
})
describe('processServerError', () => {
const makeError = ({
status = 400,
errorCode = 'ObjectAlreadyExists',
message = 'already exists: instance "instance-name"',
} = {}) => ({
type: 'error' as const,
response: new Response(undefined, { status }),
data: { requestId: '2', errorCode, message },
})
it('extracts message from parse errors', () => {
const parseError = {
type: 'error' as const,
response: new Response(undefined, { status: 400 }),
data: {
requestId: '1',
message: 'unable to parse JSON body: hi, you have an error at line 129 column 4',
},
}
expect(processServerError('fakeThingView', parseError)).toEqual({
message: 'Hi, you have an error',
statusCode: 400,
errorCode: undefined,
requestId: '1',
})
})
it('handles client errors', () => {
const clientError = {
type: 'client_error' as const,
response: new Response(undefined, { status: 200 }),
text: 'this was not json',
error: new Error('failed to parse JSON'),
}
expect(processServerError('fakeThingView', clientError)).toEqual({
message: 'Error reading API response',
statusCode: 200,
requestId: undefined,
})
})
describe('ObjectAlreadyExists', () => {
it('pulls resource name from message', () => {
expect(processServerError('fakeThingCreate', makeError())).toEqual({
errorCode: 'ObjectAlreadyExists',
message: 'Instance name already exists',
statusCode: 400,
requestId: '2',
})
})
it('pulls from method name if message does not work', () => {
const error = makeError({ message: 'whatever' })
expect(processServerError('fakeThingCreate', error)).toEqual({
errorCode: 'ObjectAlreadyExists',
message: 'Thing name already exists',
statusCode: 400,
requestId: '2',
})
})
})
describe('ObjectNotFound', () => {
it('passes through the API error', () => {
const error = makeError({
errorCode: 'ObjectNotFound',
message: 'not found: whatever',
status: 404,
})
expect(processServerError('fakeThingCreate', error)).toEqual({
errorCode: 'ObjectNotFound',
message: 'Not found: whatever',
statusCode: 404,
requestId: '2',
})
})
})
it('falls back to server error message if code not found', () => {
const error = makeError({ errorCode: 'WeirdError', message: 'whatever' })
expect(processServerError('womp', error)).toEqual({
errorCode: 'WeirdError',
message: 'Whatever',
statusCode: 400,
requestId: '2',
})
})
})
it.each([
['projectCreate', '', 'project'],
['projectCreate', 'already exists: project "abc"', 'project'],
['instanceCreate', 'already exists: disk "abc"', 'disk'],
['instanceNetworkInterfaceCreate', '', 'interface'],
['instanceNetworkInterfaceCreate', 'already exists: something else', 'something else'],
['doesNotContainC-reate', '', null],
])('getResourceName: %s', (method, message, resource) => {
expect(getResourceName(method, message)).toEqual(resource)
})