consolidate error handling code

This commit is contained in:
ccd0 2014-07-22 01:58:40 -07:00
parent d5d69b18b8
commit 4821822562
4 changed files with 61 additions and 118 deletions

View File

@ -178,33 +178,9 @@ Gallery =
Gallery.build @
error: (file, thumb) ->
post = g.posts[file.dataset.post]
src = file.src.split '/'
if src[2] is 'i.4cdn.org'
URL = Redirect.to 'file',
boardID: src[3]
filename: src[src.length - 1]
if URL
thumb.href = URL
if URL and (/^https:\/\//.test(URL) or location.protocol is 'http:')
return unless Gallery.nodes.current is file
file.src = URL
return
if g.DEAD or post.isDead or post.file.isDead
return
# XXX CORS for i.4cdn.org WHEN?
$.ajax "//a.4cdn.org/#{post.board}/thread/#{post.thread}.json", onload: ->
return if @status isnt 200
i = 0
{posts} = @response
while postObj = posts[i++]
break if postObj.no is post.ID
unless postObj.no
return post.kill()
if postObj.filedeleted
post.kill true
ImageCommon.error file, g.posts[file.dataset.post], ->
thumb.href = URL
file.src = URL if Gallery.nodes.current is file
prev: ->
Gallery.cb.open.call(

View File

@ -0,0 +1,46 @@
ImageCommon =
error: (file, post, redirect, reload) ->
if file.error and file.error.code isnt @error.MEDIA_ERR_NETWORK # video
error = switch file.error.code
when 1 then 'MEDIA_ERR_ABORTED'
when 3 then 'MEDIA_ERR_DECODE'
when 4 then 'MEDIA_ERR_SRC_NOT_SUPPORTED'
when 5 then 'MEDIA_ERR_ENCRYPTED'
unless message = $ '.warning', post.file.thumb.parentNode
message = $.el 'div', className: 'warning'
$.after post.file.thumb, message
message.textContent = "Playback error: #{error}"
return
src = file.src.split '/'
if src[2] is 'i.4cdn.org'
URL = Redirect.to 'file',
boardID: src[3]
filename: src[src.length - 1].replace /\?.+$/, ''
if URL and (/^https:\/\//.test(URL) or location.protocol is 'http:')
return redirect URL
if g.DEAD or post.isDead or post.file.isDead
return
timeoutID = reload?()
<% if (type === 'crx') { %>
$.ajax post.file.URL,
onloadend: ->
return if @status isnt 404
clearTimeout timeoutID
post.kill true
,
type: 'head'
<% } else { %>
# XXX CORS for i.4cdn.org WHEN?
$.ajax "//a.4cdn.org/#{post.board}/thread/#{post.thread}.json", onload: ->
return if @status isnt 200
for postObj in @response.posts
break if postObj.no is post.ID
if postObj.no isnt post.ID
clearTimeout timeoutID
post.kill()
else if postObj.filedeleted
clearTimeout timeoutID
post.kill true
<% } %>

View File

@ -18,8 +18,6 @@ ImageExpand =
{thumb} = @file
$.on thumb.parentNode, 'click', ImageExpand.cb.toggle
if @isClone
if @file.error
@file.error = $ '.warning', @file.thumb.parentNode
if $.hasClass thumb, 'expanding'
# If we clone a post where the image is still loading,
# make it loading in the clone too.
@ -122,20 +120,17 @@ ImageExpand =
{thumb, isVideo} = post.file
return if post.isHidden or post.file.isExpanded or $.hasClass thumb, 'expanding'
$.addClass thumb, 'expanding'
if el = post.file.fullImage
el = post.file.fullImage or $.el (if isVideo then 'video' else 'img'), className: 'full-image'
$.on el, 'error', ImageExpand.error
if post.file.fullImage
# Expand already-loaded/ing picture.
TrashQueue.remove el
else
el = post.file.fullImage = $.el (if isVideo then 'video' else 'img'),
className: 'full-image'
$.on el, 'error', ImageExpand.error
el.src = src or post.file.URL
$.after thumb, el unless el is thumb.nextSibling
$.asap (-> el.videoHeight or el.naturalHeight), ->
$.after thumb, el
post.file.fullImage = el
$.asap (-> if isVideo then el.readyState >= el.HAVE_CURRENT_DATA else el.naturalHeight), ->
ImageExpand.completeExpand post, disableAutoplay
if post.file.error
$.rm post.file.error
delete post.file.error
completeExpand: (post, disableAutoplay) ->
return unless $.hasClass post.file.thumb, 'expanding' # contracted before the image loaded
@ -204,52 +199,9 @@ ImageExpand =
# Don't try to re-expand if it was already contracted.
return
ImageExpand.contract post
if @error and @error.code isnt @error.MEDIA_ERR_NETWORK # video
error = switch @error.code
when 1 then 'MEDIA_ERR_ABORTED'
when 3 then 'MEDIA_ERR_DECODE'
when 4 then 'MEDIA_ERR_SRC_NOT_SUPPORTED'
when 5 then 'MEDIA_ERR_ENCRYPTED'
post.file.error = $.el 'div',
textContent: "Playback error: #{error}"
className: 'warning'
$.after post.file.thumb, post.file.error
return
src = @src.split '/'
if src[2] is 'i.4cdn.org'
URL = Redirect.to 'file',
boardID: src[3]
filename: src[src.length - 1]
if URL and (/^https:\/\//.test(URL) or location.protocol is 'http:')
setTimeout ImageExpand.expand, 10000, post, URL
return
if g.DEAD or post.isDead or post.file.isDead
return
timeoutID = setTimeout ImageExpand.expand, 10000, post
<% if (type === 'crx') { %>
$.ajax @src,
onloadend: ->
return if @status isnt 404
clearTimeout timeoutID
post.kill true
,
type: 'head'
<% } else { %>
# XXX CORS for i.4cdn.org WHEN?
$.ajax "//a.4cdn.org/#{post.board}/thread/#{post.thread}.json", onload: ->
return if @status isnt 200
for postObj in @response.posts
break if postObj.no is post.ID
if postObj.no isnt post.ID
clearTimeout timeoutID
post.kill()
else if postObj.filedeleted
clearTimeout timeoutID
post.kill true
<% } %>
ImageCommon.error @, post,
((URL) -> setTimeout ImageExpand.expand, 10 * $.SECOND, post, URL),
(-> setTimeout ImageExpand.expand, 10 * $.SECOND, post)
menu:
init: ->

View File

@ -43,37 +43,6 @@ ImageHover =
error: ->
return unless doc.contains @
post = g.posts[@dataset.fullID]
src = @src.split '/'
if src[2] is 'i.4cdn.org'
URL = Redirect.to 'file',
boardID: src[3]
filename: src[src.length - 1].replace /\?.+$/, ''
if URL and (/^https:\/\//.test(URL) or location.protocol is 'http:')
@src = URL
return
if g.DEAD or post.isDead or post.file.isDead
return
timeoutID = setTimeout (=> @src = post.file.URL + '?' + Date.now()), 3000
<% if (type === 'crx') { %>
$.ajax @src,
onloadend: ->
return if @status isnt 404
clearTimeout timeoutID
post.kill true
,
type: 'head'
<% } else { %>
# XXX CORS for i.4cdn.org WHEN?
$.ajax "//a.4cdn.org/#{post.board}/thread/#{post.thread}.json", onload: ->
return if @status isnt 200
for postObj in @response.posts
break if postObj.no is post.ID
if postObj.no isnt post.ID
clearTimeout timeoutID
post.kill()
else if postObj.filedeleted
clearTimeout timeoutID
post.kill true
<% } %>
ImageCommon.error @, post,
((URL) => @src = URL),
(=> setTimeout (=> @src = post.file.URL + '?' + Date.now()), 3 * $.SECOND)