Skip to content

Commit

Permalink
Refactor error suppression using custom errors
Browse files Browse the repository at this point in the history
Prefer checking errors by name than by message.
  • Loading branch information
novemberborn committed Jan 13, 2017
1 parent 0a2d32c commit 314f84b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
7 changes: 1 addition & 6 deletions init.js
Expand Up @@ -6,12 +6,7 @@ const minimatch = require('minimatch')
const { relative } = require('path')

function suppressError (err) {
return [
'no supported linter found',
'no package.json found'
].some(pattern => {
return pattern === err.message
})
return err.name === 'MissingLinterError' || err.name === 'MissingPackageError'
}

function lint (textEditor) {
Expand Down
22 changes: 19 additions & 3 deletions lib/findOptions.js
@@ -1,11 +1,24 @@
const { dirname } = require('path')
const ExtendableError = require('es6-error')
const readPkgUp = require('read-pkg-up')
const supportedLinters = require('./supportedLinters')

class MissingLinterError extends ExtendableError {
constructor (message = 'no supported linter found') {
super(message)
}
}

class MissingPackageError extends ExtendableError {
constructor (message = 'no package.json found') {
super(message)
}
}

function findOptions (file) {
return readPkgUp({ cwd: dirname(file) })
.then(({ path, pkg }) => {
if (!pkg) throw new Error('no package.json found')
if (!pkg) throw new MissingPackageError()

let linters = []
if (pkg.devDependencies) {
Expand All @@ -20,7 +33,7 @@ function findOptions (file) {
}

if (linters.length === 0) {
throw new Error('no supported linter found')
throw new MissingLinterError()
}

const linterName = linters[0]
Expand All @@ -43,4 +56,7 @@ function findOptions (file) {
})
}

module.exports = findOptions
exports = module.exports = findOptions

exports.MissingLinterError = MissingLinterError
exports.MissingPackageError = MissingPackageError
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -48,6 +48,7 @@
}
},
"dependencies": {
"es6-error": "^4.0.1",
"lru-cache": "^4.0.1",
"minimatch": "^3.0.3",
"read-pkg-up": "^2.0.0",
Expand Down
10 changes: 4 additions & 6 deletions test/init.spec.js
Expand Up @@ -6,6 +6,7 @@ const proxyquire = require('proxyquire')
const fs = require('fs')
const path = require('path')
const plugin = require('../init')
const { MissingLinterError, MissingPackageError } = require('../lib/findOptions')
const textEditorFactory = require('./util/textEditorFactory')
const linter = plugin.provideLinter()
const lint = linter.lint.bind(linter)
Expand Down Expand Up @@ -66,12 +67,9 @@ describe('linter-js-standard-engine', () => {
}
}).provideLinter()

for (const msg of [
'no supported linter found',
'no package.json found'
]) {
it(`should suppress "${msg}" errors`, () => {
currentError = new Error(msg)
for (const ErrorClass of [MissingLinterError, MissingPackageError]) {
it(`should suppress "${ErrorClass.name}" errors`, () => {
currentError = new ErrorClass()
return expect(linter.lint(textEditorFactory('')), 'to be fulfilled').then(data => expect(data, 'to be empty'))
})
}
Expand Down

0 comments on commit 314f84b

Please sign in to comment.