-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-exceptions.cfc
108 lines (98 loc) · 2.92 KB
/
03-exceptions.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* This exercise goes over:
* + Catching exceptions the traditional way
* + Catching exceptions in asynchronous code
* + Recovering from exceptions
* + Built in LogBox logging
* + Handling both results and errors in the same callback
*
* You can run this exercise from the root in CommandBox by running:
* `task run exercises/03-exceptions.cfc`
*
* You can use the `print` helper provided by CommandBox to log messages.
* (Use `print.line( "message" ).toConsole()` if you're not familiar with it.)
* Make sure to end with `.toConsole()` so your message is flushed
* to the console the moment it is printed.
*/
component extends="../BaseTask" {
/**
* Create a future that throws an exception.
* Catch this exception using a `try` / `catch` statement when calling `future.get()`.
*
* Question: What is the exception type in the `catch` statement?
*/
function partOne(){
return;
}
/**
* Create a future that throws an exception.
* Catch this exception.
* Log a friendly error message to the console.
*
* Question: What is the exception type in the asynchronous `catch`?
*/
function partTwo(){
return;
}
/**
* Create a future that may throw an exception using the `fakeAjax` helper.
* Catch this exception. Return the a default quote of your choosing from this step.
* Output the value of the pipeline to the console.
*/
function partThree(){
return;
}
/**
* Create a future that may throw an exception using the `fakeAjax` helper.
* Handle both the error case and the success case at the same time.
* In the error case, use a default quote of your choosing.
* In the success case, use the passed in quote.
* Output the quote to the console.
*/
function partFour(){
return;
}
/**
* Create a future that may throw an exception using the `fakeAjax` helper.
* Catch this exception. Return the a default quote of your choosing from this step.
* Process the quote value by passing it to `flakyAnalyticsRequest`.
* Catch this exception. Return the current quote value.
* Output the value of the pipeline to the console.
*/
function partFive(){
return;
}
function run(){
print.blueLine( "Running all `03-exceptions` exercises" ).toConsole();
print
.yellowLine( "Press Ctrl-C to exit" )
.line()
.toConsole();
partOne();
partTwo();
partThree();
partFour();
partFive();
while ( true ) {
if ( !isNull( checkInterrupted() ) ) {
return;
}
sleep( 100 );
}
}
private string function fakeAjax(){
sleep( randRange( 300, 1500 ) );
if ( randRange( 1, 2 ) == 1 ) {
var errors = [
"Server is down",
"Database is down",
"Request timeout",
"Out for lunch"
];
throw( errors[ randRange( 1, errors.len() ) ] );
}
var quotesPath = expandPath( "/commandbox" ) & "/system/Quotes.txt";
var quotes = fileRead( quotesPath ).listToArray( chr( 13 ) & chr( 10 ) );
return quotes[ randRange( 1, quotes.len() ) ];
}
}