Skip to content

Commit 5bf638f

Browse files
Merge pull request #755 from ossf/handling_errors_no_yaml
Handling errors no yaml
2 parents 32a8a48 + 64e51c3 commit 5bf638f

File tree

2 files changed

+172
-154
lines changed

2 files changed

+172
-154
lines changed

docs/labs/handling-errors.html

Lines changed: 5 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -7,160 +7,11 @@
77
<link rel="stylesheet" href="checker.css">
88
<script src="js-yaml.min.js"></script>
99
<script src="checker.js"></script>
10+
<script src="handling-errors.js"></script>
1011
<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">
1112

1213
<!-- See create_labs.md for how to create your own lab! -->
1314

14-
<!-- Sample expected answer -->
15-
<script id="expected0" type="plain/text">
16-
throw new Error("Division by zero is not allowed");
17-
</script>
18-
<!--
19-
-->
20-
<script id="expected1" type="plain/text">
21-
return a / b;
22-
</script>
23-
24-
<script id="expected2" type="plain/text">
25-
try {
26-
const result = divide(10, 2);
27-
console.log("Result:", result);
28-
} catch (err) {
29-
console.error("Error:", err.message);
30-
}
31-
</script>
32-
33-
<!-- Full pattern of correct answer -->
34-
<script id="correct0" type="plain/text">
35-
\s* throw new Error \( ("(.*)"|'(.*)'|`(.*)`) \) ; \s*
36-
</script>
37-
<script id="correct1" type="plain/text">
38-
\s* return a \/ b ; \s*
39-
</script>
40-
<script id="correct2" type="plain/text">
41-
\s* try \{
42-
const result = divide \( 10 , 2 \) ;
43-
console \. log \( ("Result:" | 'Result:' | `Result:`) , result \) ;
44-
\} catch \( err \) {
45-
console.error \( ("Error:" | 'Error:' | `Error:`) , err \. message \) ;
46-
\} \s*
47-
</script>
48-
49-
<script id="info" type="application/yaml">
50-
---
51-
hints:
52-
- index: 0
53-
absent: "; $"
54-
text: >
55-
This code uses the convention of terminating each line with a
56-
semicolon; please follow the conventions of the code being modified.
57-
- index: 0
58-
present: "(Throw|THROW|New|NEW|error|ERROR)"
59-
text: JavaScript is case-sensitive. use throw new Error(...).
60-
examples:
61-
- - " Throw new Error(\"Division by zero is not allowed\");"
62-
- - " THROW new Error(\"Division by zero is not allowed\");"
63-
- - " throw New Error(\"Division by zero is not allowed\");"
64-
- - " throw NEW Error(\"Division by zero is not allowed\");"
65-
- - " throw new error(\"Division by zero is not allowed\");"
66-
- - " throw new ERROR(\"Division by zero is not allowed\");"
67-
- index: 0
68-
absent: "throw"
69-
text: Try using the throw keyword to raise an exception, E.g., throw new Error("Message").
70-
examples:
71-
- - " return { success: false, message: \"Division by zero is not allowed\" };"
72-
- - " return \"Division by zero is not allowed\" ;"
73-
- index: 1
74-
absent: "return"
75-
text: Use the return keyword to return the result of the division.
76-
examples:
77-
- - " a / b ;"
78-
- index: 1
79-
present: "{ (.*?)} "
80-
text: Try simply returning the result of the division.
81-
examples:
82-
-
83-
- throw new Error("Division by zero is not allowed");
84-
- " return { success: true, result: a / b };"
85-
-
86-
- throw new Error("Division by zero is not allowed");
87-
- " return { result: a / b };"
88-
- index: 2
89-
absent: '\s*try\s*{\s* '
90-
text: >-
91-
Use a try block to catch any exceptions that might be thrown.
92-
It should look something like `try { ... } catch(err) {...}`
93-
(fill in the `...` sections).
94-
examples:
95-
-
96-
- throw new Error("Division by zero is not allowed");
97-
- return a / b;
98-
- " const result = divide(10, 2);"
99-
- index: 2
100-
present: '\s* try \s* { .*? if \( result.success \) .*?'
101-
text: You may assume that the result is successful within the try block.
102-
examples:
103-
-
104-
- throw new Error("Division by zero is not allowed");
105-
- return a / b;
106-
- " try { const result = divide(10 ,2); if( result.success) { console.log ( \"Result:\", result ); "
107-
- index: 2
108-
present: '.*? result.result .*?'
109-
text: The result is not an object, it is a number.
110-
examples:
111-
-
112-
- throw new Error("Division by zero is not allowed");
113-
- return a / b;
114-
- " try { const result = divide(10 ,2); console.log ( \"Result:\", result.result ); "
115-
- index: 2
116-
absent: '.*? catch .*? '
117-
text: >-
118-
Handle the error within the catch block. You need `catch(err) {...}`
119-
after `try {...}` to catch an error in the try block.
120-
examples:
121-
-
122-
- throw new Error("Division by zero is not allowed");
123-
- return a / b;
124-
- " try { const result = divide(10 ,2); console.log ( \"Result:\", result ); }"
125-
- index: 2
126-
absent: '\s* catch \s* \( .*? \) { \s* '
127-
text: Use 'catch (...) {...}' to catch an error object within the catch block.
128-
examples:
129-
-
130-
- throw new Error("Division by zero is not allowed");
131-
- return a / b;
132-
- " try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch {}"
133-
- index: 2
134-
absent: |-
135-
catch \( err \)
136-
text: >-
137-
Please use `catch(err) {...}` for purposes of this lab.
138-
examples:
139-
-
140-
- throw new Error("Division by zero is not allowed");
141-
- return a / b;
142-
- " try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (foo) {"
143-
- index: 2
144-
present: |-
145-
catch .* console \. error \( ["'][^"']*["'] , result
146-
text: >-
147-
When reporting the error, you need to report it in the catch block,
148-
which catches it as the variable `err`.
149-
Thus, you need to use `err.message` not `result` or `result.message`,
150-
since the error is in `err.message`. Note that
151-
the variable `result` is out of scope in the catch block anyway;
152-
it was declared in the try block.
153-
examples:
154-
-
155-
- throw new Error("Division by zero is not allowed");
156-
- return a / b;
157-
- " try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (err) { console.error('Error', result.message);"
158-
-
159-
- throw new Error("Division by zero is not allowed");
160-
- return a / b;
161-
- " try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (err) { console.error('Error', result );"
162-
# debug: true
163-
</script>
16415
</head>
16516
<body>
16617
<!-- For GitHub Pages formatting: -->
@@ -199,7 +50,7 @@ <h2>Task Information</h2>
19950
<ol>
20051
<li>Locate the function in your code that uses return codes to indicate success or failure. In our case, the function is <tt>divide</tt>.</li>
20152
<li>Modify the function to throw an error when an invalid operation is detected. In our case, we throw an error when the parameter <tt>b</tt> is zero.</li>
202-
<li>Set the error message to "Division by zero is not allowed".</li>
53+
<li>Set the error message to "Division by zero is forbidden".</li>
20354
<li>Update the success path to return the result of the division operation.</li>
20455
<li>Modify the calling code to use a try block to wrap the call to the <tt>divide</tt> function.</li>
20556
<li>Within the try block, log the result of the division operation if no error is thrown.</li>
@@ -219,10 +70,10 @@ <h2>Interactive Lab (<span id="grade"></span>)</h2>
21970
<pre><code>// Implement a simple division method that returns the result of a / b
22071
function divide(a, b) {
22172
if (b === 0) {
222-
<input id="attempt0" type="text" size="65" spellcheck="false"
223-
value='return { success: false, message: "Division by zero is not allowed" };'>
73+
<input id="attempt0" type="text" size="70" spellcheck="false"
74+
value='return { success: false, message: "Division by zero is forbidden" };'>
22475
} else {
225-
<input id="attempt1" type="text" size="65" spellcheck="false"
76+
<input id="attempt1" type="text" size="70" spellcheck="false"
22677
value='return { success: true, result: a / b };'>
22778
}
22879
}

docs/labs/handling-errors.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
info =
2+
{
3+
hints: [
4+
{
5+
index: 0,
6+
absent: "; $",
7+
text: "This code uses the convention of terminating each line with a semicolon; please follow the conventions of the code being modified.\n"
8+
},
9+
{
10+
index: 0,
11+
present: "(Throw|THROW|New|NEW|error|ERROR)",
12+
text: "JavaScript is case-sensitive. use throw new Error(...).",
13+
examples: [
14+
[ " Throw new Error(\"Division by zero is forbidden\");" ],
15+
[ " THROW new Error(\"Division by zero is forbidden\");" ],
16+
[ " throw New Error(\"Division by zero is forbidden\");" ],
17+
[ " throw NEW Error(\"Division by zero is forbidden\");" ],
18+
[ " throw new error(\"Division by zero is forbidden\");" ],
19+
[ " throw new ERROR(\"Division by zero is forbidden\");" ]
20+
]
21+
},
22+
{
23+
index: 0,
24+
absent: "throw",
25+
text: "Try using the throw keyword to raise an exception, E.g., throw new Error(\"Message\").",
26+
examples: [
27+
[ " return { success: false, message: \"Division by zero is forbidden\" };" ],
28+
[ " return \"Division by zero is forbidden\" ;" ]
29+
]
30+
},
31+
{
32+
index: 1,
33+
absent: "return",
34+
text: "Use the return keyword to return the result of the division.",
35+
examples: [
36+
[ " a / b ;" ]
37+
]
38+
},
39+
{
40+
index: 1,
41+
present: "{ (.*?)} ",
42+
text: "Try simply returning the result of the division.",
43+
examples: [
44+
[
45+
"throw new Error(\"Division by zero is forbidden\");",
46+
" return { success: true, result: a / b };"
47+
],
48+
[
49+
"throw new Error(\"Division by zero is forbidden\");",
50+
" return { result: a / b };"
51+
]
52+
]
53+
},
54+
{
55+
index: 2,
56+
absent: String.raw`\s*try\s*{\s* `,
57+
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).",
58+
examples: [
59+
[
60+
"throw new Error(\"Division by zero is forbidden\");",
61+
"return a / b;",
62+
" const result = divide(10, 2);"
63+
]
64+
]
65+
},
66+
{
67+
index: 2,
68+
present: String.raw`\s* try \s* { .*? if \( result.success \) .*?`,
69+
text: "You may assume that the result is successful within the try block.",
70+
examples: [
71+
[
72+
"throw new Error(\"Division by zero is forbidden\");",
73+
"return a / b;",
74+
" try { const result = divide(10 ,2); if( result.success) { console.log ( \"Result:\", result ); "
75+
]
76+
]
77+
},
78+
{
79+
index: 2,
80+
present: ".*? result.result .*?",
81+
text: "The result is not an object, it is a number.",
82+
examples: [
83+
[
84+
"throw new Error(\"Division by zero is forbidden\");",
85+
"return a / b;",
86+
" try { const result = divide(10 ,2); console.log ( \"Result:\", result.result ); "
87+
]
88+
]
89+
},
90+
{
91+
index: 2,
92+
absent: ".*? catch .*? ",
93+
text: "Handle the error within the catch block. You need `catch(err) {...}` after `try {...}` to catch an error in the try block.",
94+
examples: [
95+
[
96+
"throw new Error(\"Division by zero is forbidden\");",
97+
"return a / b;",
98+
" try { const result = divide(10 ,2); console.log ( \"Result:\", result ); }"
99+
]
100+
]
101+
},
102+
{
103+
index: 2,
104+
absent: String.raw`\s* catch \s* \( .*? \) { \s* `,
105+
text: "Use 'catch (...) {...}' to catch an error object within the catch block.",
106+
examples: [
107+
[
108+
"throw new Error(\"Division by zero is forbidden\");",
109+
"return a / b;",
110+
" try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch {}"
111+
]
112+
]
113+
},
114+
{
115+
index: 2,
116+
absent: String.raw`catch \( err \)`,
117+
text: "Please use `catch(err) {...}` for purposes of this lab.",
118+
examples: [
119+
[
120+
"throw new Error(\"Division by zero is forbidden\");",
121+
"return a / b;",
122+
" try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (foo) {"
123+
]
124+
]
125+
},
126+
{
127+
index: 2,
128+
present: String.raw`catch .* console \. error \( ["'][^"']*["'] , result`,
129+
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.",
130+
examples: [
131+
[
132+
"throw new Error(\"Division by zero is forbidden\");",
133+
"return a / b;",
134+
" try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (err) { console.error('Error', result.message);"
135+
],
136+
[
137+
"throw new Error(\"Division by zero is forbidden\");",
138+
"return a / b;",
139+
" try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (err) { console.error('Error', result );"
140+
]
141+
]
142+
}
143+
],
144+
expected: [
145+
'throw new Error("Division by zero is forbidden");',
146+
'return a / b;',
147+
`try {
148+
const result = divide(10, 2);
149+
console.log("Result:", result);
150+
} catch (err) {
151+
console.error("Error:", err.message);
152+
}`
153+
],
154+
correct: [
155+
String.raw`\s* throw new Error \( ("(.*)"|'(.*)'|${BACKQUOTE}(.*)${BACKQUOTE}) \) ; \s*`,
156+
String.raw`\s* return a \/ b ; \s*`,
157+
String.raw`\s* try \{
158+
const result = divide \( 10 , 2 \) ;
159+
console \. log \(
160+
("Result:" | 'Result:' | ${BACKQUOTE}Result:${BACKQUOTE}) , result \) ;
161+
\} catch \( err \) {
162+
console.error \(
163+
("Error:" | 'Error:' | ${BACKQUOTE}Error:${BACKQUOTE}) ,
164+
err \. message \) ;
165+
\} \s*`
166+
],
167+
}

0 commit comments

Comments
 (0)