-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspeechflow-util-error.ts
More file actions
199 lines (190 loc) · 6.08 KB
/
speechflow-util-error.ts
File metadata and controls
199 lines (190 loc) · 6.08 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
** SpeechFlow - Speech Processing Flow Graph
** Copyright (c) 2024-2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
*/
/* helper function for retrieving an Error object */
export function ensureError (error: unknown, prefix?: string, debug = false): Error {
if (error instanceof Error && prefix === undefined && debug === false)
return error
let msg = error instanceof Error ?
error.message : String(error)
if (prefix)
msg = `${prefix}: ${msg}`
if (debug && error instanceof Error)
msg = `${msg}\n${error.stack}`
if (error instanceof Error) {
const err = new Error(msg, { cause: error })
err.stack = error.stack
return err
}
else
return new Error(msg)
}
/* helper function for retrieving a Promise object */
export function ensurePromise<T> (arg: T | Promise<T>): Promise<T> {
if (!(arg instanceof Promise))
arg = Promise.resolve(arg)
return arg
}
/* helper function for running the finally code of "run" */
function runFinally (onfinally?: () => void) {
if (!onfinally)
return
try { onfinally() }
catch (_error: unknown) { /* ignored */ }
}
/* helper type for ensuring T contains no Promise */
type runNoPromise<T> =
[ T ] extends [ Promise<any> ] ? never : T
/* run a synchronous or asynchronous action */
export function run<T, X extends runNoPromise<T> | never> (
action: () => X,
oncatch?: (error: Error) => X | never,
onfinally?: () => void
): X
export function run<T, X extends runNoPromise<T> | never> (
description: string,
action: () => X,
oncatch?: (error: Error) => X | never,
onfinally?: () => void
): X
export function run<T, X extends (T | Promise<T>)> (
action: () => X,
oncatch?: (error: Error) => X,
onfinally?: () => void
): Promise<T>
export function run<T, X extends (T | Promise<T>)> (
description: string,
action: () => X,
oncatch?: (error: Error) => X,
onfinally?: () => void
): Promise<T>
export function run<T> (
...args: any[]
): T | Promise<T> | never {
/* support overloaded signatures */
let description: string | undefined
let action: () => T | Promise<T> | never
let oncatch: (error: Error) => T | Promise<T> | never
let onfinally: () => void
if (typeof args[0] === "string") {
description = args[0]
action = args[1]
oncatch = args[2]
onfinally = args[3]
}
else {
action = args[0]
oncatch = args[1]
onfinally = args[2]
}
/* perform the action */
let result: T | Promise<T>
try {
result = action()
}
catch (arg: unknown) {
/* synchronous case (error branch) */
let error = ensureError(arg, description)
if (oncatch) {
try {
result = oncatch(error)
}
catch (arg: unknown) {
error = ensureError(arg, description)
runFinally(onfinally)
throw error
}
runFinally(onfinally)
return result
}
runFinally(onfinally)
throw error
}
if (result instanceof Promise) {
/* asynchronous case (result or error branch) */
return result.catch((arg: unknown) => {
/* asynchronous case (error branch) */
let error = ensureError(arg, description)
if (oncatch) {
try {
return oncatch(error)
}
catch (arg: unknown) {
error = ensureError(arg, description)
throw error
}
}
throw error
}).finally(() => {
/* asynchronous case (result and error branch) */
runFinally(onfinally)
})
}
else {
/* synchronous case (result branch) */
runFinally(onfinally)
return result
}
}
/* run a synchronous or asynchronous action */
/* eslint @typescript-eslint/unified-signatures: off */
export function runner<T, X extends runNoPromise<T> | never, F extends (...args: any[]) => X> (
action: F,
oncatch?: (error: Error) => X | never,
onfinally?: () => void
): F
export function runner<T, X extends runNoPromise<T> | never, F extends (...args: any[]) => X> (
description: string,
action: F,
oncatch?: (error: Error) => X | never,
onfinally?: () => void
): F
export function runner<T, X extends (T | Promise<T>), F extends (...args: any[]) => Promise<T>> (
action: F,
oncatch?: (error: Error) => X,
onfinally?: () => void
): F
export function runner<T, X extends (T | Promise<T>), F extends (...args: any[]) => Promise<T>> (
description: string,
action: F,
oncatch?: (error: Error) => X,
onfinally?: () => void
): F
export function runner<T> (
...args: any[]
): (...args: any[]) => T | Promise<T> | never {
/* support overloaded signatures */
let description: string | undefined
let action: (...args: any[]) => T | Promise<T> | never
let oncatch: (error: Error) => T | Promise<T> | never
let onfinally: () => void
if (typeof args[0] === "string") {
description = args[0]
action = args[1]
oncatch = args[2]
onfinally = args[3]
}
else {
action = args[0]
oncatch = args[1]
onfinally = args[2]
}
/* wrap the "run" operation on "action" into function
which exposes the signature of "action" */
return (...args: any[]) => {
if (description)
return run(description, () => action(...args), oncatch, onfinally)
else
return run(() => action(...args), oncatch, onfinally)
}
}
/* shield cleanup operation, ignoring errors */
export function shield<T extends (void | Promise<void>)> (op: () => T) {
return run(
"shielded operation",
() => op(),
(_err) => undefined as T
)
}