codecombat/app/lib/world/errors.coffee

47 lines
1.8 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
Vector = require './vector'
module.exports.ArgumentError = class ArgumentError extends Error
2014-06-30 22:16:26 -04:00
@className: 'ArgumentError'
constructor: (@message, @functionName, @argumentName, @intendedType, @actualValue, @numArguments, @hint) ->
super @message
2014-06-30 22:16:26 -04:00
@name = 'ArgumentError'
2014-01-03 13:32:13 -05:00
if Error.captureStackTrace?
Error.captureStackTrace @, @constructor
toString: ->
s = "#{@functionName}"
2014-06-30 22:16:26 -04:00
if @argumentName is 'return'
2014-01-03 13:32:13 -05:00
s += "'s return value"
2014-06-30 22:16:26 -04:00
else if @argumentName is '_excess'
2014-01-03 13:32:13 -05:00
s += " takes only #{@numArguments} argument#{if @numArguments > 1 then 's' else ''}."
else if @argumentName
s += "'s argument #{@argumentName}"
else
2014-06-30 22:16:26 -04:00
s += ' takes no arguments.'
2014-01-03 13:32:13 -05:00
actualType = typeof @actualValue
if not @actualValue?
actualType = 'null'
else if _.isArray @actualValue
actualType = 'array'
typeMismatch = @intendedType and not @intendedType.match actualType
if typeMismatch
2014-06-30 22:16:26 -04:00
v = ''
2014-01-03 13:32:13 -05:00
if actualType is 'string'
v = "\"#{@actualValue}\""
2014-06-30 22:16:26 -04:00
else if actualType is 'number'
2014-01-03 13:32:13 -05:00
if Math.round(@actualValue) is @actualValue then @actualValue else @actualValue.toFixed(2)
2014-06-30 22:16:26 -04:00
else if actualType is 'boolean'
2014-01-03 13:32:13 -05:00
v = "#{@actualValue}"
else if (@actualValue? and @actualValue.id and @actualValue.trackedPropertiesKeys)
# (Don't import Thang, but determine whether it is Thang.)
v = @actualValue.toString()
else if @actualValue instanceof Vector
v = @actualValue.toString()
showValue = showValue or @actualValue instanceof Vector
s += " should have type #{@intendedType}, but got #{actualType}#{if v then ': ' + v else ''}."
2014-06-30 22:16:26 -04:00
else if @argumentName and @argumentName isnt '_excess'
s += ' has a problem.'
2014-01-03 13:32:13 -05:00
s += '\n' + @message if @message
s