@stacksjs/error-handling 0.58.47 → 0.58.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -6
- package/src/handler.ts +51 -0
- package/src/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/error-handling",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.58.
|
|
4
|
+
"version": "0.58.49",
|
|
5
5
|
"description": "Type safe error handling.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
],
|
|
39
39
|
"files": [
|
|
40
40
|
"README.md",
|
|
41
|
-
"dist"
|
|
41
|
+
"dist",
|
|
42
|
+
"src"
|
|
42
43
|
],
|
|
43
44
|
"scripts": {
|
|
44
45
|
"build": "bun --bun build.ts",
|
|
@@ -46,9 +47,9 @@
|
|
|
46
47
|
"prepublishOnly": "bun --bun run build"
|
|
47
48
|
},
|
|
48
49
|
"peerDependencies": {
|
|
49
|
-
"@stacksjs/cli": "
|
|
50
|
-
"@stacksjs/path": "
|
|
51
|
-
"@stacksjs/types": "
|
|
50
|
+
"@stacksjs/cli": "latest",
|
|
51
|
+
"@stacksjs/path": "latest",
|
|
52
|
+
"@stacksjs/types": "latest"
|
|
52
53
|
},
|
|
53
54
|
"dependencies": {
|
|
54
55
|
"@stacksjs/cli": "latest",
|
|
@@ -57,6 +58,6 @@
|
|
|
57
58
|
"neverthrow": "^6.1.0"
|
|
58
59
|
},
|
|
59
60
|
"devDependencies": {
|
|
60
|
-
"@stacksjs/development": "
|
|
61
|
+
"@stacksjs/development": "latest"
|
|
61
62
|
}
|
|
62
63
|
}
|
package/src/handler.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { logsPath } from '@stacksjs/path'
|
|
2
|
+
|
|
3
|
+
interface ErrorOptions {
|
|
4
|
+
silent?: boolean
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const StacksError = Error
|
|
8
|
+
|
|
9
|
+
export class ErrorHandler {
|
|
10
|
+
static logFile = logsPath('errors.log')
|
|
11
|
+
|
|
12
|
+
static handle(err: ErrorDescription | Error, options?: ErrorOptions | Error) {
|
|
13
|
+
// lets only write to the console if we are not in silent mode
|
|
14
|
+
if (!(options instanceof Error) && options?.silent !== false)
|
|
15
|
+
this.writeErrorToConsole(err, options)
|
|
16
|
+
|
|
17
|
+
if (typeof err === 'string')
|
|
18
|
+
err = new StacksError(err)
|
|
19
|
+
|
|
20
|
+
this.writeErrorToFile(err).catch(e => console.error(e))
|
|
21
|
+
|
|
22
|
+
return err
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static handleError(err: Error, options?: ErrorOptions) {
|
|
26
|
+
this.handle(err, options)
|
|
27
|
+
return err
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static async writeErrorToFile(err: Error) {
|
|
31
|
+
const formattedError = `[${new Date().toISOString()}] ${err.name}: ${err.message}\n`
|
|
32
|
+
const file = Bun.file(this.logFile)
|
|
33
|
+
const writer = file.writer()
|
|
34
|
+
const text = await file.text()
|
|
35
|
+
writer.write(`${text}\n`)
|
|
36
|
+
writer.write(`${formattedError}\n`)
|
|
37
|
+
await writer.end()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static writeErrorToConsole(err: string | Error, options?: ErrorOptions) {
|
|
41
|
+
if (options)
|
|
42
|
+
console.error(err, options)
|
|
43
|
+
else
|
|
44
|
+
console.error(err)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type ErrorDescription = string
|
|
49
|
+
export function handleError(err: ErrorDescription | Error, options?: ErrorOptions | Error): Error {
|
|
50
|
+
return ErrorHandler.handle(err, options)
|
|
51
|
+
}
|
package/src/index.ts
ADDED