Skip to content

Instantly share code, notes, and snippets.

@ujovlado
Forked from steida/gist:1d10d6fbd6930b602507
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ujovlado/ead03ce4eb137bb7a8bf to your computer and use it in GitHub Desktop.
Save ujovlado/ead03ce4eb137bb7a8bf to your computer and use it in GitHub Desktop.
goog.provide 'app.errors.Store'
goog.require 'app.errors.Error'
goog.require 'este.Store'
goog.require 'goog.Promise'
goog.require 'goog.array'
goog.require 'goog.async.throwException'
goog.require 'goog.debug.ErrorReporter'
goog.require 'goog.labs.net.xhr.Error'
class app.errors.Store extends este.Store
###*
@param {app.Routes} routes
@param {este.Dispatcher} dispatcher
@constructor
@extends {este.Store}
###
constructor: (@routes, dispatcher) ->
@name = 'errors' if goog.DEBUG
super()
###*
@type {Array<Object>}
@private
###
@errors_ = []
if !goog.DEBUG
###*
@type {goog.debug.ErrorReporter}
@private
###
@reporter_ = goog.debug.ErrorReporter.install @routes.api.clientErrors.url()
###*
Should be set after login and deleted after logout.
TODO: Listen auth? Check it and backport to songary.
@type {string}
###
@userName = ''
goog.Promise.setUnhandledRejectionHandler @unhandledRejectionHandler_.bind @
dispatcher.onError = (action, reason) =>
@errors_.push 'action': action, 'reason': reason
###*
@param {*} reason
@private
###
unhandledRejectionHandler_: (reason) ->
action = goog.array.last(@errors_)?.action
message = @getMessage_ action, reason
# TODO: Use React instead of alert.
if !goog.DEBUG
alert message
if @shouldNotBeReported_
# Don't throw, just log.
console.log action if action
console.log reason
else
if @reporter_
@reporter_.setAdditionalArguments
'action': action
'errors': @errors_
'userName': @userName
# Throw exception async to be caught by goog.debug.ErrorReporter without
# interrupting current dispatching.
goog.async.throwException reason
###*
@param {string} action
@param {*} reason
@return {string}
@private
###
getMessage_: (action, reason) ->
Store.MSG_APP_ERROR = goog.getMsg 'Action{$action}failed because: {$reason}',
'action': if action then " #{action} " else ' '
'reason': reason
###*
@param {*} reason
@return {boolean}
@private
###
shouldNotBeReported_: (reason) ->
reason instanceof app.errors.Error ||
reason instanceof goog.labs.net.xhr.Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment