Commit 9356087c authored by w8385's avatar w8385 Committed by 박근형
Browse files

Update cli/webfinger handling error with Cliffy

parent eca4b3f4
Loading
Loading
Loading
Loading
+8 −33
Original line number Diff line number Diff line
import { Command } from "@cliffy/command";
import { Command, ValidationError } from "@cliffy/command";
import { toAcctUrl } from "@fedify/fedify/vocab";
import { lookupWebFinger } from "@fedify/fedify/webfinger";
import ora from "ora";
@@ -18,11 +18,17 @@ export const command = new Command()
    "Allow private IP addresses in the URL.",
  )
  .option(
    "--max-redirection <maxRedirection:number>",
    "--max-redirection <maxRedirection:integer>",
    "Maximum number of redirections to follow.",
    { default: 5 },
  )
  .action(async (options, ...resources: string[]) => {
    if (options.maxRedirection < 0) { // Validate maxRedirection option
      throw new ValidationError(
        `Option "--max-redirection" must be a "non-negative integer", but got "${options.maxRedirection}".`,
      );
    }

    for (const resource of resources) {
      const spinner = ora({ // Create a spinner for the lookup process
        text: `Looking up WebFinger for ${resource}`,
@@ -30,7 +36,6 @@ export const command = new Command()
      }).start();
      try {
        const url = convertUrlIfHandle(resource); // Convert resource to URL
        validateMaxRedirection(options.maxRedirection);
        const webFinger = await lookupWebFinger(url, options) ?? // Look up WebFinger
          new NotFoundError(resource).throw(); // throw NotFoundError if not found

@@ -39,10 +44,6 @@ export const command = new Command()
      } catch (error) {
        if (error instanceof InvalidHandleError) { // If the handle format is invalid,
          spinner.fail(`Invalid handle format: ${error.handle}`); // log error message with handle
        } else if (error instanceof InvalidMaxRedirectionError) { // If max redirection is invalid,
          spinner.fail(
            `Invalid max redirection value: ${error.maxRedirection}`,
          ); // log error message with value
        } else if (error instanceof NotFoundError) { // If the resource is not found,
          spinner.fail(`Resource not found: ${error.resource}`); // log not found message
        } else if (error instanceof Error) {
@@ -69,17 +70,6 @@ function convertUrlIfHandle(handleOrUrl: string): URL {
  }
}

/**
 * Validates the max redirection value.
 * @param {number} maxRedirection The maximum number of redirections to validate.
 * @throws {Error} If the max redirection value is invalid.
 */
function validateMaxRedirection(maxRedirection: number): void {
  if (maxRedirection < 0 || !Number.isInteger(maxRedirection)) {
    new InvalidMaxRedirectionError(maxRedirection).throw();
  }
}

/**
 * Custom error class for invalid handle formats.
 * @param {string} handle The invalid handle that caused the error.
@@ -95,21 +85,6 @@ class InvalidHandleError extends Error {
  }
}

/**
 * Custom error class for invalid max redirection values.
 * @param {number} maxRedirection The invalid max redirection value.
 * @extends {Error}
 */
class InvalidMaxRedirectionError extends Error {
  constructor(public maxRedirection: number) {
    super(`Invalid maxRedirection value: ${maxRedirection}`);
    this.name = "InvalidMaxRedirectionError";
  }
  throw(): never {
    throw this;
  }
}

/**
 * Custom error class for not found resources.
 * @param {string} resource The resource that was not found.