Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
010e4ec
add error-handling exercise
A-O-Emmanuel Jul 24, 2025
69a0b95
[CI] Format code
github-actions[bot] Jul 25, 2025
0c70883
add github username and update proof.ci.js and error-handling.js
A-O-Emmanuel Jul 29, 2025
42df9e1
Merge branch 'implement-error-handling-exercise' of https://github.co…
A-O-Emmanuel Jul 29, 2025
272f673
Edit introduction and update proof.ci.js, error-handling.js, and erro…
A-O-Emmanuel Aug 2, 2025
8c070a5
Update exercises/practice/error-handling/error-handling.js
A-O-Emmanuel Aug 26, 2025
1c87531
edit introduction.md proof.ci.js and error-handling.js
A-O-Emmanuel Sep 23, 2025
92a7c22
resolve merge conflict: kept local code
A-O-Emmanuel Sep 23, 2025
1e5d829
Merge branch 'exercism:main' into implement-error-handling-exercise
A-O-Emmanuel Sep 23, 2025
c0be150
fix test passing by default
A-O-Emmanuel Nov 17, 2025
6978c46
Merge branch 'implement-error-handling-exercise' of https://github.co…
A-O-Emmanuel Nov 18, 2025
6243369
add handling for empty string
A-O-Emmanuel Nov 18, 2025
aaab3b1
remove generic line
A-O-Emmanuel Nov 23, 2025
dca8391
edit instructions.md, add more tests
A-O-Emmanuel Dec 27, 2025
e673a32
Merge branch 'exercism:main' into implement-error-handling-exercise
A-O-Emmanuel May 16, 2026
a11995c
Edit error-handling.spec.js to include test for specific errors and t…
A-O-Emmanuel May 16, 2026
e6dfcc9
Merge branch 'implement-error-handling-exercise' of https://github.co…
A-O-Emmanuel May 16, 2026
4721f45
Update exercises/practice/error-handling/.docs/introduction.md
A-O-Emmanuel May 20, 2026
d821b56
Update exercises/practice/error-handling/.docs/introduction.md
A-O-Emmanuel May 20, 2026
27454cd
Update exercises/practice/error-handling/error-handling.spec.js
A-O-Emmanuel May 20, 2026
d16ab8a
edit test file and change difficulty level to 4 which is medium
A-O-Emmanuel May 20, 2026
692da99
[CI] Format code
github-actions[bot] May 21, 2026
eb2e717
Update introduction.md
Cool-Katt May 21, 2026
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2770,6 +2770,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "error-handling",
"name": "Error Handling",
"uuid": "de1c75f2-2461-4347-b5ca-b3cfaafe4d79",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/error-handling/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Instructions

Implement various kinds of error handling and resource management.

An important point of programming is how to handle errors and close resources even if errors occur.

This exercise requires you to handle various errors.
26 changes: 26 additions & 0 deletions exercises/practice/error-handling/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Error Handling

In this exercise, you will implement a function called `processString` that processes a given input string with proper error handling.

You will learn how to:

- Check input types and throw errors for invalid inputs.
- Throw errors for specific cases (e.g., empty strings).
- Return the uppercase version of the string if it is valid.
- Handle and catch errors using a try..catch block


Implement the processString function using a `try…catch` block.

Inside the `try` block:
- If the input is not a string, throw a `TypeError`.
- If the input is an empty string, return `null`.
- If input length is greater than 100, or less than 10, throw a `RangeError`
- If input contains a mix of letters and numbers, throw a `SyntaxError`.
- Otherwise, return the input in `uppercase`.

Comment thread
A-O-Emmanuel marked this conversation as resolved.
*Don't forget to attach appropriate error messages then you throw! An informative and well structured error message can save you hours of debugging.*

Inside the `catch` block:
- log the error's message using `console.log`
- `throw` the `error` so it can be tested for its type.
5 changes: 5 additions & 0 deletions exercises/practice/error-handling/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/bin/configlet
/bin/configlet.exe
/package-lock.json
/yarn.lock
17 changes: 17 additions & 0 deletions exercises/practice/error-handling/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"A-O-Emmanuel"
],
"files": {
"solution": [
"error-handling.js"
],
"test": [
"error-handling.spec.js"
],
"example": [
".meta/proof.ci.js"
]
},
"blurb": "Implement various kinds of error handling and resource management."
}
26 changes: 26 additions & 0 deletions exercises/practice/error-handling/.meta/proof.ci.js
Comment thread
A-O-Emmanuel marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const processString = (input) => {
try {
if (typeof input !== 'string') {
throw new TypeError('Input must be a string');
}
if (input === '') {
return null;
}
if (input.length > 100) {
throw new RangeError('Input is too long');
}
if (input.length < 10) {
throw new RangeError('Input is too short');
}
if (/[a-zA-Z]/.test(input) && /\d/.test(input)) {
throw new SyntaxError(
'Input cannot contain a mix of letters and numbers',
);
}

return input.toUpperCase();
} catch (error) {
console.log(error.message);
throw error;
}
};
1 change: 1 addition & 0 deletions exercises/practice/error-handling/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/error-handling/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions exercises/practice/error-handling/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: [['@exercism/babel-preset-javascript', { corejs: '3.40' }]],
plugins: [],
};
8 changes: 8 additions & 0 deletions exercises/practice/error-handling/error-handling.js
Comment thread
A-O-Emmanuel marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// This is only a SKELETON file for the 'Error handling' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const processString = (input) => {
throw new Error('Remove this line and implement the function');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the way this currently is, it'll pass one of the tests by default, without the student changing anything.

};
72 changes: 72 additions & 0 deletions exercises/practice/error-handling/error-handling.spec.js
Comment thread
A-O-Emmanuel marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, expect, test, xtest } from '@jest/globals';
import { processString } from './error-handling';

