Repository navigation
Expand file tree
/
Copy pathErrorUtil.ts
More file actions
65 lines (57 loc) · 1.68 KB
/
Copy pathErrorUtil.ts
File metadata and controls
65 lines (57 loc) · 1.68 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
import fs from "fs";
import serializeError = require("serialize-error");
import { randint } from "tstl/algorithm/random";
import { Singleton } from "tstl/thread/Singleton";
import { PaymentConfiguration } from "../PaymentConfiguration";
export namespace ErrorUtil
{
export function toJSON(err: any): object
{
return (err instanceof Object && err.toJSON instanceof Function)
? err.toJSON()
: serializeError(err);
}
export async function log
(
prefix: string,
data: string | object | Error,
): Promise<void>
{
try
{
if (data instanceof Error)
data = toJSON(data);
const date: Date = new Date();
const fileName: string = `${date.getFullYear()}${cipher(date.getMonth()+1)}${cipher(date.getDate())}${cipher(date.getHours())}${cipher(date.getMinutes())}${cipher(date.getSeconds())}.${randint(0, Number.MAX_SAFE_INTEGER)}`;
const content: string = JSON.stringify(data, null, 4);
await directory.get();
await fs.promises.writeFile
(
`${PaymentConfiguration.ASSETS}/logs/errors/${prefix}_${fileName}.log`,
content,
"utf8"
);
}
catch {}
}
}
function cipher(val: number): string
{
if (val < 10)
return "0" + val;
else
return String(val);
}
const directory = new Singleton(async () =>
{
try
{
await fs.promises.mkdir(`${PaymentConfiguration.ASSETS}/logs`);
}
catch {}
try
{
await fs.promises.mkdir(`${PaymentConfiguration.ASSETS}/logs/errors`);
}
catch {}
});