From 490e8c0a3e9aa6da0aa583ddc07d09405d571467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Wed, 27 Sep 2023 12:25:02 +0100 Subject: [PATCH 1/2] fix(exception): avoid using this property in pretty error --- package.json | 2 +- src/helpers/Exception.ts | 64 +++++++++------------------------------- 2 files changed, 15 insertions(+), 51 deletions(-) diff --git a/package.json b/package.json index 83b8761..804c130 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/common", - "version": "4.15.3", + "version": "4.15.4", "description": "The Athenna common helpers to use in any Node.js ESM project.", "license": "MIT", "author": "João Lenon ", diff --git a/src/helpers/Exception.ts b/src/helpers/Exception.ts index b8edb49..896495f 100644 --- a/src/helpers/Exception.ts +++ b/src/helpers/Exception.ts @@ -12,8 +12,6 @@ import * as changeCase from 'change-case' import Youch from 'youch' import YouchTerminal from 'youch-terminal' -import { readFile } from 'node:fs' -import { fileURLToPath } from 'node:url' import { Color } from '#src/helpers/Color' import { Options } from '#src/helpers/Options' import type { ExceptionJson } from '#src/types' @@ -96,61 +94,27 @@ export class Exception extends Error { const helpKey = Color.gray.bold.bgGreen(' HELP ') const title = Color.gray.bold.bgRed(` ${this.code || this.name} `) - this.message = `${title}\n\n${Color.apply(this.message)}` + let help = '' + let message = `${title}\n\n${Color.apply(this.message)}` if (this.help && this.help !== '') { - this.help = `${helpKey}\n\n ${Color.green( + help = `${helpKey}\n\n ${Color.green( Color.apply(this.help) )}\n\n ${separator}` } else { - this.message = this.message.concat(`\n\n${separator}`) + message = this.message.concat(`\n\n${separator}`) } - const youch = new Youch(this, {}) - - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - youch._getFrameSource = function (frame) { - let path = frame.file - .replace(/dist\/webpack:\//g, '') // unix - .replace(/dist\\webpack:\\/g, '') // windows - - /** - * We ignore the error when "fileURLToPath" is unable to parse - * the path, since returning the frame source is an optional - * thing - */ - try { - path = path.startsWith('file:') ? fileURLToPath(path) : path - } catch {} - - return new Promise(resolve => { - if (!path) { - resolve(null) - return - } - readFile(path, 'utf-8', (error, contents) => { - if (error) { - resolve(null) - return - } - - const lines = contents.split(/\r?\n/) - const lineNumber = frame.line - - resolve({ - pre: lines.slice( - Math.max(0, lineNumber - (this.options.preLines + 1)), - lineNumber - 1 - ), - line: lines[lineNumber - 1], - post: lines.slice(lineNumber, lineNumber + this.options.postLines) - }) - }) - }) - } - - const pretty = await youch.toJSON() + const pretty = await new Youch( + { + help, + message, + code: this.code, + stack: this.stack, + status: this.status + }, + {} + ).toJSON() pretty.error.frames = pretty.error.frames.map(frame => { frame.isApp = true From 6f9b98aa6f6d507674969fb603f1f73cdc1bf6d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Wed, 27 Sep 2023 12:25:35 +0100 Subject: [PATCH 2/2] chore(npm): upgrade version --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 01a1af0..6145674 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "4.15.3", + "version": "4.15.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "4.15.3", + "version": "4.15.4", "license": "MIT", "dependencies": { "@fastify/formbody": "^7.4.0",