@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/dist/handler.d.ts CHANGED
@@ -1,16 +1,89 @@
1
- import type { ErrorOptions } from "@stacksjs/logging";
2
- type ErrorMessage = string;
1
+ import type { ErrorOptions } from '@stacksjs/logging';
2
+
3
+ declare type ErrorMessage = string
3
4
  export declare class ErrorHandler {
4
- static isTestEnvironment: boolean;
5
- static shouldExitProcess: boolean;
6
- static handle(err: Error | ErrorMessage | unknown, options?: ErrorOptions): Error;
7
- static handleError(err: Error, options?: ErrorOptions): Error;
8
- static writeErrorToFile(err: Error | unknown): Promise<void>;
9
- static writeErrorToConsole(err: string | Error | unknown): void;
5
+ static isTestEnvironment = false
6
+ static shouldExitProcess = true
7
+
8
+ static handle(err: Error | ErrorMessage | unknown, options?: ErrorOptions): Error {
9
+ this.shouldExitProcess = options?.shouldExit !== false
10
+ if (options?.silent !== true)
11
+ this.writeErrorToConsole(err)
12
+
13
+ let errorMessage: string
14
+
15
+ if (options?.message) {
16
+ errorMessage = options.message
17
+ }
18
+ else if (err instanceof Error) {
19
+ errorMessage = err.message
20
+ }
21
+ else if (typeof err === 'string') {
22
+ errorMessage = err
23
+ }
24
+ else {
25
+ errorMessage = JSON.stringify(err)
26
+ }
27
+
28
+ const error = new Error(errorMessage)
29
+
30
+ if (err instanceof Error) {
31
+ Object.assign(error, err)
32
+ }
33
+
34
+ this.writeErrorToFile(error).catch(e => console.error(e))
35
+
36
+ return error
37
+ }
38
+
39
+ static handleError(err: Error, options?: ErrorOptions): Error {
40
+ this.handle(err, options)
41
+ return err
42
+ }
43
+
44
+ static async writeErrorToFile(err: Error | unknown): Promise<void> {
45
+ if (!(err instanceof Error)) {
46
+ console.error('Error is not an instance of Error:', err)
47
+ return
48
+ }
49
+
50
+ const formattedError = `[${new Date().toISOString()}] ${err.name}: ${err.message}\n`
51
+ const logFilePath = path.logsPath('stacks.log') ?? path.logsPath('errors.log')
52
+
53
+ try {
54
+ await mkdir(path.dirname(logFilePath), { recursive: true })
55
+ await appendFile(logFilePath, formattedError)
56
+ }
57
+ catch (error) {
58
+ console.error('Failed to write to error file:', error)
59
+ }
60
+ }
61
+
62
+ static writeErrorToConsole(err: string | Error | unknown): void {
63
+ console.error(err)
64
+
65
+ const errorString = typeof err === 'string' ? err : err instanceof Error ? err.message : JSON.stringify(err)
66
+
67
+ if (
68
+ errorString.includes('bunx --bun cdk destroy')
69
+ || errorString === `Failed to execute command: ${italic('bunx --bun eslint . --fix')}`
70
+ || errorString === `Failed to execute command: ${italic('bun storage/framework/core/actions/src/lint/fix.ts')}`
71
+ ) {
72
+ if (!this.isTestEnvironment) {
73
+ console.log(
74
+ '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.',
75
+ )
76
+ console.log('Hoping to see you back soon!')
77
+ }
78
+ }
79
+
80
+ if (this.shouldExitProcess) {
81
+ process.exit(ExitCode.FatalError)
82
+ }
83
+ }
10
84
  }
11
85
  export declare function handleError(err: string | Error | object | unknown, options?: ErrorOptions): Error;
12
- interface WriteOptions {
13
- logFile?: string;
86
+ declare interface WriteOptions {
87
+ logFile?: string
14
88
  }
15
- export declare function writeToLogFile(message: string, options?: WriteOptions): Promise<void>;
16
- export {};
89
+ export declare function writeToLogFile(message: string, options?: WriteOptions): Promise<void>;
package/dist/http.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export declare class HttpError extends Error {
2
- status: number;
3
- constructor(status: number, message: string);
4
- }
2
+ constructor(public status: number, message: string) {
3
+ super(message)
4
+ this.name = 'Server Error!'
5
+ }
6
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,16 @@
1
- export * from "./handler";
2
- export * from "./http";
3
- export * from "./utils";
4
- export { err, Err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, Ok, okAsync, Result, ResultAsync } from "neverthrow";
1
+ export {
2
+ export * from './handler'
3
+ export * from './http'
4
+ export * from './utils'
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'