Skip to content

Commit 4f0e2e8

Browse files
authored
Merge pull request #149 from increments/fix-error-message
Fix error message for forbidden error
2 parents bec1e51 + 92af2c1 commit 4f0e2e8

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/commands/publish.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { getFileSystemRepo } from "../lib/get-file-system-repo";
66
import { getQiitaApiInstance } from "../lib/get-qiita-api-instance";
77
import { syncArticlesFromQiita } from "../lib/sync-articles-from-qiita";
88
import { validateItem } from "../lib/validators/item-validator";
9-
import { Item } from "../qiita-api";
9+
import {
10+
Item,
11+
QiitaForbiddenError,
12+
QiitaForbiddenOrBadRequestError,
13+
} from "../qiita-api";
1014

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

120-
await Promise.all(promises);
124+
try {
125+
await Promise.all(promises);
126+
} catch (err) {
127+
if (err instanceof QiitaForbiddenError) {
128+
// patchItem and postItem is possible to return 403 by bad request.
129+
throw new QiitaForbiddenOrBadRequestError(err.message, { cause: err });
130+
}
131+
throw err;
132+
}
121133
console.log("Successful!");
122134
};

src/lib/error-handler.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
QiitaBadRequestError,
33
QiitaFetchError,
44
QiitaForbiddenError,
5+
QiitaForbiddenOrBadRequestError,
56
QiitaInternalServerError,
67
QiitaNotFoundError,
78
QiitaRateLimitError,
@@ -44,6 +45,14 @@ export const handleError = async (error: Error) => {
4445
);
4546
console.error(chalk.red(""));
4647
break;
48+
case QiitaForbiddenOrBadRequestError.name:
49+
console.error(chalk.red.bold("Qiita APIへのリクエストに失敗しました"));
50+
console.error(chalk.red(" 記事ファイルに不備がないかご確認ください"));
51+
console.error(
52+
chalk.red(" または、Qiitaのアクセストークンが正しいかご確認ください"),
53+
);
54+
console.error(chalk.red(""));
55+
break;
4756
case QiitaNotFoundError.name:
4857
console.error(chalk.red.bold("記事が見つかりませんでした"));
4958
console.error(

src/qiita-api/errors.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ export class QiitaUnknownError extends Error {
5353
this.name = "QiitaUnknownError";
5454
}
5555
}
56+
57+
export class QiitaForbiddenOrBadRequestError extends Error {
58+
constructor(message: string, options?: { cause?: Error }) {
59+
// @ts-expect-error This error is fixed in ES2022.
60+
super(message, options);
61+
this.name = "QiitaForbiddenOrBadRequestError";
62+
}
63+
}

0 commit comments

Comments
 (0)