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.
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 is not important) if the percentage exceeds 70%.
checkHumidityLevel(60);
// => undefinedcheckHumidityLevel(100);
// Throws an errorElena is very pleased with your first assignment and asks 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 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 kinds 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 anArgumentError. - When everything works, if the temperature exceeds 500°C, you should throw an
OverheatingError. This error class will be instantiated with a temperature argument. Make sure that theOverheatingErroryou throw has a temperature property attached to it.
reportOverheating(null);
// Throws an ArgumentErrorreportOverheating(800);
// Throws an OverheatingErrorNow that your machine can 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 shut down 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.
Implement a function monitorTheMachine that takes an argument actions.
actions is an object that has 4 properties :
| property | description |
|---|---|
check |
a function. Checks the temperature of the machine. It may throw various errors. |
alertDeadSensor |
a function. Alerts a technician that the temperature's sensor is dead. |
alertOverheating |
a function. Will turn on a warning light on the machine. |
shutdown |
a function. Will turn off the machine. |
The monitorTheMachine(actions) function should internally call check().
If that passes, the function should not return anything.
If that throws an error, different behaviour is expected:
| exception | expected behaviour |
|---|---|
ArgumentError |
call the alertDeadSensor function. |
OverheatingError |
execute the overheating protocol. |
| (anything else) | rethrow the error |
Overheating protocol
If the temperature is less than 600 °C, turn on the warning light by calling the alertOverheating function.
If the temperature exceeds 600 °C, the situation is critical, so call the shutdown function.
monitorTheMachine({
check,
alertDeadSensor,
alertOverheating,
shutdown,
});