-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathinspector.ts
124 lines (105 loc) · 3.54 KB
/
inspector.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
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
124
/* eslint-disable n/prefer-global/process */
/* eslint-disable no-console */
import { inspect as nodeInspect } from 'node:util'
import type { logLevel } from './types'
const errorCodes = {
FORBIDDEN: 401,
BAD_USER_INPUT: 400,
INTERNAL_SERVER_ERROR: 500,
TOO_MANY_REQUESTS: 429,
}
export type ErrorExtensions = {
type: keyof typeof errorCodes
cause?: any
}
export type ErrorDetails = {
error: string
type: ErrorExtensions['type']
code: number
cause?: ErrorExtensions['cause']
}
export class CustomError extends Error {
public error: ErrorDetails['error']
public type: ErrorDetails['type']
public code: ErrorDetails['code']
public cause: ErrorDetails['cause']
public details: ErrorDetails
constructor(message: string, extensions: ErrorExtensions) {
super(message)
this.error = message
this.type = extensions.type
this.cause = extensions?.cause?.meta?.cause || extensions?.cause
this.code = typeof errorCodes[this.type] !== 'undefined' ? errorCodes[this.type] : errorCodes.INTERNAL_SERVER_ERROR
this.message = JSON.stringify({
error: this.error,
type: this.type,
code: this.code,
})
const maxCauseMessageLength = 500
if (this.cause?.message?.length > maxCauseMessageLength)
this.cause.message = `... ${this.cause.message.slice(this.cause.message.length - maxCauseMessageLength)}`
this.details = {
error: this.error,
type: this.type,
code: this.code,
...(this.cause && { cause: this.cause }),
}
if (!(process?.env?.PRISMA_APPSYNC_TESTING === 'true'))
log(message, this.details, 'ERROR')
}
}
export function parseError(error: Error): CustomError {
if (error instanceof CustomError) {
return error
}
else {
return new CustomError(error.message, {
type: 'INTERNAL_SERVER_ERROR',
cause: error,
})
}
}
export function log(message: string, obj?: any, level?: logLevel): void {
if (canPrintLog(level || 'INFO')) {
printLog(message, level || 'INFO')
if (obj) {
console.log(
nodeInspect(obj, {
compact: false,
depth: 5,
breakLength: 80,
maxStringLength: 800,
...(!process.env.LAMBDA_TASK_ROOT && {
colors: true,
}),
}),
)
}
}
}
export function printLog(message: any, level: logLevel): void {
const timestamp = new Date().toLocaleString(undefined, {
day: 'numeric',
month: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
const prefix = `◭ ${timestamp} <<${level}>>`
const log = [prefix, message].join(' ')
if (level === 'ERROR' && canPrintLog(level))
console.error(`\x1B[31m${log}`)
else if (level === 'WARN' && canPrintLog(level))
console.warn(`\x1B[33m${log}`)
else if (level === 'INFO' && canPrintLog(level))
console.info(`\x1B[36m${log}`)
}
function canPrintLog(level: logLevel): boolean {
if (process?.env?.PRISMA_APPSYNC_TESTING === 'true')
return false
const logLevel = String(process.env.PRISMA_APPSYNC_LOG_LEVEL) as logLevel
return (logLevel === 'ERROR' && level === 'ERROR')
|| (logLevel === 'WARN' && ['WARN', 'ERROR'].includes(level))
|| (logLevel === 'INFO')
}