Bläddra i källkod

Improve IndexedDB error handling

Thibaut Courouble 8 år sedan
förälder
incheckning
d63aadb27e

+ 18 - 12
assets/javascripts/app/db.coffee

@@ -17,8 +17,8 @@ class app.DB
       req.onsuccess = @onOpenSuccess
       req.onerror = @onOpenError
       req.onupgradeneeded = @onUpgradeNeeded
-    catch
-      @onOpenError()
+    catch error
+      @fail 'error', error
     return
 
   onOpenSuccess: (event) =>
@@ -26,18 +26,16 @@ class app.DB
 
     if db.objectStoreNames.length is 0
       try db.close()
-      @reason = 'empty'
-      @onOpenError()
+      @fail 'empty'
       return
 
     unless @checkedBuggyIDB
       @checkedBuggyIDB = true
       try
         @idbTransaction(db, stores: $.makeArray(db.objectStoreNames)[0..1], mode: 'readwrite').abort() # https://bugs.webkit.org/show_bug.cgi?id=136937
-      catch
+      catch error
         try db.close()
-        @reason = 'apple'
-        @onOpenError()
+        @fail 'buggy', error
         return
 
     @runCallbacks(db)
@@ -46,17 +44,25 @@ class app.DB
     return
 
   onOpenError: (event) =>
-    event?.preventDefault()
+    event.preventDefault()
     @open = false
+    error = event.target.error
 
-    if event?.target?.error?.name is 'QuotaExceededError'
+    if error.name is 'QuotaExceededError'
       @reset()
       @db()
       app.onQuotaExceeded()
     else
-      @useIndexedDB = false
-      @reason or= 'cant_open'
-      @runCallbacks()
+      @fail 'cant_open', error
+    return
+
+  fail: (reason, error) ->
+    @useIndexedDB = false
+    @reason or= reason
+    @error or= error
+    console.error? 'IDB error', error if error
+    @runCallbacks()
+    Raven.captureException error, level: 'warning' if error
     return
 
   runCallbacks: (db) ->

+ 13 - 10
assets/javascripts/templates/error_tmpl.coffee

@@ -22,25 +22,28 @@ app.templates.bootError = ->
         """ Check your Internet connection and try <a href="#" data-behavior="reload">reloading</a>.<br>
             If you keep seeing this, you're likely behind a proxy or firewall that blocks cross-domain requests. """
 
-app.templates.offlineError = (reason) ->
+app.templates.offlineError = (reason, exception) ->
   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. """
+      """ DevDocs requires IndexedDB to cache documentations for offline access.<br>
+          Unfortunately your browser either doesn't support IndexedDB or doesn't make it available. """
+    when 'buggy'
+      """ DevDocs requires IndexedDB to cache documentations for offline access.<br>
+          Unfortunately your browser's implementation of IndexedDB contains bugs that prevent DevDocs from using it. """
+    when 'exception'
+      """ An error occured when trying to open the IndexedDB database:<br>
+          <code class="_label">#{exception.name}: #{exception.message}</code> """
     when 'cant_open'
-      """ Although your browser supports IndexedDB, DevDocs couldn't open the database.<br>
+      """ An error occured when trying to open the IndexedDB database:<br>
+          <code class="_label">#{exception.name}: #{exception.message}</code><br>
           This could be because you're browsing in private mode or have disallowed offline storage on the domain. """
     when 'empty'
-      """ Although your browser supports IndexedDB, DevDocs couldn't properly set up the database.<br>
-          This could be because the database is corrupted. Try <a href="#" data-behavior="reset">resetting the app</a>. """
-    when 'apple'
-      """ Unfortunately Safari's implementation of IndexedDB is <a href="https://bugs.webkit.org/show_bug.cgi?id=136937">badly broken</a>.<br>
-          This message will automatically go away when Apple fix their code. """
+      """ The IndexedDB database appears to be corrupted. Try <a href="#" data-behavior="reset">resetting the app</a>. """
 
-  error """ Offline mode is unavailable. """,
-        """ DevDocs requires IndexedDB to cache documentations for offline access.<br>#{reason} """
+  error 'Offline mode is unavailable.', reason
 
 app.templates.unsupportedBrowser = """
   <div class="_fail">

+ 1 - 1
assets/javascripts/views/content/offline_page.coffee

@@ -18,7 +18,7 @@ class app.views.OfflinePage extends app.View
     app.docs.getInstallStatuses (statuses) =>
       return unless @activated
       if statuses is false
-        @html @tmpl('offlineError', app.db.reason)
+        @html @tmpl('offlineError', app.db.reason, app.db.error)
       else
         html = ''
         html += @renderDoc(doc, statuses[doc.slug]) for doc in app.docs.all()