-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathresponse.ts
62 lines (55 loc) · 1.69 KB
/
response.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
import { MSError } from '@microfleet/transport-amqp/lib/utils/serialization'
import { Microfleet } from '@microfleet/core'
import { HttpStatusError as HttpError } from '@microfleet/validation'
import { boomify } from '@hapi/boom'
import {
AuthenticationRequiredError,
ConnectionError,
Error as CError,
HttpStatusError,
NotFoundError,
NotImplementedError,
NotPermittedError,
NotSupportedError,
TimeoutError,
ValidationError
} from 'common-errors'
import { ServiceRequest } from '../../types/router'
export default async function responseHandler(this: Microfleet, request: ServiceRequest): Promise<void> {
const { error, reformatError } = request
if (error !== undefined) {
if (reformatError === true) {
switch (error.constructor) {
case AuthenticationRequiredError:
case ConnectionError:
case HttpStatusError:
case HttpError:
case NotImplementedError:
case NotFoundError:
case NotPermittedError:
case NotSupportedError:
case TimeoutError:
case ValidationError:
case CError:
throw error
}
if (error.constructor === MSError) {
switch (error.name) {
case 'AuthenticationRequiredError':
case 'ConnectionError':
case 'HttpStatusError':
case 'NotImplementedError':
case 'NotFoundError':
case 'NotPermittedError':
case 'NotSupportedError':
case 'TimeoutError':
case 'ValidationError':
throw error
}
}
this.log.fatal({ err: boomify(error) }, 'unexpected error')
throw new CError(`Something went wrong: ${error.message}`, error)
}
throw error
}
}