-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathutils.ts
More file actions
75 lines (67 loc) · 2.23 KB
/
utils.ts
File metadata and controls
75 lines (67 loc) · 2.23 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
import type { FeathersError } from '@feathersjs/errors'
import {
BadRequest,
Forbidden,
GeneralError,
NotFound,
Timeout,
Unavailable,
} from '@feathersjs/errors'
import type { BaseError } from 'sequelize'
export const ERROR = Symbol('feathers-sequelize/error')
const wrap = (error: FeathersError, original: BaseError) =>
Object.assign(error, { [ERROR]: original })
export const errorHandler = (error: any) => {
const { name, message } = error
if (name.startsWith('Sequelize')) {
switch (name) {
case 'SequelizeValidationError':
case 'SequelizeUniqueConstraintError':
case 'SequelizeExclusionConstraintError':
case 'SequelizeForeignKeyConstraintError':
case 'SequelizeInvalidConnectionError':
throw wrap(new BadRequest(message, { errors: error.errors }), error)
case 'SequelizeTimeoutError':
case 'SequelizeConnectionTimedOutError':
throw wrap(new Timeout(message), error)
case 'SequelizeConnectionRefusedError':
case 'SequelizeAccessDeniedError':
throw wrap(new Forbidden(message), error)
case 'SequelizeHostNotReachableError':
throw wrap(new Unavailable(message), error)
case 'SequelizeHostNotFoundError':
throw wrap(new NotFound(message), error)
default:
throw wrap(new GeneralError(message), error)
}
}
throw error
}
export const getOrder = (sort: Record<string, any> = {}): [string, string][] =>
Object.keys(sort).reduce(
(order, name) => {
let direction: 'ASC' | 'DESC' | 'ASC NULLS FIRST' | 'DESC NULLS LAST'
if (Array.isArray(sort[name])) {
direction = parseInt(sort[name][0], 10) === 1 ? 'ASC' : 'DESC'
direction +=
parseInt(sort[name][1], 10) === 1 ? ' NULLS FIRST' : ' NULLS LAST'
} else {
direction = parseInt(sort[name], 10) === 1 ? 'ASC' : 'DESC'
}
order.push([name, direction])
return order
},
[] as [string, string][],
)
export const isPlainObject = (obj: any): boolean => {
return !!obj && obj.constructor === {}.constructor
}
export const isPresent = (obj: any): boolean => {
if (Array.isArray(obj)) {
return obj.length > 0
}
if (isPlainObject(obj)) {
return Object.keys(obj).length > 0
}
return !!obj
}