-
-
Notifications
You must be signed in to change notification settings - Fork 655
add error-handling exercise #2732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
A-O-Emmanuel
wants to merge
13
commits into
exercism:main
Choose a base branch
from
A-O-Emmanuel:implement-error-handling-exercise
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
010e4ec
add error-handling exercise
A-O-Emmanuel 69a0b95
[CI] Format code
github-actions[bot] 0c70883
add github username and update proof.ci.js and error-handling.js
A-O-Emmanuel 42df9e1
Merge branch 'implement-error-handling-exercise' of https://github.co…
A-O-Emmanuel 272f673
Edit introduction and update proof.ci.js, error-handling.js, and erro…
A-O-Emmanuel 8c070a5
Update exercises/practice/error-handling/error-handling.js
A-O-Emmanuel 1c87531
edit introduction.md proof.ci.js and error-handling.js
A-O-Emmanuel 92a7c22
resolve merge conflict: kept local code
A-O-Emmanuel 1e5d829
Merge branch 'exercism:main' into implement-error-handling-exercise
A-O-Emmanuel c0be150
fix test passing by default
A-O-Emmanuel 6978c46
Merge branch 'implement-error-handling-exercise' of https://github.co…
A-O-Emmanuel 6243369
add handling for empty string
A-O-Emmanuel aaab3b1
remove generic line
A-O-Emmanuel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # 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, throw a generic `Error`. | ||
|
|
||
| - Otherwise, return the input in `uppercase`. | ||
|
|
||
| Inside the `catch` block: | ||
| - console log the `error` message | ||
| - `throw` the `error` so it can be tested for its type. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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." | ||
| } |
A-O-Emmanuel marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export const processString = (input) => { | ||
| try { | ||
| if (typeof input !== 'string') { | ||
| throw new TypeError('Input must be a string'); | ||
| } | ||
| if (input === '') { | ||
| return null | ||
| } | ||
| return input.toUpperCase(); | ||
| } catch (error) { | ||
| console.log(error.message); | ||
| throw error; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| audit=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: [], | ||
| }; |
A-O-Emmanuel marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| }; | ||
A-O-Emmanuel marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { describe, expect, test, xtest } from '@jest/globals'; | ||
| import { processString } from './error-handling'; | ||
|
|
||
| describe('Error Handling', () => { | ||
| test('throws TypeError if input is not a string', () => { | ||
| expect(() => processString(42)).toThrow(TypeError); | ||
| }); | ||
|
|
||
| xtest('returns null if string is empty', () => { | ||
| expect(processString('')).toBeNull(); | ||
| }); | ||
|
|
||
| xtest('returns uppercase string if input is valid', () => { | ||
| expect(processString('hello')).toBe('HELLO'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| ], | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": "^22.15.29", | ||
| "@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.2.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" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about on empty string, instead of throwing, it returns
'INVALID'ornull/undefinedso we can add some handling too.we can also explicitly forbid the generic
Errortype to add more complexity