Skip to content

Commit 7230db9

Browse files
authored
Merge pull request #4 from mayuri2307/TryCatchFinally
added finally block
2 parents 0ffea76 + ca21a54 commit 7230db9

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/components/TryCatchFinally.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019 American Express Travel Related Services Company, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
import { func } from 'prop-types';
16+
17+
const TryCatchFinally = ({ try: renderTry, catch: fallBack, finally: renderFinally }) => {
18+
try {
19+
return renderTry();
20+
} catch (e) {
21+
return fallBack(e);
22+
} finally {
23+
renderFinally();
24+
}
25+
};
26+
27+
TryCatchFinally.propTypes = {
28+
try: func,
29+
catch: func,
30+
finally: func,
31+
};
32+
33+
TryCatchFinally.defaultProps = {
34+
try: null,
35+
catch: (e) => {
36+
throw new Error(e);
37+
},
38+
finally: null,
39+
};
40+
41+
export default TryCatchFinally;

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
import ElseIf from './components/ElseIf';
1616
import TryCatch from './components/TryCatch';
17+
import TryCatchFinally from './components/TryCatchFinally';
1718
import Switch from './components/Switch';
1819
import Case from './components/Case';
1920
import Default from './components/Default';
2021

2122
export default {
2223
ElseIf,
2324
TryCatch,
25+
TryCatchFinally,
2426
Switch,
2527
Case,
2628
Default,

0 commit comments

Comments
 (0)