Unverified Commit 908475fd authored by Hong Minhee's avatar Hong Minhee
Browse files

Fix error handling in lookupWebFinger() function

parent 533fbb6d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -79,6 +79,9 @@ To be released.
        applicable to `format: "compact"`.  Otherwise, it throws
        a `TypeError`.

 -  The `lookupWebFinger()` and `getActorHandle()` functions no more throw
    an error when they fail to reach the WebFinger resource.

 -  Now `fedify init` generates a default *tsconfig.json* file on Node.js and
    Bun, and fills the *deno.json* file with the default `compilerOptions` on
    Deno.
+12 −2
Original line number Diff line number Diff line
@@ -5,8 +5,6 @@ import type { ResourceDescriptor } from "./jrd.ts";
import { lookupWebFinger } from "./lookup.ts";

test("lookupWebFinger()", async (t) => {
  mf.install();

  await t.step("invalid resource", async () => {
    assertEquals(await lookupWebFinger("acct:johndoe"), null);
    assertEquals(await lookupWebFinger(new URL("acct:johndoe")), null);
@@ -14,6 +12,18 @@ test("lookupWebFinger()", async (t) => {
    assertEquals(await lookupWebFinger(new URL("acct:johndoe@")), null);
  });

  await t.step("connection refused", async () => {
    assertEquals(
      await lookupWebFinger("acct:johndoe@fedify-test.internal"),
      null,
    );
    assertEquals(
      await lookupWebFinger("https://fedify-test.internal/foo"),
      null,
    );
  });

  mf.install();
  mf.mock("GET@/.well-known/webfinger", (req) => {
    assertEquals(new URL(req.url).host, "example.com");
    return new Response("", { status: 404 });
+13 −4
Original line number Diff line number Diff line
@@ -31,10 +31,19 @@ export async function lookupWebFinger(
      "Fetching WebFinger resource descriptor from {url}...",
      { url: url.href },
    );
    const response = await fetch(url, {
    let response: Response;
    try {
      response = await fetch(url, {
        headers: { Accept: "application/jrd+json" },
        redirect: "manual",
      });
    } catch (error) {
      logger.debug(
        "Failed to fetch WebFinger resource descriptor: {error}",
        { url: url.href, error },
      );
      return null;
    }
    if (
      response.status >= 300 && response.status < 400 &&
      response.headers.has("Location")