{"record":{"id":"4d82dbc9b95139ba","repo":"SillyTavern/SillyTavern","slug":"bfl-failed-to-generate-image","errorCode":null,"errorMessage":"BFL failed to generate image.","messagePattern":"BFL failed to generate image\\.","errorType":"exception","errorClass":"Error","httpStatus":500,"severity":"error","filePath":"src/endpoints/stable-diffusion.js","lineNumber":1576,"sourceCode":"                return response.sendStatus(500);\n            }\n\n            /** @type {any} */\n            const statusData = await statusResult.json();\n\n            if (statusData?.status === 'Pending') {\n                continue;\n            }\n\n            if (statusData?.status === 'Ready') {\n                const { sample } = statusData.result;\n                const fetchResult = await fetch(sample);\n                const fetchData = await fetchResult.arrayBuffer();\n                const image = Buffer.from(fetchData).toString('base64');\n                return response.send({ image: image });\n            }\n\n            throw new Error('BFL failed to generate image.', { cause: statusData });\n        }\n    } catch (error) {\n        console.error(error);\n        return response.sendStatus(500);\n    }\n});\n\nconst falai = express.Router();\n\nfalai.post('/models', async (_request, response) => {\n    try {\n        const modelsUrl = new URL('https://fal.ai/api/models?categories=text-to-image');\n        let page = 1;\n        /** @type {any} */\n        let modelsResponse;\n        let models = [];\n\n        do {","sourceCodeStart":1558,"sourceCodeEnd":1594,"githubUrl":"https://github.com/SillyTavern/SillyTavern/blob/8172dcd0ee672d3cd9a5e5f7af134f91a45cd2b8/src/endpoints/stable-diffusion.js#L1558-L1594","documentation":"Explicit throw: the BFL task status was neither 'Pending' nor 'Ready' (e.g. 'Failed', 'Error', 'Cancelled', or an unrecognised status string). The thrown Error carries statusData as its cause; it propagates to the catch at 1579 which console.errors it and returns a bare 500, discarding the cause for the client.","triggerScenarios":"BFL content-moderation rejection; task failed server-side (GPU error); status field renamed so neither branch matches; task cancelled.","commonSituations":"Prompt tripped Flux safety filters; upstream model crash; API version introducing a new status value the code does not enumerate.","solutions":["Enumerate known failure statuses ('Failed','Error') and return a descriptive 4xx/5xx instead of a generic 500.","Return error.cause (the statusData) to the client like the FAL.AI handler does at line 1725.","Retry on transient failure statuses rather than throwing immediately."],"exampleFix":"// before\n            throw new Error('BFL failed to generate image.', { cause: statusData });\n        }\n    } catch (error) {\n        console.error(error);\n        return response.sendStatus(500);\n    }\n// after\n            return response.status(502).send({ error: 'BFL failed to generate image.', status: statusData });\n        }\n    } catch (error) {\n        console.error(error);\n        return response.status(500).send(error.cause || error.message);\n    }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"/** @param {any} s */\nfunction isBflFailureStatus(s) {\n  return typeof s === 'string' && !['Pending', 'Ready'].includes(s);\n}","tryCatchPattern":"// handle known statuses explicitly instead of throwing into a bare-500 catch\nif (!['Pending', 'Ready'].includes(statusData?.status)) {\n  console.warn('BFL terminal status', statusData?.status, statusData);\n  return response.status(502).send({ error: 'BFL failed to generate image.', status: statusData });\n}","preventionTips":["Enumerate all expected statuses (Pending/Ready/Failed/Error) explicitly.","Return the cause (statusData) to the client rather than swallowing it.","Retry transient failure statuses before giving up."],"tags":["polling","task-status","bfl","error-handling"],"backgroundTag":null,"analyzedSha":"8172dcd0ee672d3cd9a5e5f7af134f91a45cd2b8","analyzedAt":"2026-08-13T07:48:40.832Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}