Skip to content

Commit 272f673

Browse files
committed
Edit introduction and update proof.ci.js, error-handling.js, and error-handling.spec.js
1 parent 42df9e1 commit 272f673

File tree

4 files changed

+22
-51
lines changed

4 files changed

+22
-51
lines changed

exercises/practice/error-handling/.docs/introduction.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ In this exercise, you will implement a function called `processString` that proc
44

55
You will learn how to:
66

7-
- Check input types and handle invalid inputs by throwing errors.
8-
- Throw custom errors for specific cases (e.g., empty strings).
9-
- Use a `try...finally` block to ensure certain code runs regardless of success or failure (such as cleanup or logging).
7+
- Check input types and throw errors for invalid inputs.
8+
- Throw errors for specific cases (e.g., empty strings).
109
- Return the uppercase version of the string if it is valid.
1110

1211
Your function should:
1312

14-
- Throw a `TypeError` if the input is not a string.
15-
- Throw an `Error` with the message `EmptyStringError` if the input string is empty.
16-
- Return the uppercase form of the input string if valid.
17-
- Use a `finally` block to log a cleanup message every time the function runs.
18-
19-
This exercise is a great way to practice writing robust functions that can gracefully handle unexpected inputs while always executing necessary cleanup logic.
13+
- Throw a `TypeError` if input is not a string.
14+
- Throw a general `Error` if input is an empty string.
15+
- Return the uppercase form of the string 'hello'.
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
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-
throw new Error('EmptyStringError');
8-
}
9-
return input.toUpperCase();
10-
} finally {
11-
console.log('Resource cleaned up');
2+
if (typeof input !== 'string') {
3+
throw new TypeError();
124
}
13-
}
14-
5+
if (input === '') {
6+
throw new Error();
7+
}
8+
return input.toUpperCase();
9+
};
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export const processString = (input) => {
2-
//TODO: implement this
3-
//should throw Error if input is not a string
4-
//should throw Error if input is an empty string
5-
//should return the uppercase version of the string if valid
6-
//should use finally to log cleanup message
7-
}
2+
//TODO: implement this
3+
//should throw TypeError if input is not a string
4+
//should throw a general Error if input is an empty string
5+
//should return the uppercase version of the string 'hello'
6+
};
Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
1-
import { describe, expect, it, jest } from '@jest/globals';
1+
import { describe, expect, test, xtest } from '@jest/globals';
22
import { processString } from './error-handling';
33

44
describe('Error Handling', () => {
5-
it('returns uppercase if valid string', () => {
6-
expect(processString('hello')).toBe('HELLO');
7-
});
8-
9-
it('throws TypeError if input is not a string', () => {
5+
test('throws TypeError if input is not a string', () => {
106
expect(() => processString(42)).toThrow(TypeError);
11-
expect(() => processString(42)).toThrow('input must be a string');
127
});
138

14-
it('throws Error with EmptyStringError if string is empty', () => {
9+
xtest('throws Error message if string is empty', () => {
1510
expect(() => processString('')).toThrow(Error);
16-
expect(() => processString('')).toThrow('EmptyStringError');
1711
});
1812

19-
it('always logs cleanup message', () => {
20-
console.log = jest.fn();
21-
22-
try {
23-
processString('');
24-
} catch {
25-
/*
26-
intentionally left empty,
27-
I expext this call to throw,
28-
but only care about verifying that the finally block is executed
29-
and clean up message logged.
30-
*/
31-
}
32-
33-
expect(console.log).toHaveBeenCalledWith('Resource cleaned up');
13+
xtest('returns uppercase string if input is valid', () => {
14+
expect(processString('hello')).toBe('HELLO');
3415
});
3516
});

0 commit comments

Comments
 (0)