@stacksjs/error-handling 0.70.371 → 0.70.376

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.
Files changed (2) hide show
  1. package/dist/handler.js +10 -3
  2. package/package.json +6 -6
package/dist/handler.js CHANGED
@@ -1,7 +1,14 @@
1
- import fs from"node:fs";import{dirname}from"node:path";import*as process from"node:process";function italic(str){return`\x1B[3m${str}\x1B[23m`}function stripAnsi(str){return str.replace(/\x1B\[[0-9;]*m/g,"")}import*as path from"@stacksjs/path";import{ExitCode}from"@stacksjs/types";export class ErrorHandler{static isTestEnvironment=!1;static shouldExitProcess=!1;static handle(err,options){if(!this.isTestEnvironment)this.shouldExitProcess=options?.shouldExit===!0;if(options?.silent!==!0)this.writeErrorToConsole(err);let errorMessage;if(options?.message)errorMessage=options.message;else if(err instanceof Error)errorMessage=err.message;else if(typeof err==="string")errorMessage=err;else errorMessage=JSON.stringify(err);const error=Error(errorMessage);if(err instanceof Error)Object.assign(error,err);this.writeErrorToFile(error).catch((e)=>console.error(e));return error}static handleError(err,options){this.handle(err,options);return err}static async writeErrorToFile(err,context){if(!(err instanceof Error)){console.error("Error is not an instance of Error:",err);return}const contextStr=context?` | url=${context.url||"N/A"} method=${context.method||"N/A"} user=${context.userId||"anonymous"}`:"",stackLine=err.stack?`
1
+ import fs from"node:fs";import{dirname}from"node:path";import*as process from"node:process";function italic(str){return`\x1B[3m${str}\x1B[23m`}function stripAnsi(str){return str.replace(/\x1B\[[0-9;]*m/g,"")}import*as path from"@stacksjs/path";import{ExitCode}from"@stacksjs/types";export class ErrorHandler{static isTestEnvironment=!1;static shouldExitProcess=!1;static handle(err,options){if(!this.isTestEnvironment)this.shouldExitProcess=options?.shouldExit===!0;if(options?.silent!==!0)this.writeErrorToConsole(err);let errorMessage;if(options?.message)errorMessage=options.message;else if(err instanceof Error)errorMessage=err.message;else if(typeof err==="string")errorMessage=err;else errorMessage=JSON.stringify(err);const error=err instanceof Error&&errorMessage===err.message?err:Error(errorMessage,err instanceof Error?{cause:err.cause??err}:void 0);if(err instanceof Error&&error!==err){Object.assign(error,err);if(err.stack)error.stack=`${error.name}: ${errorMessage}
2
+ ${err.stack.split(`
3
+ `).slice(1).join(`
4
+ `)}`}this.writeErrorToFile(error).catch((e)=>console.error(e));return error}static handleError(err,options){this.handle(err,options);return err}static async writeErrorToFile(err,context){if(!(err instanceof Error)){console.error("Error is not an instance of Error:",err);return}const contextStr=context?` | url=${context.url||"N/A"} method=${context.method||"N/A"} user=${context.userId||"anonymous"}`:"",stackLine=err.stack?`
2
5
  ${err.stack.split(`
3
6
  `).slice(1,4).join(`
4
7
  `)}`:"",formattedError=`[${new Date().toISOString()}] ${err.name}: ${err.message}${contextStr}${stackLine}
5
8
  `,logFilePath=path.logsPath("stacks.log")??path.logsPath("errors.log");try{await fs.promises.mkdir(path.dirname(logFilePath),{recursive:!0});await fs.promises.appendFile(logFilePath,formattedError)}catch(error){console.error("Failed to write to error file:",error)}}static writeErrorToConsole(err){let errorString;if(err instanceof Error)errorString=err.message;else if(typeof err==="string")errorString=err;else errorString=JSON.stringify(err);console.error(errorString);if(errorString.includes("bunx --bun cdk destroy")||errorString===`Failed to execute command: ${italic("bunx --bun eslint . --fix")}`||errorString===`Failed to execute command: ${italic("bun storage/framework/core/actions/src/lint/fix.ts")}`){if(!this.isTestEnvironment){console.log("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.");console.log("Hoping to see you back soon!")}}if(this.shouldExitProcess)process.exit(ExitCode.FatalError)}}let defaultLogPath="storage/logs/stacks.log";export function setLogPath(path){defaultLogPath=path}export async function writeToLogFile(message,options){const formattedMessage=`[${new Date().toISOString()}] ${message}
6
- `,logFile=options?.logFile??defaultLogPath,dirPath=dirname(logFile);await fs.promises.mkdir(dirPath,{recursive:!0});await fs.promises.appendFile(logFile,formattedMessage)}export function handleError(err,options){let errorMessage,contextData,errorOptions;if(options&&typeof options==="object"&&(("shouldExit"in options)||("silent"in options)))errorOptions=options;else if(options!==void 0)contextData=options&&typeof options==="object"?options:{error:options};const errMsg=err instanceof Error?err.message:typeof err==="string"?err:JSON.stringify(err);if(errorOptions?.message)errorMessage=`${errMsg}: ${errorOptions.message}`;else errorMessage=errMsg;let logMessage=`ERROR: ${stripAnsi(errorMessage)}`;if(contextData)logMessage+=`
7
- Context: ${JSON.stringify(contextData,null,2)}`;writeToLogFile(logMessage).catch((err)=>{console.error("Failed to write error log:",err)});const error=Error(errorMessage);if(err instanceof Error)Object.assign(error,err);return ErrorHandler.handle(error,errorOptions)}
9
+ `,logFile=options?.logFile??defaultLogPath,dirPath=dirname(logFile);await fs.promises.mkdir(dirPath,{recursive:!0});await fs.promises.appendFile(logFile,formattedMessage)}function serializableContext(value,seen=new Set){if(value instanceof Error)return{name:value.name,message:value.message,stack:value.stack,...value.cause!==void 0?{cause:serializableContext(value.cause,seen)}:{}};if(!value||typeof value!=="object")return value;if(seen.has(value))return"[circular]";seen.add(value);if(Array.isArray(value))return value.map((entry)=>serializableContext(entry,seen));return Object.fromEntries(Object.entries(value).map(([key,entry])=>[key,serializableContext(entry,seen)]))}export function handleError(err,options){let errorMessage,contextData,errorOptions;const cause=options instanceof Error?options:void 0;if(!cause&&options&&typeof options==="object"&&(("shouldExit"in options)||("silent"in options)))errorOptions=options;else if(!cause&&options!==void 0)contextData=options&&typeof options==="object"?options:{error:options};const errMsg=err instanceof Error?err.message:typeof err==="string"?err:JSON.stringify(err);if(errorOptions?.message)errorMessage=`${errMsg}: ${errorOptions.message}`;else if(cause)errorMessage=cause.message&&cause.message!==errMsg?`${errMsg}: ${cause.message}`:errMsg;else errorMessage=errMsg;let logMessage=`ERROR: ${stripAnsi(errorMessage)}`;if(contextData)logMessage+=`
10
+ Context: ${JSON.stringify(serializableContext(contextData),null,2)}`;if(cause?.stack)logMessage+=`
11
+ Caused by: ${stripAnsi(cause.stack)}`;writeToLogFile(logMessage).catch((err)=>{console.error("Failed to write error log:",err)});const error=cause?Error(errorMessage,{cause}):Error(errorMessage);if(err instanceof Error)Object.assign(error,err);if(cause?.stack)error.stack=`${error.name}: ${errorMessage}
12
+ ${cause.stack.split(`
13
+ `).slice(1).join(`
14
+ `)}`;return ErrorHandler.handle(error,errorOptions)}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/error-handling",
3
3
  "type": "module",
4
- "version": "0.70.371",
4
+ "version": "0.70.376",
5
5
  "description": "Type safe error handling.",
6
6
  "author": "Chris Breuer",
7
7
  "contributors": [
@@ -62,12 +62,12 @@
62
62
  "ts-syntax-highlighter": "^0.2.1"
63
63
  },
64
64
  "devDependencies": {
65
- "@stacksjs/cli": "0.70.371",
66
- "@stacksjs/config": "0.70.371",
65
+ "@stacksjs/cli": "0.70.376",
66
+ "@stacksjs/config": "0.70.376",
67
67
  "better-dx": "^0.2.17",
68
- "@stacksjs/path": "0.70.371",
69
- "@stacksjs/types": "0.70.371",
70
- "@stacksjs/validation": "0.70.371"
68
+ "@stacksjs/path": "0.70.376",
69
+ "@stacksjs/types": "0.70.376",
70
+ "@stacksjs/validation": "0.70.376"
71
71
  },
72
72
  "sideEffects": false
73
73
  }