{"record":{"id":"9c0f31bba5a2f7ea","repo":"bilibili/flv.js","slug":"fetchstreamloader-http-code-invalid-res-stat","errorCode":null,"errorMessage":"FetchStreamLoader: Http code invalid, ' + res.status + ' ' + res.statusText","messagePattern":"FetchStreamLoader: Http code invalid, ' \\+ res\\.status \\+ ' ' \\+ res\\.statusText","errorType":"http","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/io/fetch-stream-loader.js","lineNumber":157,"sourceCode":"                }\n\n                let lengthHeader = res.headers.get('Content-Length');\n                if (lengthHeader != null) {\n                    this._contentLength = parseInt(lengthHeader);\n                    if (this._contentLength !== 0) {\n                        if (this._onContentLengthKnown) {\n                            this._onContentLengthKnown(this._contentLength);\n                        }\n                    }\n                }\n\n                return this._pump.call(this, res.body.getReader());\n            } else {\n                this._status = LoaderStatus.kError;\n                if (this._onError) {\n                    this._onError(LoaderErrors.HTTP_STATUS_CODE_INVALID, {code: res.status, msg: res.statusText});\n                } else {\n                    throw new RuntimeException('FetchStreamLoader: Http code invalid, ' + res.status + ' ' + res.statusText);\n                }\n            }\n        }).catch((e) => {\n            if (this._abortController && this._abortController.signal.aborted) {\n                return;\n            }\n\n            this._status = LoaderStatus.kError;\n            if (this._onError) {\n                this._onError(LoaderErrors.EXCEPTION, {code: -1, msg: e.message});\n            } else {\n                throw e;\n            }\n        });\n    }\n\n    abort() {\n        this._requestAbort = true;","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/bilibili/flv.js/blob/42343088f22619cf014a057b3878fe3d320016a6/src/io/fetch-stream-loader.js#L139-L175","documentation":"FetchStreamLoader.open() checks the fetch() Response; if response.ok is false (non-2xx HTTP status) the loader enters kError state. Normally it reports the failure via the onError callback with LoaderErrors.HTTP_STATUS_CODE_INVALID, but when no onError handler is registered it throws RuntimeException 'FetchStreamLoader: Http code invalid, <status> <statusText>'. It is the library surfacing an HTTP-level rejection from the streaming fetch.","triggerScenarios":"Fetching an FLV stream URL that returns 404 (file missing), 403 (expired signed URL / wrong token), 5xx (server error), or any non-2xx status, while this._onError is undefined so the else branch throws instead.","commonSituations":"Expired CDN signatures or auth cookies; CORS misconfiguration surfacing as unexpected status; mis-typed stream URL; forgetting to register loader onError in a custom io pipeline so failures throw instead of being reported.","solutions":["Fix the server-side cause so the URL returns 200 (valid token, correct path, available stream)","Register an onError handler on the loader to receive LoaderErrors.HTTP_STATUS_CODE_INVALID gracefully instead of a thrown exception","Check the URL and credentials in devtools/network tab and retry with a corrected or refreshed URL"],"exampleFix":"// before\nconst loader = new FetchStreamLoader();\nloader.open(...); // throws on 404 because no onError set\n// after\nconst loader = new FetchStreamLoader();\nloader.onError = (type, data) => console.error('HTTP', data.code, data.msg);\nloader.open(...);","handlingStrategy":"try-catch","validationCode":"const res = await fetch(url, {method: 'HEAD'});\nif (!res.ok) {\n  throw new Error(`Stream URL unavailable: ${res.status} ${res.statusText}`);\n}","typeGuard":"function isOkayResponse(res) {\n  return typeof res !== 'undefined' && res !== null && typeof res.ok === 'boolean' && res.ok === true;\n}","tryCatchPattern":"loader.onError = (type, data) => {\n  if (type === LoaderErrors.HTTP_STATUS_CODE_INVALID) {\n    console.error(`Stream fetch failed: ${data.code} ${data.msg}`);\n    scheduleRetryWithBackoff();\n  }\n};\ntry {\n  loader.open(url);\n} catch (e) {\n  if (e instanceof RuntimeException && e.message.includes('Http code invalid')) {\n    console.error('Fetch failed without onError handler registered');\n  } else { throw e; }\n}","preventionTips":["Always register onError on loaders so HTTP failures are reported, not thrown","Check the URL, auth token, and CORS headers return 200 (verify with curl/HEAD)","Refresh signed/expired CDN URLs before reopening the stream","Add retry with backoff for transient 5xx responses"],"tags":["network","http","fetch","status-code"],"backgroundTag":"invalid-http-status-code","analyzedSha":"42343088f22619cf014a057b3878fe3d320016a6","analyzedAt":"2026-09-01T00:27:28.147Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}