Skip to content

Commit 971f01d

Browse files
A-O-Emmanuelgithub-actions[bot]SleeplessByteCool-Katt
authored
add error-handling exercise (#2732)
* add error-handling exercise * [CI] Format code * add github username and update proof.ci.js and error-handling.js * Edit introduction and update proof.ci.js, error-handling.js, and error-handling.spec.js * Update exercises/practice/error-handling/error-handling.js Co-authored-by: Derk-Jan Karrenbeld <derk-jan+github@karrenbeld.info> * edit introduction.md proof.ci.js and error-handling.js * fix test passing by default * add handling for empty string * remove generic line * edit instructions.md, add more tests * Edit error-handling.spec.js to include test for specific errors and test for generic errors and test for error messages * Update exercises/practice/error-handling/.docs/introduction.md Co-authored-by: Cool-Katt <stani_s1@abv.bg> * Update exercises/practice/error-handling/.docs/introduction.md Co-authored-by: Cool-Katt <stani_s1@abv.bg> * Update exercises/practice/error-handling/error-handling.spec.js Co-authored-by: Cool-Katt <stani_s1@abv.bg> * edit test file and change difficulty level to 4 which is medium * [CI] Format code * Update introduction.md --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Derk-Jan Karrenbeld <derk-jan+github@karrenbeld.info> Co-authored-by: Cool-Katt <stani_s1@abv.bg>
1 parent c9005fa commit 971f01d

14 files changed

Lines changed: 300 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2770,6 +2770,14 @@
27702770
"practices": [],
27712771
"prerequisites": [],
27722772
"difficulty": 2
2773+
},
2774+
{
2775+
"slug": "error-handling",
2776+
"name": "Error Handling",
2777+
"uuid": "de1c75f2-2461-4347-b5ca-b3cfaafe4d79",
2778+
"practices": [],
2779+
"prerequisites": [],
2780+
"difficulty": 4
27732781
}
27742782
]
27752783
},
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Instructions
2+
3+
Implement various kinds of error handling and resource management.
4+
5+
An important point of programming is how to handle errors and close resources even if errors occur.
6+
7+
This exercise requires you to handle various errors.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Error Handling
2+
3+
In this exercise, you will implement a function called `processString` that processes a given input string with proper error handling.
4+
5+
You will learn how to:
6+
7+
- Check input types and throw errors for invalid inputs.
8+
- Throw errors for specific cases (e.g., empty strings).
9+
- Return the uppercase version of the string if it is valid.
10+
- Handle and catch errors using a try..catch block
11+
12+
13+
Implement the processString function using a `try…catch` block.
14+
15+
Inside the `try` block:
16+
- If the input is not a string, throw a `TypeError`.
17+
- If the input is an empty string, return `null`.
18+
- If input length is greater than 100, or less than 10, throw a `RangeError`
19+
- If input contains a mix of letters and numbers, throw a `SyntaxError`.
20+
- Otherwise, return the input in `uppercase`.
21+
22+
*Don't forget to attach appropriate error messages then you throw! An informative and well structured error message can save you hours of debugging.*
23+
24+
Inside the `catch` block:
25+
- log the error's message using `console.log`
26+
- `throw` the `error` so it can be tested for its type.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
/bin/configlet
3+
/bin/configlet.exe
4+
/package-lock.json
5+
/yarn.lock
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"A-O-Emmanuel"
4+
],
5+
"files": {
6+
"solution": [
7+
"error-handling.js"
8+
],
9+
"test": [
10+
"error-handling.spec.js"
11+
],
12+
"example": [
13+
".meta/proof.ci.js"
14+
]
15+
},
16+
"blurb": "Implement various kinds of error handling and resource management."
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const processString = (input) => {
2+
try {
3+
if (typeof input !== 'string') {
4+
throw new TypeError('Input must be a string');
5+
}
6+
if (input === '') {
7+
return null;
8+
}
9+
if (input.length > 100) {
10+
throw new RangeError('Input is too long');
11+
}
12+
if (input.length < 10) {
13+
throw new RangeError('Input is too short');
14+
}
15+
if (/[a-zA-Z]/.test(input) && /\d/.test(input)) {
16+
throw new SyntaxError(
17+
'Input cannot contain a mix of letters and numbers',
18+
);
19+
}
20+
21+
return input.toUpperCase();
22+
} catch (error) {
23+
console.log(error.message);
24+
throw error;
25+
}
26+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
presets: [['@exercism/babel-preset-javascript', { corejs: '3.40' }]],
3+
plugins: [],
4+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// This is only a SKELETON file for the 'Error handling' exercise. It's been provided as a
3+
// convenience to get you started writing code faster.
4+
//
5+
6+
export const processString = (input) => {
7+
throw new Error('Remove this line and implement the function');
8+
};

0 commit comments

Comments
 (0)