Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
feat(rethrow): add method to rethrow error with details, close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jun 29, 2017
1 parent 57aeb8e commit 9fa47a3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ doMyStuff()
If `info` argument is missing a description or a solution, an error will be
thrown.

## Re-throwing a detailed error

You might not want to handle the error, but rather add details and rethrow
it. For this there is a helper function

```js
const {throwDetailedError} = require('@cypress/error-message')
const info = {
description: 'error description',
solution: 'our solution'
}
foo()
.catch(throwDetailedError(info))
// later catch this detailed error
.catch(err => {
// err has original exception + description and solution
console.error(err)
})
```

### Small print

Support: if you find any problems with this module, email / tweet /
Expand Down
49 changes: 39 additions & 10 deletions src/index-spec.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
'use strict'

const { formErrorText } = require('.')
const { formErrorText, throwDetailedError } = require('.')
const { platformRegex, versionRegex } = require('./get-os-info')
const la = require('lazy-ass')
const is = require('check-more-types')
const snapshot = require('snap-shot')

/* global describe, it */
describe('formErrorText', () => {
const info = {
description: 'error description',
solution: 'our solution'
}
const error = new Error('exception message')
const info = {
description: 'error description',
solution: 'our solution'
}
const error = new Error('exception message')

const normalizePlatform = text =>
text.replace(platformRegex, 'Platform: name')
const normalizeVersion = text => text.replace(versionRegex, 'Version: number')
const normalizePlatform = text => text.replace(platformRegex, 'Platform: name')
const normalizeVersion = text => text.replace(versionRegex, 'Version: number')

describe('formErrorText', () => {
it('is a function', () => {
la(is.fn(formErrorText))
})
Expand Down Expand Up @@ -55,3 +54,33 @@ describe('formErrorText', () => {
})
})
})

describe('rethrowing error with details', () => {
it('is a function', () => {
la(is.fn(throwDetailedError))
})

it('returns function that is waiting for an error', () => {
const f = throwDetailedError(info)
la(is.fn(f))
})

it('rethrows error but with details', () => {
const f = throwDetailedError(info)
return f(error).then(
output => {
console.error('problem, should have failed')
console.error('output', output)
throw new Error('nope')
},
err => {
la(err !== error, 'should have new error instance')
la(
err.message.includes(info.solution),
'rethrown error does not have solution',
err.message
)
}
)
})
})
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ function formErrorText (info) {
}
}

const raise = text => {
// makes error message "better" by starting it at a new line
throw new Error('\n' + text)
}

const throwDetailedError = info => error =>
formErrorText(info)(error).then(raise)

module.exports = {
utils,
formErrorText
formErrorText,
throwDetailedError
}

0 comments on commit 9fa47a3

Please sign in to comment.