Skip to content
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
68 changes: 67 additions & 1 deletion concepts/errors/about.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,69 @@
# About

TODO: add information on errors concept
Errors are useful to report when something is wrong or unexpected in a program or a piece of code.

They are javascript objects.

The main property of this object is `message`:

```javascript
const error = new Error('Oops, something went wrong');

console.log(error.message);
// => "Oops, something went wrong"
```

Using the `throw` syntax, you can throw an Error.

```javascript
throw new Error('Oops');
```

When an Error is thrown, the current execution is stopped and resumes in the first catch block of the call stack.

```javascript
try {
throw new Error('Oops');
} catch (error) {
console.log(error.message);
// => "Oops"
}
```

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.

```javascript
class CustomError extends Error {}

try {
// ... Code that may throw an error
} catch (error) {
if (error instanceof CustomError) {
console.log('The error thrown is an instance of the CustomError');
}
}
```

## Error Types

In addition to the `Error` object, other built-in error objects exist. You can learn more about it [here][error-types]

## Custom Errors

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

```javascript
class MyCustomError extends Error {}
```

## Throwing non errors

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.

## Error stacktraces

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.

[error-types]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#error_types
[custom-error-type]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#custom_error_types
[error-stack]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack
44 changes: 43 additions & 1 deletion concepts/errors/introduction.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
# Introduction

TODO: add introduction for errors concept
Errors are useful to report when something is wrong or unexpected in a program or a piece of code.

They are javascript objects.

The main property of this object is `message`:

```javascript
const error = new Error('Oops, something went wrong');

console.log(error.message);
// => "Oops, something went wrong"
```

Using the `throw` syntax, you can throw an Error.

```javascript
throw new Error('Oops');
```

When an Error is thrown, the current execution is stopped and resume in the first catch block of the call stack.

```javascript
try {
throw new Error('Oops');
} catch (error) {
console.log(error.message);
// => "Oops"
}
```

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.

```javascript
class CustomError extends Error {}

try {
// ... Code that may throw an error
} catch (error) {
if (error instanceof CustomError) {
console.log('The error thrown is an instance of the CustomError');
}
}
```
43 changes: 42 additions & 1 deletion concepts/errors/links.json
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
[]
[
{
"url": "https://developer.mozilla.org/en-US/docs/Glossary/Primitive",
"description": "mdn-primitive"
},
{
"url": "https://en.wikipedia.org/wiki/Immutable_object",
"description": "wiki-mutability"
},
{
"url": "https://developer.mozilla.org/en-US/docs/Glossary/Primitive",
"description": "mdn-primitive"
},
{
"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"
}
]
16 changes: 16 additions & 0 deletions exercises/concept/errors/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Hints

## 1. Monitor the humidity level of the room

- 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.

## 2. Detect overheating

- 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

## 3. Monitor the machine

- Make sure that you handle all the errors in the catch block, you should have 4 different cases.

[if-else-condition]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
[class-constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
97 changes: 97 additions & 0 deletions exercises/concept/errors/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Instructions

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.

## 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 assignement 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 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.

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.

## Tasks

## 1. Monitor the humidity level of the room

Implements a function `checkHumidityLevel` that takes the humidity percentage as a parameter.

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

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

```javascript
checkHumidityLevel(100);
// Throws an error
```

## 2. Detect overheating

Implements a function `reportOverheating` that takes the temperature as a parameter.

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 instanciated with a temperature argument. Make sure that the `OverheatingError` you throw has a temperature property attached to it.

```javascript
reportOverheating(null);
// Throws an ArgumentError
```

```javascript
reportOverheating(800);
// Throws an OverheatingError
```

## 3. Monitor the machine

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

`actions` is an object that has 4 properties :

- `check` is a _*function*_ that, when called, checks the temperature of the machine.
It may throw various errors

- `alertDeadSensor` is a _*function*_ that, when called, alerts a technician that the temperature's sensor is dead.

- `alertOverheating` is a _*function*_ that, when called, will turn on a warning light on the machine.

- `shutdown` is a _*function*_ that, when called, will turn off the machine.

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:

- `ArgumentError`: when this happens, call the `alertDeadSensor` function.
- `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.
- _anything else_: when this happens, rethrow the error

```javascript
monitorTheMachine({
check,
alertDeadSensor,
alertOverheating,
shutdown,
});
```
45 changes: 45 additions & 0 deletions exercises/concept/errors/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Introduction

Errors are useful to report when something is wrong or unexpected in a program or a piece of code.

They are javascript objects.

The main property of this object is `message`:

```javascript
const error = new Error('Oops, something went wrong');

console.log(error.message);
// => "Oops, something went wrong"
```

Using the `throw` syntax, you can throw an Error.

```javascript
throw new Error('Oops');
```

When an Error is thrown, the current execution is stopped and resume in the first catch block of the call stack.

```javascript
try {
throw new Error('Oops');
} catch (error) {
console.log(error.message);
// => "Oops"
}
```

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.

```javascript
class CustomError extends Error {}

try {
// ... Code that may throw an error
} catch (error) {
if (error instanceof CustomError) {
console.log('The error thrown is an instance of the CustomError');
}
}
```
29 changes: 29 additions & 0 deletions exercises/concept/errors/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"root": true,
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module"
},
"globals": {
"BigInt": true
},
"env": {
"es6": true,
"node": true,
"jest": true
},
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings"
],
"rules": {
"linebreak-style": "off",

"import/extensions": "off",
"import/no-default-export": "off",
"import/no-unresolved": "off",
"import/prefer-default-export": "off"
}
}
3 changes: 3 additions & 0 deletions exercises/concept/errors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
yarn-error.log

10 changes: 10 additions & 0 deletions exercises/concept/errors/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"contributors": ["SleeplessByte"],
"authors": ["TomPradat"],
"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."
}
29 changes: 29 additions & 0 deletions exercises/concept/errors/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Design

## Goal

The goal of this exercise is to teach the student how to handle errors / exception, throw them and create their own.

## Learning objectives

- try {} catch {}
- throw Error
- instanceOf SpecialError
- Creating custom errors
- Prefilling a message
- Constructing a message based on some constructor value
- Storing constructor values on the error (so that you have context)

## Out of scope

- Promise rejection
- Throwing non-errors

## Concepts

- errors

## Prerequisites

- strings
- classes-intro
Loading