1 00:00:00,590 --> 00:00:03,180 At present, to use async and await with Express, 2 00:00:03,180 --> 00:00:07,240 we need some way to capture any errors that might occur in our await function, 3 00:00:07,240 --> 00:00:10,500 like if something goes wrong with the server when we try to create a new quote. 4 00:00:10,500 --> 00:00:13,310 Let's take a look at what happens if an error occurs now. 5 00:00:13,310 --> 00:00:14,190 Above my code, 6 00:00:14,190 --> 00:00:18,609 I'll throw a fake error using JavaScript's built-in error constructor. 7 00:00:28,650 --> 00:00:33,043 If we go back to Postman and try to create a new quote again, 8 00:00:33,043 --> 00:00:36,374 you can see that our API isn't responding. 9 00:00:36,374 --> 00:00:39,340 It will just hang indefinitely and that's not what we want. 10 00:00:39,340 --> 00:00:43,616 To address this, we need to wrap our code in a JavaScript try-catch block. 11 00:00:55,212 --> 00:00:59,170 All the code we've written in this route so far goes into the try block. 12 00:01:05,130 --> 00:01:07,310 The catch block takes one parameter, 13 00:01:07,310 --> 00:01:10,450 which represents whatever error message it catches. 14 00:01:12,270 --> 00:01:13,564 Inside the catch block, 15 00:01:13,564 --> 00:01:16,774 we can send that message back to the client in the form of JSON. 16 00:01:19,541 --> 00:01:23,541 For example, let's mimic if 17 00:01:23,541 --> 00:01:28,661 something went wrong by throwing this 18 00:01:28,661 --> 00:01:33,468 fake error inside of the try block. 19 00:01:35,703 --> 00:01:40,154 So what should happen is an error will be thrown and this message will fall into 20 00:01:40,154 --> 00:01:43,652 the catch block and it will be sent back to the client as JSON. 21 00:01:43,652 --> 00:01:48,449 So let's save, make sure the server is running, and go back to Postman. 22 00:01:48,449 --> 00:01:51,284 We'll try to create the quote again, and 23 00:01:51,284 --> 00:01:55,140 now you see we're being sent an error message. 24 00:01:55,140 --> 00:01:59,680 Notice, however, that we're still getting back a status code of 200 OK, 25 00:01:59,680 --> 00:02:02,230 even though something obviously went wrong. 26 00:02:02,230 --> 00:02:05,870 When something goes wrong, we wanna make sure we're sending accurate HTTP 27 00:02:05,870 --> 00:02:10,200 status codes, as well as communicating clear errors to the client. 28 00:02:10,200 --> 00:02:11,810 We'll talk about that in the next video. 29 00:02:13,070 --> 00:02:15,670 I'll go ahead and erase the error we just made. 30 00:02:17,110 --> 00:02:20,780 And let's do this one more time by refactoring our route handler that gets 31 00:02:20,780 --> 00:02:23,390 a single quote to use try-catch. 32 00:02:23,390 --> 00:02:25,804 I'll start by creating a try-catch block. 33 00:02:31,971 --> 00:02:33,880 Remembering to pass in the error. 34 00:02:35,670 --> 00:02:39,070 We'll copy and paste our code into the try block. 35 00:02:41,550 --> 00:02:44,102 And now in the catch block, in the event of an error, 36 00:02:44,102 --> 00:02:46,783 I can send back some JSON containing an error message. 37 00:02:56,894 --> 00:02:59,847 For additional practice, I recommend going back and 38 00:02:59,847 --> 00:03:03,007 refactoring this first route to use try-catch as well.