@stacksjs/error-handling 0.66.0 → 0.68.0

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/src/handler.ts DELETED
@@ -1,148 +0,0 @@
1
- import type { ErrorOptions } from '@stacksjs/logging'
2
- import { access, appendFile, mkdir } from 'node:fs/promises'
3
- import { dirname } from 'node:path'
4
- import process from 'node:process'
5
- import { italic, stripAnsi } from '@stacksjs/cli'
6
- import { config } from '@stacksjs/config'
7
- import * as path from '@stacksjs/path'
8
- import { ExitCode } from '@stacksjs/types'
9
- import { isString } from '@stacksjs/validation'
10
-
11
- type ErrorMessage = string
12
-
13
- export class ErrorHandler {
14
- static isTestEnvironment = false
15
- static shouldExitProcess = true
16
-
17
- static handle(err: Error | ErrorMessage | unknown, options?: ErrorOptions): Error {
18
- this.shouldExitProcess = options?.shouldExit !== false
19
- if (options?.silent !== true)
20
- this.writeErrorToConsole(err)
21
-
22
- let errorMessage: string
23
-
24
- if (options?.message) {
25
- // Use the message from options if provided
26
- errorMessage = options.message
27
- }
28
- else if (err instanceof Error) {
29
- errorMessage = err.message
30
- }
31
- else if (typeof err === 'string') {
32
- errorMessage = err
33
- }
34
- else {
35
- errorMessage = JSON.stringify(err)
36
- }
37
-
38
- // Create a new Error with the determined message
39
- const error = new Error(errorMessage)
40
-
41
- // If the original err was an Error instance, copy its properties
42
- if (err instanceof Error) {
43
- Object.assign(error, err)
44
- }
45
-
46
- this.writeErrorToFile(error).catch(e => console.error(e))
47
-
48
- return error
49
- }
50
-
51
- static handleError(err: Error, options?: ErrorOptions): Error {
52
- this.handle(err, options)
53
- return err
54
- }
55
-
56
- static async writeErrorToFile(err: Error | unknown): Promise<void> {
57
- if (!(err instanceof Error)) {
58
- console.error('Error is not an instance of Error:', err)
59
- return
60
- }
61
-
62
- const formattedError = `[${new Date().toISOString()}] ${err.name}: ${err.message}\n`
63
- const logFilePath = path.logsPath('stacks.log') ?? path.logsPath('errors.log')
64
-
65
- try {
66
- await mkdir(path.dirname(logFilePath), { recursive: true })
67
- await appendFile(logFilePath, formattedError)
68
- }
69
- catch (error) {
70
- console.error('Failed to write to error file:', error)
71
- }
72
- }
73
-
74
- static writeErrorToConsole(err: string | Error | unknown): void {
75
- console.error(err)
76
-
77
- const errorString = typeof err === 'string' ? err : err instanceof Error ? err.message : JSON.stringify(err)
78
-
79
- if (
80
- errorString.includes('bunx --bun cdk destroy')
81
- || errorString === `Failed to execute command: ${italic('bunx --bun eslint . --fix')}`
82
- || errorString === `Failed to execute command: ${italic('bun storage/framework/core/actions/src/lint/fix.ts')}`
83
- ) {
84
- if (!this.isTestEnvironment) {
85
- // eslint-disable-next-line no-console
86
- console.log(
87
- 'No need to worry. The edge function is currently being destroyed. Please run `buddy undeploy` shortly again, and continue doing so until it succeeds running.',
88
- )
89
- // eslint-disable-next-line no-console
90
- console.log('Hoping to see you back soon!')
91
- }
92
- }
93
-
94
- if (this.shouldExitProcess) {
95
- process.exit(ExitCode.FatalError)
96
- }
97
- }
98
- }
99
-
100
- export function handleError(err: string | Error | object | unknown, options?: ErrorOptions): Error {
101
- let errorMessage: string
102
-
103
- if (isString(err)) {
104
- errorMessage = err
105
- }
106
- else if (err instanceof Error) {
107
- errorMessage = err.message
108
- }
109
- else if (options instanceof Error) {
110
- errorMessage = options.message
111
- }
112
- else {
113
- errorMessage = String(err)
114
- }
115
-
116
- writeToLogFile(`ERROR: ${stripAnsi(errorMessage)}`)
117
-
118
- return ErrorHandler.handle(err, options)
119
- }
120
-
121
- interface WriteOptions {
122
- logFile?: string
123
- }
124
-
125
- export async function writeToLogFile(message: string, options?: WriteOptions): Promise<void> {
126
- const formattedMessage = `[${new Date().toISOString()}] ${message}\n`
127
-
128
- try {
129
- const logFile = options?.logFile ?? config.logging.logsPath ?? 'storage/logs/stacks.log'
130
-
131
- try {
132
- // Check if the file exists
133
- await access(logFile)
134
- }
135
- catch {
136
- // File doesn't exist, create the directory
137
- // eslint-disable-next-line no-console
138
- console.log('Creating log file directory...', logFile)
139
- await mkdir(dirname(logFile), { recursive: true })
140
- }
141
-
142
- // Append the message to the log file
143
- await appendFile(logFile, formattedMessage)
144
- }
145
- catch (error) {
146
- console.error('Failed to write to log file:', error)
147
- }
148
- }
package/src/http.ts DELETED
@@ -1,6 +0,0 @@
1
- export class HttpError extends Error {
2
- constructor(public status: number, message: string) {
3
- super(message)
4
- this.name = 'Server Error!'
5
- }
6
- }
package/src/index.ts DELETED
@@ -1,16 +0,0 @@
1
- export * from './handler'
2
- export * from './http'
3
- export * from './utils'
4
- export {
5
- err,
6
- Err,
7
- errAsync,
8
- fromPromise,
9
- fromSafePromise,
10
- fromThrowable,
11
- ok,
12
- Ok,
13
- okAsync,
14
- Result,
15
- ResultAsync,
16
- } from 'neverthrow'
package/src/utils.ts DELETED
@@ -1,26 +0,0 @@
1
- import { ErrorHandler } from './handler'
2
-
3
- export function rescue<T, F>(
4
- fn: () => T | Promise<T>,
5
- fallback: F,
6
- onError?: (error: Error) => void,
7
- ): T | F | Promise<T | F> {
8
- try {
9
- const result = fn()
10
- if (result instanceof Promise) {
11
- return result.catch((error) => {
12
- if (onError) {
13
- onError(ErrorHandler.handle(error))
14
- }
15
- return fallback
16
- })
17
- }
18
- return result
19
- }
20
- catch (error) {
21
- if (onError) {
22
- onError(ErrorHandler.handle(error))
23
- }
24
- return fallback
25
- }
26
- }
@@ -1,37 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Server Error</title>
7
- <style>
8
- body {
9
- font-family: Arial, sans-serif;
10
- background-color: #f4f4f4;
11
- margin: 0;
12
- padding: 20px;
13
- }
14
- .error-container {
15
- background-color: #ffefef;
16
- border: 1px solid #ff4d4d;
17
- color: #d8000c;
18
- padding: 20px;
19
- border-radius: 5px;
20
- }
21
- .error-title {
22
- font-size: 24px;
23
- font-weight: bold;
24
- margin-bottom: 10px;
25
- }
26
- .error-message {
27
- font-size: 18px;
28
- }
29
- </style>
30
- </head>
31
- <body>
32
- <div class="error-container">
33
- <div class="error-title">An Error Occurred</div>
34
- <p class="error-message">{{ERROR_MESSAGE}}</p>
35
- </div>
36
- </body>
37
- </html>