Merge pull request #7350 from atom/ns-assertions

Add assertion mechanism
This commit is contained in:
Nathan Sobo
2015-06-22 12:18:44 -05:00
4 changed files with 110 additions and 35 deletions

View File

@@ -198,28 +198,31 @@ class Atom extends Model
initialize: ->
sourceMapCache = {}
window.onerror = =>
@lastUncaughtError = Array::slice.call(arguments)
[message, url, line, column, originalError] = @lastUncaughtError
window.onerror = (message, url, line, column, error) =>
@lastUncaughtError = error
convertedLine = convertLine(url, line, column, sourceMapCache)
{line, column} = convertedLine if convertedLine?
originalError.stack = convertStackTrace(originalError.stack, sourceMapCache) if originalError
eventObject = {message, url, line, column, originalError}
# TODO: These should be deprecated for 2.0 once we transition the
# exception-reporting package to the new API.
error ?= {}
error.message ?= message
error.url = url
error.line = line
error.column = column
error.originalError = error
openDevTools = true
eventObject.preventDefault = -> openDevTools = false
error.preventDefault = -> openDevTools = false
@emitter.emit 'will-throw-error', eventObject
# TODO: Deprecate onWillThrowError once we transition the notifications
# package to use onDidThrowError instead.
@emitter.emit 'will-throw-error', error
@emit 'uncaught-error', arguments... if includeDeprecatedAPIs
@emitter.emit 'did-throw-error', error
if openDevTools
@openDevTools()
@executeJavaScriptInDevTools('DevToolsAPI.showConsole()')
@emit 'uncaught-error', arguments... if includeDeprecatedAPIs
@emitter.emit 'did-throw-error', {message, url, line, column, originalError}
@disposables?.dispose()
@disposables = new CompositeDisposable
@@ -321,18 +324,16 @@ class Atom extends Model
# Extended: Invoke the given callback whenever there is an unhandled error.
#
# * `callback` {Function} to be called whenever there is an unhandled error
# * `event` {Object}
# * `originalError` {Object} the original error object
# * `message` {String} the original error object
# * `url` {String} Url to the file where the error originated.
# * `line` {Number}
# * `column` {Number}
# * `callback` {Function} to be called whenever there is an unhandled error.
# * `error` The unhandled {Error} object.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidThrowError: (callback) ->
@emitter.on 'did-throw-error', callback
onDidFailAssertion: (callback) ->
@emitter.on 'did-fail-assertion', callback
###
Section: Atom Details
###
@@ -715,6 +716,22 @@ class Atom extends Model
Section: Private
###
assert: (condition, message, metadata) ->
return true if condition
error = new Error("Assertion failed: " + message)
Error.captureStackTrace(error, @assert)
if metadata?
if typeof metadata is 'function'
error.metadata = metadata()
else
error.metadata = metadata
@emitter.emit 'did-fail-assertion', error
false
deserializeProject: ->
Project = require './project'

View File

@@ -292,7 +292,17 @@ class TokenizedBuffer extends Model
# Returns a {Boolean} indicating whether the given buffer row starts
# a a foldable row range due to the code's indentation patterns.
isFoldableCodeAtRow: (row) ->
return false if @buffer.isRowBlank(row) or @tokenizedLineForRow(row).isComment()
# Investigating an exception that's occurring here due to the line being
# undefined. This should paper over the problem but we want to figure out
# what is happening:
tokenizedLine = @tokenizedLineForRow(row)
atom.assert tokenizedLine?, "TokenizedLine is defined", =>
metadata:
row: row
rowCount: @tokenizedLines.length
return false unless tokenizedLine?
return false if @buffer.isRowBlank(row) or tokenizedLine.isComment()
nextRow = @buffer.nextNonBlankRow(row)
return false unless nextRow?