fix: possible exception while logging errors
Massimo Melina committed
Apr 14, 2023 at 14:12 UTC
ed78c6e1ec9d928e9f0d9502fa430f00751e8c46
2 files changed
+8
-7
src/log.ts
+3
-4
@@ -11,7 +11,7 @@ import events from './events'
11
import _ from 'lodash'
12
import { prepareFolder } from './util-files'
13
import { getCurrentUsername } from './perm'
14
-import { makeNetMatcher } from './misc'
14
+import { makeNetMatcher, tryJson } from './misc'
15
16
class Logger {
17
stream?: Writable
@@ -127,8 +127,7 @@ debugLogFile.on('open', () => {
127
const was = console.error
128
console.error = function(...args: any[]) {
129
was.apply(this, args)
130
- const params = args.map(x =>
131
- typeof x === 'string' ? x : JSON.stringify(x)).join(' ')
132
- debugLogFile.write(new Date().toLocaleString() + ': ' + params + '\n')
130
+ args = args.map(x => typeof x === 'string' ? x : (tryJson(x) ?? String(x)))
131
+ debugLogFile.write(new Date().toLocaleString() + ': ' + args.join(' ') + '\n')
132
}
133
}).on('error', () => console.log("cannot create debug.log"))
src/util-http.ts
+5
-3
@@ -20,13 +20,15 @@ export function httpsString(url: string, options:RequestOptions={}): Promise<Inc
20
21
export function httpsStream(url: string, options:RequestOptions={}): Promise<IncomingMessage> {
22
return new Promise((resolve, reject) => {
23
- https.request(url, options, res => {
23
+ const req = https.request(url, options, res => {
24
if (!res.statusCode || res.statusCode >= 400)
25
- throw res
25
+ return reject(new Error(String(res.statusCode), { cause: res }))
26
if (res.statusCode === HTTP_TEMPORARY_REDIRECT && res.headers.location)
27
return resolve(httpsStream(res.headers.location, options))
28
resolve(res)
29
- }).on('error', reject).end()
29
+ }).on('error', e => {
30
+ reject((req as any).res || e)
31
+ }).end()
32
})
33
}
34