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

Commit

Permalink
feat(guard): throw an error if info or error are invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jun 15, 2017
1 parent fa7b1a9 commit d14e98e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ doMyStuff()
)
```

If `info` argument is missing a description or a solution, an error will be
thrown.

### Small print

Support: if you find any problems with this module, email / tweet /
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"scripts": {
"ban": "ban",
"deps": "deps-ok && dependency-check --no-dev .",
"deps": "deps-ok && dependency-check --no-dev --no-default-entries --entry src/index.js .",
"issues": "git-issues",
"license": "license-checker --production --onlyunknown --csv",
"lint": "standard --verbose --fix src/*.js",
Expand Down
18 changes: 18 additions & 0 deletions src/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ describe('formErrorText', () => {
.then(normalizeVersion)
)
)

describe('throws an error if', () => {
it('error is missing', () => {
la(is.raises(() => formErrorText(info)()))
})

it('error is not an exception', () => {
la(is.raises(() => formErrorText(info)('a problem')))
})

it('info is missing description', () => {
la(is.raises(() => formErrorText({solution: 'do something'})))
})

it('info is missing solution', () => {
la(is.raises(() => formErrorText({description: 'hmm'})))
})
})
})
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const { stripIndents } = require('common-tags')
const { merge } = require('ramda')
const {getOsVersion, getPlatformInfo} = require('./get-os-info')
const la = require('lazy-ass')
const is = require('check-more-types')

function addPlatformInformation (info) {
return getPlatformInfo()
Expand All @@ -20,8 +22,18 @@ const utils = {
getPlatformInfo
}

const isInfo = is.schema({
description: is.unemptyString,
solution: is.unemptyString,
printStack: is.maybe.bool
})

function formErrorText (info) {
la(isInfo(info), 'invalid info object', info)

return function onError (error) {
la(is.error(error), 'expected error object', error)

const hr = '----------'
return formError(info, error)
.then((extended) => stripIndents`
Expand Down

0 comments on commit d14e98e

Please sign in to comment.