Skip to content

Handle a failed request

Octokit can return an HTTP error with a response, or reject before it receives one. Keep the original failure so your caller can inspect its status and headers. This example returns an empty result when GitHub answers 404. A private repository can also return 404 when the supplied credentials cannot see it.

import { Effect } from 'effect';
import { Octokit, RequestError } from 'octokit';
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
const lookup = Effect.tryPromise({
try: (signal) =>
octokit.rest.repos.get({
owner: 'octokit',
repo: 'octokit.js',
request: { signal },
}),
catch: (cause) => cause,
}).pipe(
Effect.map(({ data }) => data.full_name),
Effect.catch((cause) =>
cause instanceof RequestError && cause.response?.status === 404
? Effect.succeed(null)
: Effect.fail(cause),
),
);
console.log(await Effect.runPromise(lookup));

The umbrella client includes retry and throttling plugins. An additional Effect retry can repeat a whole SDK attempt sequence. For a write operation, decide whether it is safe to repeat after a lost response.

Make a request · Inspect the SDK behavior