Skip to content

Commit

Permalink
PCC-1882: Added error handling and tested preview command
Browse files Browse the repository at this point in the history
  • Loading branch information
aumkar committed Jan 28, 2025
1 parent 4513979 commit aeef080
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
19 changes: 15 additions & 4 deletions packages/cli/src/cli/commands/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { exit } from "process";
import chalk from "chalk";
import AddOnApiHelper from "../../lib/addonApiHelper";
import { Logger, SpinnerLogger } from "../../lib/logger";
import { errorHandler } from "../exceptions";
import { errorHandler, IncorrectAccount } from "../exceptions";

type GeneratePreviewParam = {
documentId: string;
Expand Down Expand Up @@ -46,9 +46,20 @@ export const generatePreviewLink = errorHandler<GeneratePreviewParam>(
const generateLinkLogger = new SpinnerLogger("Generating preview link");
generateLinkLogger.start();

const previewLink = await AddOnApiHelper.previewFile(cleanedId, domain, {
baseUrl,
});
let previewLink: string;
try {
previewLink = await AddOnApiHelper.previewFile(cleanedId, domain, {
baseUrl,
});
} catch (e) {
if (e instanceof IncorrectAccount) {
generateLinkLogger.fail(
"Selected account doesn't belong to domain of the site.",
);
return;
}
throw e;
}

generateLinkLogger.succeed(
"Successfully generated preview link. Please copy it below:",
Expand Down
24 changes: 19 additions & 5 deletions packages/cli/src/cli/commands/import/drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import type { GaxiosResponse } from "gaxios";
import type { drive_v3 } from "googleapis";
import queryString from "query-string";
import AddOnApiHelper from "../../../lib/addonApiHelper";
import { PersistedTokens } from "../../../lib/auth";
import { Logger } from "../../../lib/logger";
import { errorHandler } from "../../exceptions";
import { errorHandler, IncorrectAccount } from "../../exceptions";
import { createFolder, getAuthedDrive, preprocessBaseURL } from "./utils";

type DrupalImportParams = {
Expand Down Expand Up @@ -88,10 +89,23 @@ export const importFromDrupal = errorHandler<DrupalImportParams>(
// Get site details
const site = await AddOnApiHelper.getSite(siteId);

const tokens = await AddOnApiHelper.getGoogleTokens({
scopes: ["https://www.googleapis.com/auth/drive.file"],
domain: site.domain,
});
let tokens: PersistedTokens;
try {
tokens = await AddOnApiHelper.getGoogleTokens({
scopes: ["https://www.googleapis.com/auth/drive.file"],
domain: site.domain,
});
} catch (e) {
if (e instanceof IncorrectAccount) {
logger.error(
chalk.red(
"ERROR: Selected account doesn't belong to domain of the site.",
),
);
return;
}
throw e;
}

const drive = getAuthedDrive(tokens);

Expand Down
25 changes: 20 additions & 5 deletions packages/cli/src/cli/commands/import/wordpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import type { GaxiosResponse } from "gaxios";
import { drive_v3 } from "googleapis";
import queryString from "query-string";
import AddOnApiHelper from "../../../lib/addonApiHelper";
import { PersistedTokens } from "../../../lib/auth";
import { Logger } from "../../../lib/logger";
import { errorHandler } from "../../exceptions";
import { errorHandler, IncorrectAccount } from "../../exceptions";
import { createFolder, getAuthedDrive, preprocessBaseURL } from "./utils";

const DEFAULT_PAGE_SIZE = 50;
Expand Down Expand Up @@ -131,10 +132,24 @@ export const importFromWordPress = errorHandler<WordPressImportParams>(
// Get site details
const site = await AddOnApiHelper.getSite(siteId);

const tokens = await AddOnApiHelper.getGoogleTokens({
scopes: ["https://www.googleapis.com/auth/drive.file"],
domain: site.domain,
});
let tokens: PersistedTokens;
try {
tokens = await AddOnApiHelper.getGoogleTokens({
scopes: ["https://www.googleapis.com/auth/drive.file"],
domain: site.domain,
});
} catch (e) {
if (e instanceof IncorrectAccount) {
logger.error(
chalk.red(
"ERROR: Selected account doesn't belong to domain of the site.",
),
);
return;
}
throw e;
}

const drive = getAuthedDrive(tokens);
const folder = await createFolder(
drive,
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,15 +738,15 @@ yargs(hideBin(process.argv))
demandOption: true,
type: "string",
})
.option("baseUrl", {
describe: "Base URL for the generated preview link.",
type: "string",
demandOption: false,
})
.option("domain", {
describe: "Domain of the document's site",
type: "string",
demandOption: true,
})
.option("baseUrl", {
describe: "Base URL for the generated preview link.",
type: "string",
demandOption: false,
});
},
async (args) =>
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export class SpinnerLogger {
succeed(message: string) {
if (this.logger) this.logger.succeed(message);
}
fail(message: string) {
if (this.logger) this.logger.fail(message);
}
}

export class Logger {
Expand Down

0 comments on commit aeef080

Please sign in to comment.