Skip to content

Commit 546765e

Browse files
authored
Add new concept exercise : errors (#978)
* Add new concept exercise : errors * 👌 Update to match newest requirements * 👌 Run scripts/sync * 🚨
1 parent 2c634e6 commit 546765e

File tree

17 files changed

+658
-3
lines changed

17 files changed

+658
-3
lines changed

concepts/errors/about.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,69 @@
11
# About
22

3-
TODO: add information on errors concept
3+
Errors are useful to report when something is wrong or unexpected in a program or a piece of code.
4+
5+
They are javascript objects.
6+
7+
The main property of this object is `message`:
8+
9+
```javascript
10+
const error = new Error('Oops, something went wrong');
11+
12+
console.log(error.message);
13+
// => "Oops, something went wrong"
14+
```
15+
16+
Using the `throw` syntax, you can throw an Error.
17+
18+
```javascript
19+
throw new Error('Oops');
20+
```
21+
22+
When an Error is thrown, the current execution is stopped and resumes in the first catch block of the call stack.
23+
24+
```javascript
25+
try {
26+
throw new Error('Oops');
27+
} catch (error) {
28+
console.log(error.message);
29+
// => "Oops"
30+
}
31+
```
32+
33+
As any object in Javascript the Error can be "extended" to create Custom errors. You can use the `instanceof` syntax to check if the error caught is an instance of a particular object.
34+
35+
```javascript
36+
class CustomError extends Error {}
37+
38+
try {
39+
// ... Code that may throw an error
40+
} catch (error) {
41+
if (error instanceof CustomError) {
42+
console.log('The error thrown is an instance of the CustomError');
43+
}
44+
}
45+
```
46+
47+
## Error Types
48+
49+
In addition to the `Error` object, other built-in error objects exist. You can learn more about it [here][error-types]
50+
51+
## Custom Errors
52+
53+
You can also define your own [Custom error Type][custom-error-type] by creating a class that extends one of the built-ins Error Types
54+
55+
```javascript
56+
class MyCustomError extends Error {}
57+
```
58+
59+
## Throwing non errors
60+
61+
While the syntax `throw` is usually used to throw an Error object, Javascript is flexible and will let you throw a `string`, a `null` or any primitive type.
62+
63+
## Error stacktraces
64+
65+
While this is not standard in Javascript, most of the Javascript environments implement a [`stack`][error-stack] property on the Error objects, allowing you to get the stack trace of the error that was thrown.
66+
67+
[error-types]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#error_types
68+
[custom-error-type]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#custom_error_types
69+
[error-stack]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack

concepts/errors/introduction.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
11
# Introduction
22

3-
TODO: add introduction for errors concept
3+
Errors are useful to report when something is wrong or unexpected in a program or a piece of code.
4+
5+
They are javascript objects.
6+
7+
The main property of this object is `message`:
8+
9+
```javascript
10+
const error = new Error('Oops, something went wrong');
11+
12+
console.log(error.message);
13+
// => "Oops, something went wrong"
14+
```
15+
16+
Using the `throw` syntax, you can throw an Error.
17+
18+
```javascript
19+
throw new Error('Oops');
20+
```
21+
22+
When an Error is thrown, the current execution is stopped and resume in the first catch block of the call stack.
23+
24+
```javascript
25+
try {
26+
throw new Error('Oops');
27+
} catch (error) {
28+
console.log(error.message);
29+
// => "Oops"
30+
}
31+
```
32+
33+
As any object in Javascript the Error can be "extended" to create Custom errors. You can use the `instanceof` syntax to check if the error catched is an instance of a particular object.
34+
35+
```javascript
36+
class CustomError extends Error {}
37+
38+
try {
39+
// ... Code that may throw an error
40+
} catch (error) {
41+
if (error instanceof CustomError) {
42+
console.log('The error thrown is an instance of the CustomError');
43+
}
44+
}
45+
```

concepts/errors/links.json

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
1-
[]
1+
[
2+
{
3+
"url": "https://developer.mozilla.org/en-US/docs/Glossary/Primitive",
4+
"description": "mdn-primitive"
5+
},
6+
{
7+
"url": "https://en.wikipedia.org/wiki/Immutable_object",
8+
"description": "wiki-mutability"
9+
},
10+
{
11+
"url": "https://developer.mozilla.org/en-US/docs/Glossary/Primitive",
12+
"description": "mdn-primitive"
13+
},
14+
{
15+
"url": "https://developer.mozilla.org/en-US/docs/Glossary/Primitive",
16+
"description": "mdn-primitive"
17+
},
18+
{
19+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze",
20+
"description": "mdn-object-freeze"
21+
},
22+
{
23+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules",
24+
"description": "mdn-module"
25+
},
26+
{
27+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#Renaming_imports_and_exports",
28+
"description": "mdn-renaming-modules"
29+
},
30+
{
31+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports",
32+
"description": "mdn-dynamic-imports"
33+
},
34+
{
35+
"url": "https://bitsofco.de/what-is-tree-shaking/",
36+
"description": "blog-tree-shaking"
37+
},
38+
{
39+
"url": "https://2ality.com/2015/07/es6-module-exports.html#es6-modules-export-immutable-bindings",
40+
"description": "blog-live-bindings"
41+
}
42+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Hints
2+
3+
## 1. Monitor the humidity level of the room
4+
5+
- You might want to use an [if...else condition][if-else-condition] to check if the humidity percentage is higher than the maximum allowed value.
6+
7+
## 2. Detect overheating
8+
9+
- This function should be very similar to the `checkHumidityLevel`. You might want to check [how to construct a new object][class-constructor] from a class
10+
11+
## 3. Monitor the machine
12+
13+
- Make sure that you handle all the errors in the catch block, you should have 4 different cases.
14+
15+
[if-else-condition]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
16+
[class-constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Instructions
2+
3+
Elena is the new quality manager of a newspaper factory. As she has just arrived in the company, she has decided to review some of the processes in the factory to see what could be improved. She found out that technicians are doing a lot of quality checks by hand. She sees there is a good oportunity for automation and asks you, a freelance developer, to develop a piece of software to monitor some of the machines.
4+
5+
## Check the humidity level of the production room
6+
7+
Your first mission is to write a piece of software to monitor the humidity level of the production room. There is already a sensor connected to the software of the company that returns periodically the humidity percentage of the room. You need to implement a function in the software that will throw an error if the humidity percentage is too high.
8+
9+
## Detect overheating and broken sensors
10+
11+
Elena is very pleased with your first assignement and ask you to deal with the monitoring of the machines' temperature.
12+
13+
While chatting with a technician, Greg, you are told that if the temperature of a machine exceeds 500°C, the technicians start worrying about overheating.
14+
15+
The machine is equiped with a sensor that measures its internal temperature. You should know that the sensor is very sensitive and often breaks. In this case the technicians will need to change it.
16+
17+
Your job is to implement a function that throws an error if the sensor is broken or if the machine starts overheating.
18+
19+
Knowing that you will later need to react differently depending on the error, you need a mechanism to differentiate the two kind of errors.
20+
21+
You could rely on the error messages, but this would make your code fragile as it would break if the message gets updated.
22+
23+
So to be able to do so properly, you'll throw instances of different error classes.
24+
25+
## Catching errors
26+
27+
Now that your machine is able to detect errors, you will implement a function that reacts to those errors in different ways :
28+
29+
- If the sensor is broken, you need to warn a technician
30+
- If the temperature is too high, you will either shutdown the machine if the temperature exceeds 600°C or turn on a warning light if it is less than that.
31+
- If another error happens, you'll rethrow it.
32+
33+
## Tasks
34+
35+
## 1. Monitor the humidity level of the room
36+
37+
Implements a function `checkHumidityLevel` that takes the humidity percentage as a parameter.
38+
39+
You should throw an error (the message isn't important) if the percentage exceeds 70%.
40+
41+
```javascript
42+
checkHumidityLevel(60);
43+
// Returns undefined
44+
```
45+
46+
```javascript
47+
checkHumidityLevel(100);
48+
// Throws an error
49+
```
50+
51+
## 2. Detect overheating
52+
53+
Implements a function `reportOverheating` that takes the temperature as a parameter.
54+
55+
If the sensor is broken, the temperature will be null. In this case you should throw an `ArgumentError`.
56+
57+
When everything works, if the temperature exceeds 500°C, you should throw a `OverheatingError`. This error class will be instanciated with a temperature argument. Make sure that the `OverheatingError` you throw has a temperature property attached to it.
58+
59+
```javascript
60+
reportOverheating(null);
61+
// Throws an ArgumentError
62+
```
63+
64+
```javascript
65+
reportOverheating(800);
66+
// Throws an OverheatingError
67+
```
68+
69+
## 3. Monitor the machine
70+
71+
Implements a function `monitorTheMachine` that takes an argument `actions`.
72+
73+
`actions` is an object that has 4 properties :
74+
75+
- `check` is a _*function*_ that, when called, checks the temperature of the machine.
76+
It may throw various errors
77+
78+
- `alertDeadSensor` is a _*function*_ that, when called, alerts a technician that the temperature's sensor is dead.
79+
80+
- `alertOverheating` is a _*function*_ that, when called, will turn on a warning light on the machine.
81+
82+
- `shutdown` is a _*function*_ that, when called, will turn off the machine.
83+
84+
The `monitorTheMachine` function should call `check()`. If it passes, the function should not return anything. However it may `throw` an error. When this happens, you should, depending on the error:
85+
86+
- `ArgumentError`: when this happens, call the `alertDeadSensor` function.
87+
- `OverheatingError`: when this happens, if the temperature is less than 600 °C, call the `alertOverheating` function to turn on the warning light. If the temperature exceeds 600°C, the situation is critical, call the `shutdown` function.
88+
- _anything else_: when this happens, rethrow the error
89+
90+
```javascript
91+
monitorTheMachine({
92+
check,
93+
alertDeadSensor,
94+
alertOverheating,
95+
shutdown,
96+
});
97+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Introduction
2+
3+
Errors are useful to report when something is wrong or unexpected in a program or a piece of code.
4+
5+
They are javascript objects.
6+
7+
The main property of this object is `message`:
8+
9+
```javascript
10+
const error = new Error('Oops, something went wrong');
11+
12+
console.log(error.message);
13+
// => "Oops, something went wrong"
14+
```
15+
16+
Using the `throw` syntax, you can throw an Error.
17+
18+
```javascript
19+
throw new Error('Oops');
20+
```
21+
22+
When an Error is thrown, the current execution is stopped and resume in the first catch block of the call stack.
23+
24+
```javascript
25+
try {
26+
throw new Error('Oops');
27+
} catch (error) {
28+
console.log(error.message);
29+
// => "Oops"
30+
}
31+
```
32+
33+
As any object in Javascript the Error can be "extended" to create Custom errors. You can use the `instanceof` syntax to check if the error catched is an instance of a particular object.
34+
35+
```javascript
36+
class CustomError extends Error {}
37+
38+
try {
39+
// ... Code that may throw an error
40+
} catch (error) {
41+
if (error instanceof CustomError) {
42+
console.log('The error thrown is an instance of the CustomError');
43+
}
44+
}
45+
```

exercises/concept/errors/.eslintrc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"root": true,
3+
"parser": "babel-eslint",
4+
"parserOptions": {
5+
"ecmaVersion": 7,
6+
"sourceType": "module"
7+
},
8+
"globals": {
9+
"BigInt": true
10+
},
11+
"env": {
12+
"es6": true,
13+
"node": true,
14+
"jest": true
15+
},
16+
"extends": [
17+
"eslint:recommended",
18+
"plugin:import/errors",
19+
"plugin:import/warnings"
20+
],
21+
"rules": {
22+
"linebreak-style": "off",
23+
24+
"import/extensions": "off",
25+
"import/no-default-export": "off",
26+
"import/no-unresolved": "off",
27+
"import/prefer-default-export": "off"
28+
}
29+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
yarn-error.log
3+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"contributors": ["SleeplessByte"],
3+
"authors": ["TomPradat"],
4+
"files": {
5+
"solution": ["errors.js"],
6+
"test": ["errors.spec.js"],
7+
"exemplar": [".meta/example.js"]
8+
},
9+
"blurb": "Learn how to handle errors by creating a piece of software for a newspaper factory."
10+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Design
2+
3+
## Goal
4+
5+
The goal of this exercise is to teach the student how to handle errors / exception, throw them and create their own.
6+
7+
## Learning objectives
8+
9+
- try {} catch {}
10+
- throw Error
11+
- instanceOf SpecialError
12+
- Creating custom errors
13+
- Prefilling a message
14+
- Constructing a message based on some constructor value
15+
- Storing constructor values on the error (so that you have context)
16+
17+
## Out of scope
18+
19+
- Promise rejection
20+
- Throwing non-errors
21+
22+
## Concepts
23+
24+
- errors
25+
26+
## Prerequisites
27+
28+
- strings
29+
- classes-intro

0 commit comments

Comments
 (0)