Unverified Commit 00522be8 authored by Hong Minhee's avatar Hong Minhee
Browse files

Add suppressError option to deref accessors

parent 3a666e9c
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -10,6 +10,9 @@ To be released.

 -  Improved runtime type error messages for Activity Vocabulary API.  [[#79]]

 -  Added `suppressError` option to dereferencing accessors of Activity
    Vocabulary classes.

 -  Added `Federation.setInboxDispatcher()` method.  [[#71]]

 -  Frequently used JSON-LD contexts are now preloaded.  [[74]]
+1422 −153

File changed.

Preview size limit exceeded, changes collapsed.

+2 −1
Original line number Diff line number Diff line
@@ -91,11 +91,12 @@ export async function* generateClasses(
  yield "// deno-lint-ignore-file ban-unused-ignore\n";
  yield "// @ts-ignore TS7016\n";
  yield 'import jsonld from "jsonld";\n';
  yield 'import { getLogger } from "@logtape/logtape";\n';
  yield `import { LanguageTag, parseLanguageTag }
    from "@phensley/language-tag";\n`;
  yield `import { decode as decodeMultibase, encode as encodeMultibase }
    from "multibase";`;
  yield `import { type DocumentLoader, fetchDocumentLoader }
  yield `import { type DocumentLoader, fetchDocumentLoader, type RemoteDocument }
    from "${runtimePath}/docloader.ts";\n`;
  yield `import {
    exportSpki,
+20 −2
Original line number Diff line number Diff line
@@ -36,13 +36,27 @@ async function* generateProperty(
      options: {
        documentLoader?: DocumentLoader,
        contextLoader?: DocumentLoader,
        suppressError?: boolean,
      } = {}
    ): Promise<${getTypeNames(property.range, types)}> {
    ): Promise<${getTypeNames(property.range, types)} | null> {
      const documentLoader =
        options.documentLoader ?? this._documentLoader ?? fetchDocumentLoader;
      const contextLoader =
        options.contextLoader ?? this._contextLoader ?? fetchDocumentLoader;
      const { document } = await documentLoader(url.href);
      let fetchResult: RemoteDocument;
      try {
        fetchResult = await documentLoader(url.href);
      } catch (error) {
        if (options.suppressError) {
          getLogger(["fedify", "vocab"]).error(
            "Failed to fetch {url}: {error}",
            { error, url: url.href }
          );
          return null;
        }
        throw error;
      }
      const { document } = fetchResult;
    `;
    for (const range of property.range) {
      if (!(range in types)) continue;
@@ -83,6 +97,7 @@ async function* generateProperty(
        options: {
          documentLoader?: DocumentLoader,
          contextLoader?: DocumentLoader,
          suppressError?: boolean,
        } = {}
      ): Promise<${getTypeNames(property.range, types)} | null> {
        if (this.${await getFieldName(property.uri)}.length < 1) return null;
@@ -90,6 +105,7 @@ async function* generateProperty(
        if (v instanceof URL) {
          const fetched =
            await this.#fetch${toPascalCase(property.singularName)}(v, options);
          if (fetched == null) return null;
          this.${await getFieldName(property.uri)}[0] = fetched;
          return fetched;
        }
@@ -116,6 +132,7 @@ async function* generateProperty(
        options: {
          documentLoader?: DocumentLoader,
          contextLoader?: DocumentLoader,
          suppressError?: boolean,
        } = {}
      ): AsyncIterable<${getTypeNames(property.range, types)}> {
        const vs = this.${await getFieldName(property.uri)};
@@ -125,6 +142,7 @@ async function* generateProperty(
            const fetched =
              await this.#fetch${toPascalCase(property.singularName)}(
                v, options);
            if (fetched == null) continue;
            vs[i] = fetched;
            yield fetched;
            continue;
+18 −0
Original line number Diff line number Diff line
@@ -307,6 +307,11 @@ test("Activity.getObject()", async () => {
  assertInstanceOf(object, Object);
  assertEquals(object.id, new URL("https://example.com/object"));
  assertEquals(object.name, "Fetched object");

  const activity2 = new Activity({
    object: new URL("https://example.com/not-found"),
  });
  assertEquals(await activity2.getObject({ suppressError: true }), null);
});

test("Activity.getObjects()", async () => {
@@ -330,6 +335,19 @@ test("Activity.getObjects()", async () => {
  assertEquals(objects[0].name, "Fetched object");
  assertInstanceOf(objects[1], Object);
  assertEquals(objects[1].name, "Second object");

  const activity2 = new Activity({
    objects: [
      new URL("https://example.com/not-found"),
      new Object({
        name: "Second object",
      }),
    ],
  });
  const objects2 = await toArray(activity2.getObjects({ suppressError: true }));
  assertEquals(objects2.length, 1);
  assertInstanceOf(objects2[0], Object);
  assertEquals(objects2[0].name, "Second object");
});

test("Activity.clone()", async () => {