describe('Error Handling', () => {
test('never throws a generic Error for any invalid input', () => {
const invalidInputs = [
42, // TypeError
'short', // RangeError (too short)
'a'.repeat(101), // RangeError (too long)
'12345test6789text', // SyntaxError (mixed)
];

for (const input of invalidInputs) {
let error;

try {
processString(input);
} catch (err) {
error = err;
}

expect(error).toBeInstanceOf(Error);
expect(error.constructor).not.toBe(Error);
expect(error.message).toEqual(expect.stringMatching(/.+/));
}
});

xtest('throws TypeError if input is not a string', () => {
expect(() => processString(42)).toThrow(
expect.objectContaining({
name: 'TypeError',
message: expect.stringMatching(/.+/),
}),
);
});

xtest('throws error if input is too short', () => {
expect(() => processString('short')).toThrow(
expect.objectContaining({
name: 'RangeError',
message: expect.stringMatching(/.+/),
}),
);
});

xtest('throws error if input is too long', () => {
const longString = 'a'.repeat(101);
expect(() => processString(longString)).toThrow(
expect.objectContaining({
name: 'RangeError',
message: expect.stringMatching(/.+/),
}),
);
});

xtest('throws error if input contains a mix of letters and numbers', () => {
expect(() => processString('12345test6789text')).toThrow(
expect.objectContaining({
name: 'SyntaxError',
message: expect.stringMatching(/.+/),
}),
);
});

xtest('returns null if string is empty', () => {
expect(processString('')).toBeNull();
});

xtest('returns uppercase string if input is valid', () => {
expect(processString('hellotherefriend')).toBe('HELLOTHEREFRIEND');
});
});
45 changes: 45 additions & 0 deletions exercises/practice/error-handling/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @ts-check

import config from '@exercism/eslint-config-javascript';
import maintainersConfig from '@exercism/eslint-config-javascript/maintainers.mjs';

import globals from 'globals';

export default [
...config,
...maintainersConfig,
{
files: maintainersConfig[1].files,
rules: {
'jest/expect-expect': ['warn', { assertFunctionNames: ['expect*'] }],
},
},
{
files: ['scripts/**/*.mjs'],
languageOptions: {
globals: {
...globals.node,
},
},
},
// <<inject-rules-here>>
{
ignores: [
// # Protected or generated
'/.appends/**/*',
'/.github/**/*',
'/.vscode/**/*',

// # Binaries
'/bin/*',

// # Configuration
'/config',
'/babel.config.js',

// # Typings
'/exercises/**/global.d.ts',
'/exercises/**/env.d.ts',
],
},
];
22 changes: 22 additions & 0 deletions exercises/practice/error-handling/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
verbose: true,
projects: ['<rootDir>'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
'**/test/**/*.[jt]s?(x)',
'**/?(*.)+(spec|test).[jt]s?(x)',
],
testPathIgnorePatterns: [
'/(?:production_)?node_modules/',
'.d.ts$',
'<rootDir>/test/fixtures',
'<rootDir>/test/helpers',
'__mocks__',
],
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
moduleNameMapper: {
'^(\\.\\/.+)\\.js$': '$1',
},
};
38 changes: 38 additions & 0 deletions exercises/practice/error-handling/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@exercism/javascript-practice-error-handling",
"description": "Exercism practice exercise on error-handling",
"author": "Katrina Owen",
"contributors": [
"Derk-Jan Karrenbeld <derk-jan+git@karrenbeld.info> (https://derk-jan.com)",
"Tejas Bubane (https://tejasbubane.github.io/)"
],
"private": true,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/exercism/javascript",
"directory": "exercises/practice/error-handling"
},
"devDependencies": {
"@exercism/babel-preset-javascript": "^0.5.1",
"@exercism/eslint-config-javascript": "^0.8.1",
"@jest/globals": "^29.7.0",
"@types/node": "^24.3.0",
"@types/shelljs": "^0.8.17",
"babel-jest": "^29.7.0",
"core-js": "~3.42.0",
"diff": "^8.0.2",
"eslint": "^9.28.0",
"expect": "^29.7.0",
"globals": "^16.3.0",
"jest": "^29.7.0"
},
"dependencies": {},
"scripts": {
"lint": "corepack pnpm eslint .",
"test": "corepack pnpm jest",
"watch": "corepack pnpm jest --watch",
"format": "corepack pnpm prettier -w ."
},
"packageManager": "pnpm@9.15.2"
}