Skip to content

Commit

Permalink
Use more specific error types.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyoung committed Jun 4, 2014
1 parent 29cb7fb commit 83fd4bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
16 changes: 8 additions & 8 deletions lib/error-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ErrorReporter = (function() {
throw new Error('Source code not provided');
}
if (toString.call(sourceCode) !== '[object String]') {
throw new Error('Source code must be a string');
throw new TypeError('Source code must be a string');
}
this._sourceCode = sourceCode;
}
Expand All @@ -21,7 +21,7 @@ ErrorReporter = (function() {
throw new Error('Message not provided');
}
if (toString.call(message) !== '[object String]') {
throw new Error('Message must be a string');
throw new TypeError('Message must be a string');
}
if (message.length === 0) {
throw new Error('Message must not be empty');
Expand All @@ -30,27 +30,27 @@ ErrorReporter = (function() {
throw new Error('Line number not provided');
}
if (toString.call(lineNumber) !== '[object Number]') {
throw new Error('Line number must be a number');
throw new TypeError('Line number must be a number');
}
if (lineNumber <= 0) {
throw new Error('Line number is invalid');
throw new RangeError('Line number is invalid');
}
if (columnNumber == null) {
throw new Error('Column number not provided');
}
if (toString.call(columnNumber) !== '[object Number]') {
throw new Error('Column number must be a number');
throw new TypeError('Column number must be a number');
}
if (columnNumber <= 0) {
throw new Error('Column number is invalid');
throw new RangeError('Column number is invalid');
}
lines = this._sourceCode.split('\n');
if (lineNumber > lines.length) {
throw new Error('Line number is out of range');
throw new RangeError('Line number is out of range');
}
currentLine = lines[lineNumber - 1];
if (columnNumber > currentLine.length) {
throw new Error('Column number is out of range');
throw new RangeError('Column number is out of range');
}
error = [];
error.push("Error on line " + lineNumber + ", column " + columnNumber + ": " + message);
Expand Down
16 changes: 8 additions & 8 deletions spec/error-reporter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe 'Error reporter', ->
context 'with source code that is not a string', ->
it 'should throw an error', ->
exercise = -> new ErrorReporter({})
expect(exercise).to.throw Error, 'Source code must be a string'
expect(exercise).to.throw TypeError, 'Source code must be a string'

context 'with source code that is a string', ->
it 'should create an instance', ->
Expand All @@ -41,7 +41,7 @@ describe 'Error reporter', ->
it 'should throw an error', ->
errorReporter = new ErrorReporter 'a'
exercise = -> errorReporter.reportError {}, 1, 1
expect(exercise).to.throw Error, 'Message must be a string'
expect(exercise).to.throw TypeError, 'Message must be a string'

context 'that is empty', ->
it 'should throw an error', ->
Expand All @@ -62,19 +62,19 @@ describe 'Error reporter', ->
it 'should throw an error', ->
errorReporter = new ErrorReporter 'a'
exercise = -> errorReporter.reportError 'Test message', {}, 1
expect(exercise).to.throw Error, 'Line number must be a number'
expect(exercise).to.throw TypeError, 'Line number must be a number'

context 'that is invalid', ->
it 'should throw an error', ->
errorReporter = new ErrorReporter 'a'
exercise = -> errorReporter.reportError 'Test message', 0, 1
expect(exercise).to.throw Error, 'Line number is invalid'
expect(exercise).to.throw RangeError, 'Line number is invalid'

context 'that is out of range', ->
it 'should throw an error', ->
errorReporter = new ErrorReporter 'a'
exercise = -> errorReporter.reportError 'Test message', 2, 1
expect(exercise).to.throw Error, 'Line number is out of range'
expect(exercise).to.throw RangeError, 'Line number is out of range'


describe 'with a column number', ->
Expand All @@ -89,19 +89,19 @@ describe 'Error reporter', ->
it 'should throw an error', ->
errorReporter = new ErrorReporter 'a'
exercise = -> errorReporter.reportError 'Test message', 1, {}
expect(exercise).to.throw Error, 'Column number must be a number'
expect(exercise).to.throw TypeError, 'Column number must be a number'

context 'that is invalid', ->
it 'should throw an error', ->
errorReporter = new ErrorReporter 'a'
exercise = -> errorReporter.reportError 'Test message', 1, 0
expect(exercise).to.throw Error, 'Column number is invalid'
expect(exercise).to.throw RangeError, 'Column number is invalid'

context 'that is out of range', ->
it 'should throw an error', ->
errorReporter = new ErrorReporter 'a'
exercise = -> errorReporter.reportError 'Test message', 1, 2
expect(exercise).to.throw Error, 'Column number is out of range'
expect(exercise).to.throw RangeError, 'Column number is out of range'


context 'with valid parameters', ->
Expand Down
16 changes: 8 additions & 8 deletions src/error-reporter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ErrorReporter
throw new Error 'Source code not provided' unless sourceCode?

unless toString.call(sourceCode) is '[object String]'
throw new Error 'Source code must be a string'
throw new TypeError 'Source code must be a string'

@_sourceCode = sourceCode

Expand All @@ -47,34 +47,34 @@ class ErrorReporter
throw new Error 'Message not provided' unless message?

unless toString.call(message) is '[object String]'
throw new Error 'Message must be a string'
throw new TypeError 'Message must be a string'

throw new Error 'Message must not be empty' if message.length is 0


throw new Error 'Line number not provided' unless lineNumber?

unless toString.call(lineNumber) is '[object Number]'
throw new Error 'Line number must be a number'
throw new TypeError 'Line number must be a number'

throw new Error 'Line number is invalid' if lineNumber <= 0
throw new RangeError 'Line number is invalid' if lineNumber <= 0


throw new Error 'Column number not provided' unless columnNumber?

unless toString.call(columnNumber) is '[object Number]'
throw new Error 'Column number must be a number'
throw new TypeError 'Column number must be a number'

throw new Error 'Column number is invalid' if columnNumber <= 0
throw new RangeError 'Column number is invalid' if columnNumber <= 0


lines = @_sourceCode.split '\n'
throw new Error 'Line number is out of range' if lineNumber > lines.length
throw new RangeError 'Line number is out of range' if lineNumber > lines.length

currentLine = lines[lineNumber - 1]

if columnNumber > currentLine.length
throw new Error 'Column number is out of range'
throw new RangeError 'Column number is out of range'


error = []
Expand Down

0 comments on commit 83fd4bb

Please sign in to comment.