{"id":"21e0e2ac92f544c2","repo":"sindresorhus/got","slug":"the-request-function-must-return-a-value","errorCode":null,"errorMessage":"The request function must return a value","messagePattern":"The request function must return a value","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/core/options.ts","lineNumber":3632,"sourceCode":"\t\t\tif (this.#internals.http2) {\n\t\t\t\treturn http2Client.auto as RequestFunction;\n\t\t\t}\n\n\t\t\treturn https.request;\n\t\t}\n\n\t\treturn http.request;\n\t}\n\n\t#callFallbackRequest(\n\t\turl: URL,\n\t\toptions: NativeRequestOptions,\n\t\tcallback?: (response: AcceptableResponse) => void,\n\t): AcceptableResponse | ClientRequest | Promise<AcceptableResponse | ClientRequest> {\n\t\tconst fallbackRequest = this.#getFallbackRequestFunction();\n\n\t\tif (!fallbackRequest) {\n\t\t\tthrow new TypeError('The request function must return a value');\n\t\t}\n\n\t\tconst fallbackResult = fallbackRequest(url, options, callback);\n\n\t\tif (fallbackResult === undefined) {\n\t\t\tthrow new TypeError('The request function must return a value');\n\t\t}\n\n\t\tif (is.promise(fallbackResult)) {\n\t\t\treturn this.#resolveFallbackRequestResult(fallbackResult);\n\t\t}\n\n\t\treturn fallbackResult;\n\t}\n\n\tasync #resolveRequestWithFallback(\n\t\trequestResult: Promise<AcceptableResponse | ClientRequest | undefined>,\n\t\t{url, options, callback, requestStartedAt}: RequestFallbackContext,","sourceCodeStart":3614,"sourceCodeEnd":3650,"githubUrl":"https://github.com/sindresorhus/got/blob/e3924aa1e53a6ca3eb93a43618ce532442a89b40/source/core/options.ts#L3614-L3650","documentation":"Thrown from `#callFallbackRequest` when the fallback request function is itself falsy. Got's custom-`request` feature allows a user-supplied function to handle the transport; if that function returns `undefined`, Got falls back to the default transport (http/https/http2). This particular throw means the fallback function resolved to nothing — i.e. `#getFallbackRequestFunction()` returned a falsy value, which should not happen under normal config.","triggerScenarios":"A custom `request` option returns `undefined` AND the internally-resolved fallback (`http.request` / `https.request` / `http2Client.auto`) is somehow unavailable (e.g. monkey-patched http module returning falsy). The three throw sites at 3632/3638/3676 cover: missing fallback, sync-undefined fallback result, and async-undefined fallback result respectively.","commonSituations":"Writing a custom transport adapter (mock, proxy, test double) whose function signature returns void; mocking `http.request`/`https.request` in tests so the fallback is falsy; partially-implemented request wrappers that forget to `return`.","solutions":["Ensure the custom `request` function always returns a `ClientRequest`, a `Response`, or a Promise resolving to one — never `undefined`.","If you replaced Node's `http.request` / `https.request` in tests, make the mock return a valid request object.","Audit adapter code paths for early `return;` statements missing a value.","This error indicates an internal contract violation; if it surfaces without a custom `request`, file a Got bug with the full options."],"exampleFix":"// before\nconst customRequest = (url, options, callback) => {\n  doSomethingAsync(url); // forgot to return\n};\ngot.extend({request: customRequest});\n\n// after\nconst customRequest = (url, options, callback) => {\n  return http.request(url, options, callback);\n};\ngot.extend({request: customRequest});","handlingStrategy":"validation","validationCode":"function makeAdapter(transport) {\n  return (url, options, callback) => {\n    const result = transport(url, options, callback);\n    if (!result) throw new TypeError('adapter produced no request object');\n    return result;\n  };\n}\ngot.extend({request: makeAdapter(http.request)});","typeGuard":"const isRequestResult = (v: unknown): v is import('http').ClientRequest | Promise<import('http').ClientRequest> =>\n  v !== undefined && v !== null;","tryCatchPattern":null,"preventionTips":["Always `return` from custom request adapters — never let control fall off the end.","Add a unit test asserting the adapter returns a truthy value.","Don't stub Node's core `http.request`/`https.request` to return undefined in tests.","Lint with `consistent-return` enabled."],"tags":["custom-request","transport","http"],"analyzedSha":"e3924aa1e53a6ca3eb93a43618ce532442a89b40","analyzedAt":"2026-08-03T19:22:24.770Z","schemaVersion":2}