codecombat/app/core/errors.coffee

58 lines
1.8 KiB
CoffeeScript
Raw Normal View History

errorModalTemplate = require 'templates/core/error'
{applyErrorsToForm} = require 'core/forms'
2014-01-03 13:32:13 -05:00
module.exports.parseServerError = (text) ->
try
2014-06-30 22:16:26 -04:00
error = JSON.parse(text) or {message: 'Unknown error.'}
2014-01-03 13:32:13 -05:00
catch SyntaxError
2014-06-30 22:16:26 -04:00
error = {message: text or 'Unknown error.'}
2014-01-03 13:32:13 -05:00
error = error[0] if _.isArray(error)
error
module.exports.genericFailure = (jqxhr) ->
2014-09-03 20:47:29 -04:00
Backbone.Mediator.publish('errors:server-error', {response: jqxhr})
2014-01-03 13:32:13 -05:00
return connectionFailure() if not jqxhr.status
error = module.exports.parseServerError(jqxhr.responseText)
message = error.message
message = error.property + ' ' + message if error.property
console.warn(jqxhr.status, jqxhr.statusText, error)
2014-02-16 15:51:20 -05:00
existingForm = $('.form:visible:first')
2014-01-03 13:32:13 -05:00
if existingForm[0]
missingErrors = applyErrorsToForm(existingForm, [error])
for error in missingErrors
2014-03-04 15:06:31 -05:00
existingForm.append($('<div class="alert alert-danger"></div>').text(error.message))
2014-01-03 13:32:13 -05:00
else
2014-02-16 15:51:20 -05:00
res = errorModalTemplate(
2014-06-30 22:16:26 -04:00
status: jqxhr.status
statusText: jqxhr.statusText
2014-02-16 15:51:20 -05:00
message: message
)
2014-01-03 13:32:13 -05:00
showErrorModal(res)
module.exports.backboneFailure = (model, jqxhr, options) ->
module.exports.genericFailure(jqxhr)
module.exports.connectionFailure = connectionFailure = ->
html = errorModalTemplate(
status: 0
2014-06-30 22:16:26 -04:00
statusText: 'Connection Gone'
2014-01-03 13:32:13 -05:00
message: 'No response from the CoCo servers, captain.'
)
showErrorModal(html)
module.exports.showNotyNetworkError = (jqxhr) ->
noty({
text: jqxhr.responseText or 'Unknown error'
layout: 'topCenter'
type: 'error'
killer: false,
dismissQueue: true
})
2014-01-03 13:32:13 -05:00
showErrorModal = (html) ->
# TODO: make a views/modal/error_modal view for this to use so the template can reuse templates/core/modal-base?
2014-01-03 13:32:13 -05:00
$('#modal-wrapper').html(html)
$('.modal:visible').modal('hide')
$('#modal-error').modal('show')