Skip to content

Fix error message for forbidden error #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { getFileSystemRepo } from "../lib/get-file-system-repo";
import { getQiitaApiInstance } from "../lib/get-qiita-api-instance";
import { syncArticlesFromQiita } from "../lib/sync-articles-from-qiita";
import { validateItem } from "../lib/validators/item-validator";
import { Item } from "../qiita-api";
import {
Item,
QiitaForbiddenError,
QiitaForbiddenOrBadRequestError,
} from "../qiita-api";

export const publish = async (argv: string[]) => {
const args = arg(
Expand Down Expand Up @@ -117,6 +121,14 @@ export const publish = async (argv: string[]) => {
await fileSystemRepo.saveItem(responseItem, false, true);
});

await Promise.all(promises);
try {
await Promise.all(promises);
} catch (err) {
if (err instanceof QiitaForbiddenError) {
// patchItem and postItem is possible to return 403 by bad request.
throw new QiitaForbiddenOrBadRequestError(err.message, { cause: err });
}
throw err;
}
console.log("Successful!");
};
9 changes: 9 additions & 0 deletions src/lib/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
QiitaBadRequestError,
QiitaFetchError,
QiitaForbiddenError,
QiitaForbiddenOrBadRequestError,
QiitaInternalServerError,
QiitaNotFoundError,
QiitaRateLimitError,
Expand Down Expand Up @@ -44,6 +45,14 @@ export const handleError = async (error: Error) => {
);
console.error(chalk.red(""));
break;
case QiitaForbiddenOrBadRequestError.name:
console.error(chalk.red.bold("Qiita APIへのリクエストに失敗しました"));
console.error(chalk.red(" 記事ファイルに不備がないかご確認ください"));
console.error(
chalk.red(" または、Qiitaのアクセストークンが正しいかご確認ください"),
);
console.error(chalk.red(""));
break;
case QiitaNotFoundError.name:
console.error(chalk.red.bold("記事が見つかりませんでした"));
console.error(
Expand Down
8 changes: 8 additions & 0 deletions src/qiita-api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ export class QiitaUnknownError extends Error {
this.name = "QiitaUnknownError";
}
}

export class QiitaForbiddenOrBadRequestError extends Error {
constructor(message: string, options?: { cause?: Error }) {
// @ts-expect-error This error is fixed in ES2022.
super(message, options);
this.name = "QiitaForbiddenOrBadRequestError";
}
}