Coding task · JavaScript and TypeScript course · Module 2: Functions, Arrays and Objects

Error Basics - Custom Error Classes

A custom error class is class NameError extends Error with a constructor that calls super(message). Put try inside the loop so that one alarm does not stop the scan of the remaining enclosures.

Task

Create the classes DinosaurEscapeError(species, enclosure), FeedingError(dinoName, reason) and SensorError(sensorType, enclosure) that extend Error, pass a message to super(), set this.name to the class name and store the parameters in properties with the same names. Write a function securityScan(enclosures) that, for each enclosure { name, species, voltage, dinoEscaped }, throws in a try block a DinosaurEscapeError when dinoEscaped is true or a SensorError('voltage', name) when voltage < 5000. In catch recognise the error with instanceof and display 'RED ALERT: ' + message or 'YELLOW ALERT: ' + message, and rethrow an error of any other type. The function returns an object with the number of alerts { red, yellow }.