diff --git a/lib/error-reporter.js b/lib/error-reporter.js index 1283085..377df4d 100644 --- a/lib/error-reporter.js +++ b/lib/error-reporter.js @@ -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; } @@ -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'); @@ -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); diff --git a/spec/error-reporter.coffee b/spec/error-reporter.coffee index bc31c0e..0f20e09 100644 --- a/spec/error-reporter.coffee +++ b/spec/error-reporter.coffee @@ -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', -> @@ -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', -> @@ -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', -> @@ -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', -> diff --git a/src/error-reporter.coffee b/src/error-reporter.coffee index 5a00746..efd957f 100644 --- a/src/error-reporter.coffee +++ b/src/error-reporter.coffee @@ -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 @@ -47,7 +47,7 @@ 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 @@ -55,26 +55,26 @@ class ErrorReporter 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 = []