Skip to content

Commit

Permalink
Merge pull request #108 from AthennaIO/develop
Browse files Browse the repository at this point in the history
update deps
  • Loading branch information
jlenon7 authored Jan 10, 2024
2 parents 92b8780 + bc4a329 commit 3b2398e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 10 deletions.
13 changes: 6 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "4.27.0",
"version": "4.28.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -61,7 +61,7 @@
"change-case": "^4.1.2",
"collect.js": "^4.36.1",
"execa": "^8.0.1",
"fastify": "^4.25.1",
"fastify": "^4.25.2",
"got": "^12.6.1",
"http-status-codes": "^2.2.0",
"is-wsl": "^2.2.0",
Expand Down
1 change: 1 addition & 0 deletions src/globals/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Error.prototype.toAthennaException = function (options: ExceptionJson = {}) {
options.stack = options.stack || this.stack
options.message = options.message || this.message
options.code = options.code || changeCase.constantCase(options.name)
options.details = options.details || this.details || this.errors

return new Exception(options)
}
23 changes: 22 additions & 1 deletion src/helpers/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import YouchTerminal from 'youch-terminal'
import { Color } from '#src/helpers/Color'
import { Options } from '#src/helpers/Options'
import type { ExceptionJson } from '#src/types'
import { Is } from '@athenna/common'

export class Exception extends Error {
public code?: string
public help?: any
public status?: number
public details?: any[]
public isAthennaException = true

/**
Expand All @@ -46,6 +48,10 @@ export class Exception extends Error {
this.help = options.help
}

if (options.details) {
this.details = options.details
}

if (options.stack) {
this.stack = options.stack
} else {
Expand All @@ -65,6 +71,7 @@ export class Exception extends Error {
json.message = this.message

if (this.help) json.help = this.help
if (this.details) json.details = this.details
if (stack) json.stack = this.stack

return json
Expand Down Expand Up @@ -92,11 +99,24 @@ export class Exception extends Error {

const separator = Color.cyan('-----')
const helpKey = Color.gray.bold.bgGreen(' HELP ')
const detailsKey = Color.gray.bold.bgHex('#f18b0e')(' DETAILS ')
const title = Color.gray.bold.bgRed(` ${this.code || this.name} `)

let help = ''
let message = `${title}\n\n${Color.apply(this.message)}`

if (this.details && this.details.length) {
message = `${message}\n\n${detailsKey}\n\n${this.details
.map(detail => {
if (Is.String(detail)) {
return Color.orange(Color.apply(detail))
}
return Color.orange(JSON.stringify(detail, null, 2))
})
.join('\n')}`
}

if (this.help && this.help !== '') {
help = `${helpKey}\n\n ${Color.green(
Color.apply(this.help)
Expand All @@ -111,7 +131,8 @@ export class Exception extends Error {
message,
code: this.code,
stack: this.stack,
status: this.status
status: this.status,
details: this.details
}),
{}
).toJSON()
Expand Down
1 change: 1 addition & 0 deletions src/types/json/ExceptionJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export interface ExceptionJson {
status?: number
message?: string
help?: any
details?: any[]
stack?: string
}
25 changes: 25 additions & 0 deletions tests/unit/ExceptionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ export default class ExceptionTest {

const prettyError = await exception.prettify()

console.log(prettyError)

assert.isDefined(prettyError)
assert.typeOf(prettyError, 'string')
}

@Test()
public async shouldBeAbleToPrettifyTheExceptionWithDetails({ assert }: Context) {
class InternalServerException extends Exception {
constructor(content = 'Internal Server Error.', status = 500) {
super({
status,
message: content,
details: ['Machine error', 'Runtime error'],
code: 'E_RUNTIME_EXCEPTION',
help: 'Restart your computer, works always. 👍'
})
}
}

const exception = new InternalServerException()
const prettyError = await exception.prettify()

console.log(prettyError)

assert.isDefined(prettyError)
assert.typeOf(prettyError, 'string')
}
Expand Down

0 comments on commit 3b2398e

Please sign in to comment.