From 3eb5a0caaac57edc2d8d22368cc542fbb97f7c0f Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sun, 18 Sep 2016 15:18:44 -0400 Subject: [PATCH] Better error handling when cookies are disabled --- assets/javascripts/app/app.coffee | 10 ++++++++++ assets/javascripts/lib/cookie_store.coffee | 4 +++- assets/javascripts/templates/error_tmpl.coffee | 3 +++ assets/javascripts/templates/notif_tmpl.coffee | 4 ++++ assets/javascripts/views/content/offline_page.coffee | 4 ++++ 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/assets/javascripts/app/app.coffee b/assets/javascripts/app/app.coffee index c2798ab2..b700d611 100644 --- a/assets/javascripts/app/app.coffee +++ b/assets/javascripts/app/app.coffee @@ -75,6 +75,7 @@ .install() @previousErrorHandler = onerror window.onerror = @onWindowError.bind(@) + CookieStore.onBlocked = @onCookieBlocked return bootOne: -> @@ -207,8 +208,17 @@ @quotaExceeded = true new app.views.Notif 'QuotaExceeded', autoHide: null Raven.captureMessage 'QuotaExceededError', level: 'warning' + return + + onCookieBlocked: (key, value, actual) -> + return if @cookieBlocked + @cookieBlocked = true + new app.views.Notif 'CookieBlocked', autoHide: null + Raven.captureMessage "CookieBlocked/#{key}", level: 'warning', extra: {value, actual} + return onWindowError: (args...) -> + return if @cookieBlocked if @isInjectionError args... @onInjectionError() else if @isAppError args... diff --git a/assets/javascripts/lib/cookie_store.coffee b/assets/javascripts/lib/cookie_store.coffee index 1e033d5d..9cfb4099 100644 --- a/assets/javascripts/lib/cookie_store.coffee +++ b/assets/javascripts/lib/cookie_store.coffee @@ -1,6 +1,8 @@ class @CookieStore INT = /^\d+$/ + @onBlocked: -> + get: (key) -> value = Cookies.get(key) value = parseInt(value, 10) if value? and INT.test(value) @@ -13,7 +15,7 @@ class @CookieStore value = 1 if value == true Cookies.set(key, '' + value, path: '/', expires: 1e8) - throw new Error("Failed to set cookie '#{key}'") unless @get(key) == value + @constructor.onBlocked(key, value, @get(key)) if @get(key) != value return del: (key) -> diff --git a/assets/javascripts/templates/error_tmpl.coffee b/assets/javascripts/templates/error_tmpl.coffee index e2caf83d..a6a8e974 100644 --- a/assets/javascripts/templates/error_tmpl.coffee +++ b/assets/javascripts/templates/error_tmpl.coffee @@ -23,6 +23,9 @@ app.templates.bootError = -> If you keep seeing this, you're likely behind a proxy or firewall that blocks cross-domain requests. """ app.templates.offlineError = (reason) -> + if reason is 'cookie_blocked' + return error """ Cookies must be enabled to use offline mode. """ + reason = switch reason when 'not_supported' """ Unfortunately your browser either doesn't support IndexedDB or does not make it available. """ diff --git a/assets/javascripts/templates/notif_tmpl.coffee b/assets/javascripts/templates/notif_tmpl.coffee index 3dcfd1b4..73a3352c 100644 --- a/assets/javascripts/templates/notif_tmpl.coffee +++ b/assets/javascripts/templates/notif_tmpl.coffee @@ -19,6 +19,10 @@ app.templates.notifQuotaExceeded = -> textNotif """ The offline database has exceeded its size limitation. """, """ Unfortunately this quota can't be detected programmatically, and the database can't be opened while over the quota, so it had to be reset. """ +app.templates.notifCookieBlocked = -> + textNotif """ Please enable cookies. """, + """ DevDocs will not work properly if cookies are disabled. """ + app.templates.notifInvalidLocation = -> textNotif """ DevDocs must be loaded from #{app.config.production_host} """, """ Otherwise things are likely to break. """ diff --git a/assets/javascripts/views/content/offline_page.coffee b/assets/javascripts/views/content/offline_page.coffee index bfba16a6..20fcbaee 100644 --- a/assets/javascripts/views/content/offline_page.coffee +++ b/assets/javascripts/views/content/offline_page.coffee @@ -11,6 +11,10 @@ class app.views.OfflinePage extends app.View return render: -> + if app.cookieBlocked + @html @tmpl('offlineError', 'cookie_blocked') + return + app.docs.getInstallStatuses (statuses) => return unless @activated if statuses is false