Skip to content

Commit 169fe0d

Browse files
committed
improvement in error handling
1 parent 310b832 commit 169fe0d

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

src/commands.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,8 @@ export async function format(format_string: string, contest_id: string, task_id?
181181
console.error(e.toString());
182182
}
183183
} catch {
184-
if (contest === null || tasks === null) {
185-
console.error("failed to get contest information.");
186-
return;
187-
}
184+
// TODO: もう少し良いエラーハンドリングができないものか
185+
console.error("failed to get contest information.");
188186
}
189187
}
190188
}

src/project.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,25 @@ export async function validateProjectJSON(data: ContestProject): Promise<[true,
140140
export async function init(contest_id: string, options: { force?: boolean, contestDirnameFormat?: string }): Promise<ContestProject> {
141141
const atcoder = new AtCoder();
142142
if (!await atcoder.checkSession()) await atcoder.login();
143+
const [contest, tasks] = await Promise.all([atcoder.contest(contest_id), atcoder.tasks(contest_id)]).catch(() => {
144+
throw new Error("failed to get contest information.");
145+
});
146+
const format = options.contestDirnameFormat !== undefined ? options.contestDirnameFormat : (await getConfig()).get("default-contest-dirname-format");
147+
const dirname = formatContestDirname(format, contest);
143148
try {
144-
const [contest, tasks] = await Promise.all([atcoder.contest(contest_id), atcoder.tasks(contest_id)]);
145-
const format = options.contestDirnameFormat !== undefined ? options.contestDirnameFormat : (await getConfig()).get("default-contest-dirname-format");
146-
const dirname = formatContestDirname(format, contest);
147-
try {
148-
await promisify(mkdir)(dirname);
149-
}
150-
catch {
151-
// forceオプションがtrueでない場合のみエラーで停止する
152-
if (options.force !== true) {
153-
throw new Error(`${dirname} file/directory already exists.`)
154-
}
155-
}
156-
process.chdir(dirname);
157-
const data = {contest, tasks};
158-
await saveProjectJSON(data, process.cwd());
159-
console.log(`${dirname}/${PROJECT_JSON_FILE_NAME} created.`);
160-
return data;
149+
await promisify(mkdir)(dirname);
161150
}
162151
catch {
163-
throw new Error("failed to get contest information.");
152+
// forceオプションがtrueでない場合のみエラーで停止する
153+
if (options.force !== true) {
154+
throw new Error(`${dirname} file/directory already exists.`)
155+
}
164156
}
157+
process.chdir(dirname);
158+
const data = {contest, tasks};
159+
await saveProjectJSON(data, process.cwd());
160+
console.log(`${dirname}/${PROJECT_JSON_FILE_NAME} created.`);
161+
return data;
165162
}
166163

167164
interface DetailedTask {

0 commit comments

Comments
 (0)