diff --git a/README.md b/README.md index a1ffcdc..5e419df 100644 --- a/README.md +++ b/README.md @@ -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 / diff --git a/src/index-spec.js b/src/index-spec.js index df07050..0e342eb 100644 --- a/src/index-spec.js +++ b/src/index-spec.js @@ -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)) }) @@ -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 + ) + } + ) + }) +}) diff --git a/src/index.js b/src/index.js index defb2b2..efdd2e2 100644 --- a/src/index.js +++ b/src/index.js @@ -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 }