Skip to content

Commit

Permalink
feat: ensure get error
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 28, 2019
1 parent 85760ca commit 1f4a2d6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { flow, isNil, castArray, first } = require('lodash')
const PrettyError = require('pretty-error')
const ensureError = require('ensure-error')
const cleanStack = require('clean-stack')
const isIterable = require('is-iterable')

Expand Down Expand Up @@ -58,12 +59,15 @@ const beautyError = (error, opts) => {
return pretty.render(error)
}

const getError = genericError =>
first(
const getError = rawError => {
const genericError = ensureError(rawError)

return first(
isIterable(genericError)
? Array.from(genericError)
: castArray(genericError)
)
}

module.exports = flow([getError, cleanError, beautyError])
module.exports.beautyError = beautyError
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "beauty-error",
"description": "Getting a beauty error. Oriented for well printed errors. Be beauty, no ugly.",
"homepage": "https://documentup.com/Kikobeats/beauty-error",
"homepage": "https://nicedoc.io/Kikobeats/beauty-error",
"version": "1.1.5",
"main": "index.js",
"author": {
Expand All @@ -24,6 +24,7 @@
],
"dependencies": {
"clean-stack": "~2.0.0",
"ensure-error": "~2.0.0",
"is-iterable": "~1.1.1",
"lodash": "~4.17.11",
"pretty-error": "~2.1.1"
Expand Down
18 changes: 18 additions & 0 deletions test/get-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const test = require('ava')
const { getError } = require('..')

test('get error from native error', t => {
const error = getError(new Error('my error'))
t.true(error instanceof Error)
t.is(error.message, 'my error')
t.true(!!error.stack)
})

test('get error from string', t => {
const error = getError('my error')
t.true(error instanceof Error)
t.is(error.message, "'my error'")
t.true(!!error.stack)
})
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const test = require('ava')
const prettifyError = require('..')
const beautyError = require('..')

test('pretty an error', t => {
const error = new Error("Hello darkness my old' friend")
t.snapshot(prettifyError({ ...error, stack: '' }))
t.snapshot(beautyError({ ...error, stack: '' }))
})

0 comments on commit 1f4a2d6

Please sign in to comment.