@stacksjs/error-handling 0.59.11 → 0.61.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/error-handling",
3
3
  "type": "module",
4
- "version": "0.59.11",
4
+ "version": "0.61.1",
5
5
  "description": "Type safe error handling.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
@@ -55,7 +55,7 @@
55
55
  "@stacksjs/cli": "latest",
56
56
  "@stacksjs/path": "latest",
57
57
  "@stacksjs/types": "latest",
58
- "neverthrow": "^6.1.0"
58
+ "neverthrow": "^6.2.1"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@stacksjs/development": "latest"
package/src/handler.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { dirname, logsPath } from '@stacksjs/path'
1
+ import * as path from '@stacksjs/path'
2
+ import { ExitCode } from '@stacksjs/types'
2
3
  import fs from 'fs-extra'
3
4
 
4
5
  interface ErrorOptions {
@@ -8,50 +9,57 @@ interface ErrorOptions {
8
9
  export const StacksError = Error
9
10
 
10
11
  export class ErrorHandler {
11
- static logFile = logsPath('errors.log')
12
+ // static logFile = path.logsPath('errors.log')
12
13
 
13
- static handle(err: ErrorDescription | Error, options?: ErrorOptions | Error) {
14
+ static handle(err: ErrorDescription | Error | unknown, options?: ErrorOptions): Error {
14
15
  // let's only write to the console if we are not in silent mode
15
- if (!(options instanceof Error) && options?.silent !== false)
16
- this.writeErrorToConsole(err, options)
16
+ if (options?.silent !== false) this.writeErrorToConsole(err)
17
17
 
18
- if (typeof err === 'string')
19
- err = new StacksError(err)
18
+ if (typeof err === 'string') err = new StacksError(err)
20
19
 
21
- this.writeErrorToFile(err).catch(e => console.error(e))
20
+ this.writeErrorToFile(err).catch((e) => console.error(e))
22
21
 
23
- return err
22
+ return err as Error // TODO: improve this type
24
23
  }
25
24
 
26
- static handleError(err: Error, options?: ErrorOptions) {
25
+ static handleError(err: Error, options?: ErrorOptions): Error {
27
26
  this.handle(err, options)
28
27
  return err
29
28
  }
30
29
 
31
- static async writeErrorToFile(err: Error) {
30
+ static async writeErrorToFile(err: Error | unknown) {
31
+ if (!(err instanceof Error)) {
32
+ console.error('Error is not an instance of Error:', err)
33
+ return
34
+ }
35
+
32
36
  const formattedError = `[${new Date().toISOString()}] ${err.name}: ${err.message}\n`
33
- const errorsLogFilePath = logsPath('errors.log')
37
+ const errorsLogFilePath = path.logsPath('errors.log')
34
38
 
35
39
  try {
36
40
  // Ensure the directory exists
37
- await fs.mkdir(dirname(errorsLogFilePath), { recursive: true })
41
+ await fs.mkdir(path.dirname(errorsLogFilePath), { recursive: true })
38
42
  // Append the message to the log file
39
43
  await fs.appendFile(errorsLogFilePath, formattedError)
40
- }
41
- catch (error) {
44
+ } catch (error) {
42
45
  console.error('Failed to write to error file:', error)
43
46
  }
44
47
  }
45
48
 
46
- static writeErrorToConsole(err: string | Error, options?: ErrorOptions) {
47
- if (options)
48
- console.error(err, options)
49
- else
49
+ static writeErrorToConsole(err: string | Error | unknown): void {
50
+ if (
51
+ err === 'Failed to execute command: bunx biome check --apply .' ||
52
+ err === 'Failed to execute command: bun --bun storage/framework/core/actions/src/lint/fix.ts'
53
+ )
54
+ // To trigger this, run `buddy release` with a lint error in your codebase
50
55
  console.error(err)
56
+ process.exit(ExitCode.FatalError) // TODO: abstract this by differently catching the error somewhere
57
+ console.error(err)
51
58
  }
52
59
  }
53
60
 
54
61
  type ErrorDescription = string
55
- export function handleError(err: ErrorDescription | Error, options?: ErrorOptions | Error): Error {
62
+
63
+ export function handleError(err: ErrorDescription | Error | unknown, options?: ErrorOptions): Error {
56
64
  return ErrorHandler.handle(err, options)
57
65
  }
package/src/index.ts CHANGED
@@ -1,2 +1,14 @@
1
1
  export * from './handler'
2
- export { err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync, Err, Ok, Result, ResultAsync } from 'neverthrow'
2
+ export {
3
+ err,
4
+ errAsync,
5
+ fromPromise,
6
+ fromSafePromise,
7
+ fromThrowable,
8
+ ok,
9
+ okAsync,
10
+ Err,
11
+ Ok,
12
+ Result,
13
+ ResultAsync,
14
+ } from 'neverthrow'
package/dist/handler.d.ts DELETED
@@ -1,14 +0,0 @@
1
- interface ErrorOptions {
2
- silent?: boolean;
3
- }
4
- export declare const StacksError: ErrorConstructor;
5
- export declare class ErrorHandler {
6
- static logFile: any;
7
- static handle(err: ErrorDescription | Error, options?: ErrorOptions | Error): Error;
8
- static handleError(err: Error, options?: ErrorOptions): Error;
9
- static writeErrorToFile(err: Error): Promise<void>;
10
- static writeErrorToConsole(err: string | Error, options?: ErrorOptions): void;
11
- }
12
- type ErrorDescription = string;
13
- export declare function handleError(err: ErrorDescription | Error, options?: ErrorOptions | Error): Error;
14
- export {};
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './handler';
2
- export { err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync, Err, Ok, Result, ResultAsync } from 'neverthrow';