Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.

Commit 398fac1

Browse files
committed
Improved error handling.
1 parent 1acd4aa commit 398fac1

21 files changed

+192
-17
lines changed

bin/couchdb-builder

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require(__dirname + '/../lib/cli.js')(process, console);

lib/cli.js

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/handler/CommonCoffeeHandler.js

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/handler/CommonJsHandler.js

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/handler/JsonHandler.js

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/handler/TextHandler.js

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/handler/error/HandlerError.js

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
"url": "http://ezzatron.com/"
2323
},
2424
"main": "lib/couchdb-builder.js",
25+
"bin": {
26+
"couchdb-builder": "bin/couchdb-builder"
27+
},
28+
"scripts": {
29+
"test": "scripts/test"
30+
},
2531
"engines": {
2632
"node": ">= 0.10.0",
2733
"npm": ">= 1.0.0"
@@ -36,9 +42,6 @@
3642
"mocha": "^2.2.5",
3743
"sinon": "^1.14.1"
3844
},
39-
"scripts": {
40-
"test": "scripts/test"
41-
},
4245
"dependencies": {
4346
"bluebird": "^3.1.5",
4447
"readdirp": "^2.0.0"

scripts/build

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
set -e
44

5+
rm -rf ./lib
56
node_modules/.bin/coffee --output lib --compile src

src/cli.coffee

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = (_process, _console, builder = require '.') ->
2+
argv = _process.argv.slice 2
3+
4+
unless argv.length
5+
_console.error 'Path is required.'
6+
7+
return _process.exit 1
8+
9+
return builder.build argv[0]
10+
.then (result) ->
11+
return _console.log JSON.stringify result, null, 2
12+
.catch (error) ->
13+
return _console.error error.toString()

src/handler/CommonCoffeeHandler.coffee

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ path = require 'path'
44
Promise = require 'bluebird'
55
util = require 'util'
66

7+
HandlerError = require './error/HandlerError'
8+
79
module.exports = class CommonCoffeeHandler
810

911
constructor: (@template) ->
@@ -20,11 +22,16 @@ module.exports = class CommonCoffeeHandler
2022
return resolve null if path.extname(filePath) isnt '.coffee'
2123

2224
fs.readFile filePath, (error, data) =>
23-
return reject error if error
25+
if error
26+
error = new HandlerError 'CommonCoffeeHandler', filePath, error
27+
28+
return reject error
2429

2530
try
2631
js = coffee.compile data.toString()
2732
catch error
33+
error = new HandlerError 'CommonCoffeeHandler', filePath, error
34+
2835
return reject error
2936

3037
resolve [

src/handler/CommonJsHandler.coffee

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ path = require 'path'
33
Promise = require 'bluebird'
44
util = require 'util'
55

6+
HandlerError = require './error/HandlerError'
7+
68
module.exports = class CommonJsHandler
79

810
constructor: (@template) ->
@@ -21,7 +23,10 @@ module.exports = class CommonJsHandler
2123
return resolve null if path.extname(filePath) isnt '.js'
2224

2325
fs.readFile filePath, (error, data) =>
24-
return reject error if error
26+
if error
27+
error = new HandlerError 'CommonJsHandler', filePath, error
28+
29+
return reject error
2530

2631
resolve [
2732
path.basename filePath, '.js'

src/handler/JsonHandler.coffee

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ fs = require 'fs'
22
path = require 'path'
33
Promise = require 'bluebird'
44

5+
HandlerError = require './error/HandlerError'
6+
57
module.exports = class JsonHandler
68

79
handleFile: (filePath) -> new Promise (resolve, reject) ->
810
return resolve null if path.extname(filePath) isnt '.json'
911

1012
fs.readFile filePath, (error, json) ->
11-
return reject error if error
13+
if error
14+
error = new HandlerError 'JsonHandler', filePath, error
15+
16+
return reject error
1217

1318
try
1419
data = JSON.parse json
1520
catch error
21+
error = new HandlerError 'JsonHandler', filePath, error
22+
1623
return reject error
1724

1825
resolve [path.basename(filePath, '.json'), data]

src/handler/TextHandler.coffee

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ fs = require 'fs'
22
path = require 'path'
33
Promise = require 'bluebird'
44

5+
HandlerError = require './error/HandlerError'
6+
57
module.exports = class TextHandler
68

79
constructor: (@extensions = ['', '.txt']) ->
@@ -12,7 +14,10 @@ module.exports = class TextHandler
1214
return resolve null unless extension in @extensions
1315

1416
fs.readFile filePath, (error, data) ->
15-
return reject error if error
17+
if error
18+
error = new HandlerError 'TextHandler', filePath, error
19+
20+
return reject error
1621

1722
resolve [
1823
path.basename filePath, extension

src/handler/error/HandlerError.coffee

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = class HandlerError extends Error
2+
3+
constructor: (@handlerName, @filename, @cause) ->
4+
@message = "#{@handlerName} could not process '#{@filename}'."
5+
6+
toString: ->
7+
string = @message
8+
string += " Caused by:\n\n#{@cause.toString()}" if @cause
9+
10+
return string

test/suite/cli.spec.coffee

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Promise = require 'bluebird'
2+
3+
cli = require '../../src/cli'
4+
5+
describe 'CLI', ->
6+
7+
beforeEach ->
8+
@process =
9+
argv: ['a', 'b', 'c']
10+
exit: sinon.spy()
11+
@console = sinon.spyObject 'console', ['log', 'error']
12+
@builder = sinon.spyObject 'builder', ['build']
13+
14+
it 'outputs indented JSON', ->
15+
expected = '''
16+
{
17+
"a": 1,
18+
"b": 2
19+
}
20+
'''
21+
@builder.build.returns Promise.resolve a: 1, b: 2
22+
23+
return cli @process, @console, @builder
24+
.then =>
25+
sinon.assert.calledWith @builder.build, 'c'
26+
sinon.assert.calledWith @console.log, expected
27+
28+
it 'outputs error messages on failure', ->
29+
@builder.build.returns Promise.reject 'Error message.'
30+
31+
return cli @process, @console, @builder
32+
.then =>
33+
sinon.assert.calledWith @console.error, 'Error message.'
34+
35+
it 'requires a path argument', ->
36+
@process.argv = ['a', 'b']
37+
cli @process, @console, @builder
38+
39+
sinon.assert.calledWith @console.error, 'Path is required.'

test/suite/handler/CommonCoffeeHandler.spec.coffee

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CommonCoffeeHandler = require '../../../src/handler/CommonCoffeeHandler'
2+
HandlerError = require '../../../src/handler/error/HandlerError'
23

34
describe 'CommonCoffeeHandler', ->
45

@@ -45,11 +46,11 @@ describe 'CommonCoffeeHandler', ->
4546

4647
return @subject.handleFile path
4748
.catch (actual) ->
48-
assert.instanceOf actual, SyntaxError
49+
assert.instanceOf actual, HandlerError
4950

5051
it 'handles file system errors', ->
5152
path = "#{__dirname}/../../fixture/invalid/nonexistent.coffee"
5253

5354
return @subject.handleFile path
5455
.catch (actual) ->
55-
assert.instanceOf actual, Error
56+
assert.instanceOf actual, HandlerError

test/suite/handler/CommonJsHandler.spec.coffee

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CommonJsHandler = require '../../../src/handler/CommonJsHandler'
2+
HandlerError = require '../../../src/handler/error/HandlerError'
23

34
describe 'CommonJsHandler', ->
45

@@ -39,4 +40,4 @@ describe 'CommonJsHandler', ->
3940

4041
return @subject.handleFile path
4142
.catch (actual) ->
42-
assert.instanceOf actual, Error
43+
assert.instanceOf actual, HandlerError

0 commit comments

Comments
 (0)