Fixed an old hidden parsing crash that was there but hidden by the old try/catch block.

This commit is contained in:
Marc-Andre Ferland 2022-10-11 15:32:06 -04:00
parent d1fa13d67a
commit 83b0239791

View File

@ -471,7 +471,8 @@ async function doMakeImage(task) {
const previewPrompt = task['previewPrompt'] const previewPrompt = task['previewPrompt']
const progressBar = task['progressBar'] const progressBar = task['progressBar']
let res = '' let res = undefined
let stepUpdate = undefined
try { try {
res = await fetch('/image', { res = await fetch('/image', {
method: 'POST', method: 'POST',
@ -497,9 +498,21 @@ async function doMakeImage(task) {
let timeTaken = (prevTime === -1 ? -1 : t - prevTime) let timeTaken = (prevTime === -1 ? -1 : t - prevTime)
let jsonStr = textDecoder.decode(value) let jsonStr = textDecoder.decode(value)
let stepUpdate = undefined
try { try {
stepUpdate = JSON.parse(jsonStr) // hack for a middleman buffering all the streaming updates, and unleashing them on the poor browser in one shot.
// this results in having to parse JSON like {"step": 1}{"step": 2}...{"status": "succeeded"..}
// which is obviously invalid.
// So we need to just extract the last {} section, starting from "status" to the end of the response
let lastChunkIdx = jsonStr.indexOf('}{')
if (lastChunkIdx !== -1) {
finalJSON += jsonStr.substring(0, lastChunkIdx + 1)
jsonStr = jsonStr.substring(lastChunkIdx + 2)
} else {
finalJSON += jsonStr
jsonStr = ''
}
stepUpdate = JSON.parse(finalJSON)
finalJSON = jsonStr
} catch (e) { } catch (e) {
if (e instanceof SyntaxError) { if (e instanceof SyntaxError) {
finalJSON += jsonStr finalJSON += jsonStr
@ -507,9 +520,7 @@ async function doMakeImage(task) {
throw e throw e
} }
} }
if (!stepUpdate || stepUpdate.step === undefined) { if (typeof stepUpdate === 'object' && 'step' in stepUpdate) {
finalJSON += jsonStr
} else {
let batchSize = stepUpdate.total_steps let batchSize = stepUpdate.total_steps
let overallStepCount = stepUpdate.step + task.batchesDone * batchSize let overallStepCount = stepUpdate.step + task.batchesDone * batchSize
let totalSteps = batchCount * batchSize let totalSteps = batchCount * batchSize
@ -531,6 +542,8 @@ async function doMakeImage(task) {
if (stepUpdate.output !== undefined) { if (stepUpdate.output !== undefined) {
showImages(reqBody, stepUpdate, outputContainer, true) showImages(reqBody, stepUpdate, outputContainer, true)
} }
} else {
finalJSON = jsonStr
} }
prevTime = t prevTime = t
@ -541,7 +554,7 @@ async function doMakeImage(task) {
} }
} }
if (res.status != 200) { if (!res || res.status != 200 || !stepUpdate) {
if (serverStatus === 'online') { if (serverStatus === 'online') {
logError('Stable Diffusion had an error: ' + await res.text(), res, outputMsg) logError('Stable Diffusion had an error: ' + await res.text(), res, outputMsg)
} else { } else {
@ -549,42 +562,24 @@ async function doMakeImage(task) {
} }
res = undefined res = undefined
progressBar.style.display = 'none' progressBar.style.display = 'none'
} else { } else if (stepUpdate.status !== 'succeeded') {
if (finalJSON !== undefined && finalJSON.indexOf('}{') !== -1) { let msg = ''
// hack for a middleman buffering all the streaming updates, and unleashing them if (res.detail !== undefined) {
// on the poor browser in one shot. msg = res.detail
// this results in having to parse JSON like {"step": 1}{"step": 2}...{"status": "succeeded"..}
// which is obviously invalid.
// So we need to just extract the last {} section, starting from "status" to the end of the response
let lastChunkIdx = finalJSON.lastIndexOf('}{') if (msg.toLowerCase().includes('out of memory')) {
if (lastChunkIdx !== -1) { msg += `<br/><br/>
let remaining = finalJSON.substring(lastChunkIdx) <b>Suggestions</b>:
finalJSON = remaining.substring(1) <br/>
1. If you have set an initial image, please try reducing its dimension to ${MAX_INIT_IMAGE_DIMENSION}x${MAX_INIT_IMAGE_DIMENSION} or smaller.<br/>
2. Try disabling the '<em>Turbo mode</em>' under '<em>Advanced Settings</em>'.<br/>
3. Try generating a smaller image.<br/>`
} }
} else {
msg = res
} }
logError(msg, res, outputMsg)
res = JSON.parse(finalJSON) res = undefined
if (res.status !== 'succeeded') {
let msg = ''
if (res.detail !== undefined) {
msg = res.detail
if (msg.toLowerCase().includes('out of memory')) {
msg += `<br/><br/>
<b>Suggestions</b>:
<br/>
1. If you have set an initial image, please try reducing its dimension to ${MAX_INIT_IMAGE_DIMENSION}x${MAX_INIT_IMAGE_DIMENSION} or smaller.<br/>
2. Try disabling the '<em>Turbo mode</em>' under '<em>Advanced Settings</em>'.<br/>
3. Try generating a smaller image.<br/>`
}
} else {
msg = res
}
logError(msg, res, outputMsg)
res = undefined
}
} }
} catch (e) { } catch (e) {
console.log('request error', e) console.log('request error', e)
@ -594,11 +589,11 @@ async function doMakeImage(task) {
res = undefined res = undefined
} }
if (!res) return false if (!stepUpdate) return false
lastPromptUsed = reqBody['prompt'] lastPromptUsed = reqBody['prompt']
showImages(reqBody, res, outputContainer, false) showImages(reqBody, stepUpdate, outputContainer, false)
return true return true
} }