Skip to content

Commit 5f45282

Browse files
Merge pull request #889 from shimos/ja-translate-labs-handling-errors
ja_handling-errors.html: Translate labs/handling-errors into Japanese
2 parents bf65b1e + 65d8edb commit 5f45282

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

docs/labs/handling-errors.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ info =
88
index: 0,
99
absent: "; $",
1010
text: "This code uses the convention of terminating each line with a semicolon; please follow the conventions of the code being modified.\n",
11+
text_ja: "このコードは各行の末尾にセミコロンをつけるコンベンションを使用しています。修正するコードのコンベンションに従ってください。",
1112
},
1213
{
1314
index: 0,
1415
present: "(Throw|THROW|New|NEW|error|ERROR)",
1516
text: "JavaScript is case-sensitive. use throw new Error(...).",
17+
text_ja: "JavaScriptは大文字小文字を区別します。throw new Error(...)としてください。",
1618
examples: [
1719
[ " Throw new Error(\"Division by zero is forbidden\");" ],
1820
[ " THROW new Error(\"Division by zero is forbidden\");" ],
@@ -26,6 +28,7 @@ info =
2628
index: 0,
2729
absent: "throw",
2830
text: "Try using the throw keyword to raise an exception, E.g., throw new Error(\"Message\").",
31+
text_ja: "throwキーワードを使って例外を投げてください。例: throw new Error(\"Message\")",
2932
examples: [
3033
[ " return { success: false, message: \"Division by zero is forbidden\" };" ],
3134
[ " return \"Division by zero is forbidden\" ;" ]
@@ -35,6 +38,7 @@ info =
3538
index: 1,
3639
absent: "return",
3740
text: "Use the return keyword to return the result of the division.",
41+
text_ja: "returnキーワードを使って、割り算の結果を返してください。",
3842
examples: [
3943
[ " a / b ;" ]
4044
],
@@ -43,6 +47,7 @@ info =
4347
index: 1,
4448
present: "{ (.*?)} ",
4549
text: "Try simply returning the result of the division.",
50+
text_ja: "シンプルに割り算の結果だけを返すようにしてください。",
4651
examples: [
4752
[
4853
"throw new Error(\"Division by zero is forbidden\");",
@@ -58,6 +63,7 @@ info =
5863
index: 2,
5964
absent: String.raw`\s*try\s*{\s* `,
6065
text: "Use a try block to catch any exceptions that might be thrown. It should look something like `try { ... } catch(err) {...}` (fill in the `...` sections).",
66+
text_ja: "tryブロックを使って投げられる可能性のある例外を受けられるようにしてください。try { ... } catch(err) { ... } のようになるはずです (...の部分を埋めてください)",
6167
examples: [
6268
[
6369
"throw new Error(\"Division by zero is forbidden\");",
@@ -70,6 +76,7 @@ info =
7076
index: 2,
7177
present: String.raw`\s* try \s* { .*? if \( result.success \) .*?`,
7278
text: "You may assume that the result is successful within the try block.",
79+
text_ja: "tryブロックの中では、結果は成功しているとみなしてかまいません。",
7380
examples: [
7481
[
7582
"throw new Error(\"Division by zero is forbidden\");",
@@ -82,6 +89,7 @@ info =
8289
index: 2,
8390
present: ".*? result.result .*?",
8491
text: "The result is not an object, it is a number.",
92+
text_ja: "結果はオブジェクトではなく数値です。",
8593
examples: [
8694
[
8795
"throw new Error(\"Division by zero is forbidden\");",
@@ -94,6 +102,7 @@ info =
94102
index: 2,
95103
absent: ".*? catch .*? ",
96104
text: "Handle the error within the catch block. You need `catch(err) {...}` after `try {...}` to catch an error in the try block.",
105+
text_ja: "catchブロックでエラーを処理してください。try {...} の後に、catch(err){...} でエラーをキャッチする必要があります。",
97106
examples: [
98107
[
99108
"throw new Error(\"Division by zero is forbidden\");",
@@ -106,6 +115,7 @@ info =
106115
index: 2,
107116
absent: String.raw`\s* catch \s* \( .*? \) { \s* `,
108117
text: "Use 'catch (...) {...}' to catch an error object within the catch block.",
118+
text_ja: "catch (...) {...}を用いて、エラーオブジェクトをキャッチするようにしてください。",
109119
examples: [
110120
[
111121
"throw new Error(\"Division by zero is forbidden\");",
@@ -118,6 +128,7 @@ info =
118128
index: 2,
119129
absent: String.raw`catch \( err \)`,
120130
text: "Please use `catch(err) {...}` for purposes of this lab.",
131+
text_ja: "このラボでは、catch (err) {...} を用いてください。",
121132
examples: [
122133
[
123134
"throw new Error(\"Division by zero is forbidden\");",
@@ -130,6 +141,7 @@ info =
130141
index: 2,
131142
present: String.raw`catch .* console \. error \( ["'][^"']*["'] , result`,
132143
text: "When reporting the error, you need to report it in the catch block, which catches it as the variable `err`. Thus, you need to use `err.message` not `result` or `result.message`, since the error is in `err.message`. Note that the variable `result` is out of scope in the catch block anyway; it was declared in the try block.",
144+
text_ja: "エラーをレポートするには、catchブロックの中でerr変数としてキャッチしたものを使います。したがって、エラーはerr.messageであるため、resultやresult.messageではなくerr.messageを使う必要があります。result変数はtryブロック内で宣言されており、どのみちcatchブロックのスコープ外であることに注意してください。",
133145
examples: [
134146
[
135147
"throw new Error(\"Division by zero is forbidden\");",

docs/labs/ja_handling-errors.html

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<!DOCTYPE html>
2+
<html lang="ja">
3+
<head>
4+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<link rel="stylesheet" href="https://best.openssf.org/assets/css/style.css">
8+
<link rel="stylesheet" href="checker.css">
9+
<script src="checker.js"></script>
10+
<script src="handling-errors.js"></script>
11+
<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">
12+
13+
<!-- See create_labs.md for how to create your own lab! -->
14+
15+
</head>
16+
<body>
17+
<!-- For GitHub Pages formatting: -->
18+
<div class="container-lg px-3 my-5 markdown-body">
19+
<h1>ラボ演習 handling-errors</h1>
20+
<p>
21+
これはセキュアなソフトウェア開発に関するラボ演習です。
22+
ラボの詳細については、<a href="introduction.html" target="_blank">概要</a>をご覧ください。
23+
24+
<p>
25+
<h2>タスク</h2>
26+
<p>
27+
<b>例外ハンドリングの仕組みを活用して、下記のコードの堅牢性を高めましょう。</b>
28+
29+
<p>
30+
<h2>背景</h2>
31+
<p>
32+
この演習では、リターンコードの仕組みから例外ハンドリングに移行することで、JavaScriptの関数の堅牢性をどのように高めるかを例示します。
33+
一番の目標はエラーをより効果的に対処する方法を理解することで、これによりコードの可読性やメンテナンス性を高めることができます。
34+
</p>
35+
最初に、このコードは、起こりうるエラーを対処するために、リターンコードの仕組みを用いています。
36+
<tt>divide</tt>関数は、処理が成功したかエラーが起きたかを表すオブジェクトを返します。
37+
呼び出し元では、successプロパティをチェックして、正常な結果かエラーにそれぞれ対処する必要があります。
38+
この方法でも動作はしますが、エラーをチェックするロジックが繰り返されてコードが汚くなりかねません。なお悪いことに、呼び出し元がエラーというリターンコードを無視してしまうと、予期しない失敗やセキュリティ脆弱性を引き起こすことすらありえます。
39+
40+
<p>
41+
<h2>タスクの詳細</h2>
42+
<p>
43+
44+
以下のコードを修正して、例外ハンドリングの仕組みを活用し堅牢性を高めてください。
45+
</p>
46+
不正な処理が検出されたとき(この場合は、0で割ったとき)例外を投げることによって、コードの正常フローとエラー処理のロジックを分離することができます。呼び出し元はtry..catchブロックを用いて、投げられる可能性のある例外を処理することができます。これによってコードがよりきれいになり、より堅牢となります。
47+
</p>
48+
このタスクを完了するには、以下を行います。
49+
</p>
50+
<ol>
51+
<li>コードの中で成功か失敗かを表すのにリターンコードを使っている関数を見つけます。今回の例では、<tt>divide</tt>関数です。</li>
52+
<li>その関数を、不正な処理を検出した場合にエラーを投げるように変更します。今回の例では、パラメータ<tt>b</tt>が0のときにエラーを投げるようにします。</li>
53+
<li>エラーメッセージを"Division by zero is forbidden"(「0による割り算は禁止されています」)にします。</li>
54+
<li>成功パスを、割り算の結果を返すように変更します。</li>
55+
<li>呼び出しているコードを、<tt>divide</tt>関数呼び出しの回りをtryブロックで囲むように変更します。</li>
56+
<li>tryブロックの中で、エラーが投げられなかった場合は割り算の結果を記録するようにします。</li>
57+
<li>catchブロックを追加し、<tt>divide</tt>関数から投げられたエラーを処理するようにします。</li>
58+
<li>成功パスでもエラーパスでも、コンソールやエラーコンソールに適切なメッセージを表示するようにします。</li>
59+
</ol>
60+
<p>
61+
必要に応じて、「ヒント」ボタンと「諦める」ボタンを使用してください。
62+
<p>
63+
<h2>演習 (<span id="grade"></span>)</h2>
64+
<p>
65+
下記のコードを変更して、<tt>divide</tt>関数とその呼び出し元がリターンコードの仕組みではなく例外ハンドリングを用いるように変更してください。
66+
<tt>divide</tt>関数は、0による割り算が行われようとした時にエラーを投げなければなりません。
67+
呼び出すコードは、try...catchブロックを用いてエラーを処理し、<tt>result</tt>という定数に結果を格納し、発生したエラーは<tt>error</tt>という変数に受け、適切なメッセージを表示しなければなりません。
68+
69+
<form id="lab">
70+
<pre><code>// a/bの結果を返すシンプルな割り算メソッドの実装
71+
function divide(a, b) {
72+
if (b === 0) {
73+
<input id="attempt0" type="text" size="70" spellcheck="false"
74+
value='return { success: false, message: "Division by zero is forbidden" };'>
75+
} else {
76+
<input id="attempt1" type="text" size="70" spellcheck="false"
77+
value='return { success: true, result: a / b };'>
78+
}
79+
}
80+
81+
// divide関数を呼び出し、結果かエラーを表示する
82+
<textarea id="attempt2" rows="8" cols="60" spellcheck="false">
83+
const result = divide(10, 2);
84+
if (result.success) {
85+
console.log("Result:", result.result);
86+
} else {
87+
console.error("Error:", result.message);
88+
}
89+
</textarea></code></pre>
90+
<button type="button" class="hintButton">ヒント</button>
91+
<button type="button" class="resetButton">リセット</button>
92+
<button type="button" class="giveUpButton">諦める</button>
93+
<br><br>
94+
<p>
95+
<i>このラボは<a href="https://www.microsoft.com/">Microsoft</a>のAvishay Balterによって開発されました。</i>
96+
<br><br>
97+
<p id="correctStamp" class="small">
98+
<textarea id="debugData" class="displayNone" rows="20" cols="65" readonly>
99+
</textarea>
100+
</form>
101+
</div><!-- End GitHub pages formatting -->
102+
</body>
103+
</html>

0 commit comments

Comments
 (0)