-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add a `error-cause` section to `error-try-catch` See. #1714 * chore: add a error-cause to `error-try-catch` conclusion section * chore: Use cause property in ajaxapp/promise example codes * chore: access to cause property in `Error cause` section's example code * refactor: Exec `npm run textlint:fix` * chore: align with other heading expressions * chore: change from active form to passive form See. #1732 (comment) Co-authored-by: azu <azu@users.noreply.github.com> * chore: add conjunction Co-authored-by: Suguru Inatomi <suguru.inatomi@gmail.com> * chore: 表記ゆれの統一(送出 -> 投げられる) Co-authored-by: azu <azu@users.noreply.github.com> * chore: cut out a example code * fix: remove unnecessary console.error * fix: Error Causeの説明をより丁寧にする * fix: remove logging cause property * fix: exec `npm run textlint:fix` * fix: broken test Co-authored-by: azu <azu@users.noreply.github.com> * chore: Remove empty lines Co-authored-by: azu <azu@users.noreply.github.com> * fix: add cause description * fix: use Number.parseInt instead of parseInt Co-authored-by: azu <azu@users.noreply.github.com> * chore: change function definition order safeParseInt and sumNumStrings --------- Co-authored-by: azu <azu@users.noreply.github.com> Co-authored-by: Suguru Inatomi <suguru.inatomi@gmail.com>
- Loading branch information
1 parent
80be4a7
commit 76dd4c1
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 数値の文字列を受け取り数値を返す関数 | ||
// 'text' など数値にはならない文字列を渡された場合は例外を投げられる | ||
function safeParseInt(numStr) { | ||
const num = Number.parseInt(numStr, 10); | ||
if (Number.isNaN(num)) { | ||
throw new Error(`${numStr} is not a numeric`); | ||
} | ||
return num; | ||
} | ||
|
||
// 数字の文字列を二つ受け取り、合計を返す関数 | ||
function sumNumStrings(a, b) { | ||
try { | ||
const aNumber = safeParseInt(a); | ||
const bNumber = safeParseInt(b); | ||
return aNumber + bNumber; | ||
} catch (e) { | ||
throw new Error("Failed to sum a and b", { cause: e }); | ||
} | ||
} | ||
|
||
try { | ||
// 数値にならない文字列 'string' を渡しているので例外が投げられる | ||
sumNumStrings("string", "2"); | ||
} catch (err) { | ||
console.error(`エラーが発生しました (${err})`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters