Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Concept Exercise: Errors #1277

Merged
merged 7 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion concepts/errors/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"blurb": "TODO: add blurb for errors concept",
"blurb": "Error handling in JavaScript is done via exceptions. Learn how to throw exceptions and then handle them using a try...catch statement.",
"authors": ["TomPradat"],
"contributors": ["SleeplessByte"]
}
40 changes: 8 additions & 32 deletions concepts/errors/links.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
[
{
"url": "https://developer.mozilla.org/en-US/docs/Glossary/Primitive",
"description": "mdn-primitive"
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#exception_handling_statements",
"description": "MDN: Exception Handling"
},
{
"url": "https://en.wikipedia.org/wiki/Immutable_object",
"description": "wiki-mutability"
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch",
"description": "MDN: try...catch"
},
{
"url": "https://developer.mozilla.org/en-US/docs/Glossary/Primitive",
"description": "mdn-primitive"
"url": "https://javascript.info/try-catch",
"description": "javascript.info: Error Handling"
},
{
"url": "https://developer.mozilla.org/en-US/docs/Glossary/Primitive",
"description": "mdn-primitive"
},
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze",
"description": "mdn-object-freeze"
},
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules",
"description": "mdn-module"
},
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#Renaming_imports_and_exports",
"description": "mdn-renaming-modules"
},
{
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports",
"description": "mdn-dynamic-imports"
},
{
"url": "https://bitsofco.de/what-is-tree-shaking/",
"description": "blog-tree-shaking"
},
{
"url": "https://2ality.com/2015/07/es6-module-exports.html#es6-modules-export-immutable-bindings",
"description": "blog-live-bindings"
"url": "https://javascript.info/custom-errors",
"description": "javascript.info: Custom Errors"
}
]
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@
"concepts": ["functions"],
"prerequisites": ["objects", "arrays", "null-undefined"],
"status": "beta"
},
{
"slug": "factory-sensors",
"name": "Factory Sensors",
"uuid": "2ccafa38-2802-44c1-8758-7415edefa909",
"concepts": ["errors"],
"prerequisites": ["classes", "null-undefined", "conditionals"],
"status": "beta"
}
],
"practice": [
Expand Down
61 changes: 27 additions & 34 deletions exercises/concept/errors/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,18 @@

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 opportunity for automation and asks you, a freelance developer, to develop a piece of software to monitor some of the machines.

## Check the humidity level of the production room

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.

## Detect overheating and broken sensors

Elena is very pleased with your first assignment and ask you to deal with the monitoring of the machines' temperature.

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.

The machine is equipped 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.

Your job is to implement a function that throws an error if the sensor is broken or if the machine starts overheating.

Knowing that you will later need to react differently depending on the error, you need a mechanism to differentiate the two kind of errors.

You could rely on the error messages, but this would make your code fragile as it would break if the message gets updated.

So to be able to do so properly, you'll throw instances of different error classes.

## Catching errors

Now that your machine is able to detect errors, you will implement a function that reacts to those errors in different ways :

- If the sensor is broken, you need to warn a technician
- 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.
- If another error happens, you'll rethrow it.

## 1. Monitor the humidity level of the room

Implements a function `checkHumidityLevel` that takes the humidity percentage as a parameter.
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.
The function should be called `checkHumidityLevel` and take the humidity percentage as a parameter.

You should throw an error (the message isn't important) if the percentage exceeds 70%.
You should throw an error (the message is not important) if the percentage exceeds 70%.

```javascript
checkHumidityLevel(60);
// Returns undefined
// => undefined
```

```javascript
Expand All @@ -48,11 +23,23 @@ checkHumidityLevel(100);

## 2. Detect overheating

Implements a function `reportOverheating` that takes the temperature as a parameter.
Elena is very pleased with your first assignment and ask you to deal with the monitoring of the machines' temperature.
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.

If the sensor is broken, the temperature will be null. In this case you should throw an `ArgumentError`.
The machine is equipped 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.

When everything works, if the temperature exceeds 500°C, you should throw a `OverheatingError`. This error class will be instantiated with a temperature argument. Make sure that the `OverheatingError` you throw has a temperature property attached to it.
Your job is to implement a function `reportOverheating` that takes the temperature as a parameter and throws an error if the sensor is broken or if the machine starts overheating.
Knowing that you will later need to react differently depending on the error, you need a mechanism to differentiate the two kind of errors.
You could rely on the error messages, but this would make your code fragile as it would break if the message gets updated.
So to be able to do so properly, you'll throw instances of different error classes.

- If the sensor is broken, the temperature will be `null`.
In this case you should throw an `ArgumentError`.
- When everything works, if the temperature exceeds 500°C, you should throw a `OverheatingError`.
This error class will be instantiated with a temperature argument.
Make sure that the `OverheatingError` you throw has a temperature property attached to it.

```javascript
reportOverheating(null);
Expand All @@ -66,6 +53,12 @@ reportOverheating(800);

## 3. Monitor the machine

Now that your machine is able to detect errors, you will implement a function that reacts to those errors in different ways :

- If the sensor is broken, you need to warn a technician
- 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.
- If another error happens, you'll rethrow it.

Implements a function `monitorTheMachine` that takes an argument `actions`.

`actions` is an object that has 4 properties :
Expand Down
8 changes: 4 additions & 4 deletions exercises/concept/errors/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"contributors": ["SleeplessByte"],
"blurb": "Learn how to handle errors by creating a piece of software for a newspaper factory.",
"authors": ["TomPradat"],
"contributors": ["SleeplessByte", "junedev"],
"files": {
"solution": ["errors.js"],
"test": ["errors.spec.js"],
"exemplar": [".meta/example.js"]
},
"blurb": "Learn how to handle errors by creating a piece of software for a newspaper factory."
"exemplar": [".meta/exemplar.js"]
}
}
9 changes: 5 additions & 4 deletions exercises/concept/errors/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ The goal of this exercise is to teach the student how to handle errors / excepti

## Learning objectives

- try {} catch {}
- throw Error
- try {} catch {}
- instanceOf SpecialError
- Creating custom errors
- Prefilling a message
Expand All @@ -21,9 +21,10 @@ The goal of this exercise is to teach the student how to handle errors / excepti

## Concepts

- errors
- `errors`

## Prerequisites

- strings
- classes-intro
- `classes` to understand how to create errors
- `conditionals` because they are needed for the exercise
- `null-undefined` because they are needed for the exercise
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ export class OverheatingError extends Error {
}
}

/**
* Check if the humidity level is not too high.
*
* @param {number} humidityPercentage
* @throws {Error}
*/
export function checkHumidityLevel(humidityPercentage) {
if (humidityPercentage > 70) {
throw new Error('Humidity level is too low');
}
}

/**
* Check if the temperature is not too high.
*
* @param {number|null} temperature
* @throws {ArgumentError|OverheatingError}
*/
export function reportOverheating(temperature) {
if (temperature === null) {
throw new ArgumentError();
Expand All @@ -21,6 +33,17 @@ export function reportOverheating(temperature) {
}
}

/**
* Triggers the needed action depending on the result of the machine check.
*
* @param {{
* check: function,
* alertDeadSensor: function,
* alertOverheating: function,
* shutdown: function
* }} actions
* @throws {ArgumentError|OverheatingError|Error}
*/
export function monitorTheMachine({
check,
alertDeadSensor,
Expand Down
16 changes: 7 additions & 9 deletions exercises/concept/errors/errors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// @ts-check
//
// The line above enables type checking for this file. Various IDEs interpret
// the @ts-check directive. It will give you helpful autocompletion when
// implementing this exercise.

export class ArgumentError extends Error {}

Expand All @@ -14,33 +10,35 @@ export class OverheatingError extends Error {
}

/**
* Check if the humidity level is high enough
*
* @param {number|null} humidityPercentage
* Check if the humidity level is not too high.
*
* @param {number} humidityPercentage
* @throws {Error}
*/
export function checkHumidityLevel(humidityPercentage) {
throw new Error('Implement the checkHumidity function');
}

/**
* Check if the temperature is not too high
* Check if the temperature is not too high.
*
* @param {number|null} temperature
*
* @throws {ArgumentError|OverheatingError}
*/
export function reportOverheating(temperature) {
throw new Error('Implement the reportOverheating function');
}

/**
* Triggers the needed action depending on the result of the machine check.
*
* @param {{
* check: function,
* alertDeadSensor: function,
* alertOverheating: function,
* shutdown: function
* }} actions
* @throws {ArgumentError|OverheatingError|Error}
*/
export function monitorTheMachine(actions) {
throw new Error('Implement the monitorTheMachine function');
Expand Down
Loading