1
00:00:00,440 --> 00:00:01,460
Not bad, not bad.

2
00:00:01,810 --> 00:00:07,530
I think we have good general understanding of the basic foundation in Mongo's.

3
00:00:08,210 --> 00:00:14,620
So now let's switch gears and talk about how we can handle it more gracefully, because at the moment,

4
00:00:14,930 --> 00:00:17,830
well, we're just leaving our user hanging.

5
00:00:17,840 --> 00:00:18,240
Correct.

6
00:00:18,740 --> 00:00:26,780
So if the user sends some kind of request with incorrect data, then, of course, we have the air here

7
00:00:27,050 --> 00:00:28,000
on the server.

8
00:00:28,340 --> 00:00:33,860
But as you can see, the user is just hanging and probably it's not the best approach.

9
00:00:34,070 --> 00:00:41,900
And again, the whole reason for that is because we have a synchronous operation, how we're not handling

10
00:00:42,190 --> 00:00:47,870
if there is an error and the fix is following what we want to go with try catch.

11
00:00:48,140 --> 00:00:55,250
And then we want to place our weight in the try block and also right away set up my response.

12
00:00:55,490 --> 00:01:03,230
So if we are successful, I still want to send back the response with our task document, the one that

13
00:01:03,230 --> 00:01:05,930
we're getting back from the model.

14
00:01:06,440 --> 00:01:10,260
And then the second thing, if there is an error, what do we do?

15
00:01:10,670 --> 00:01:13,280
Well, we could also send back the response.

16
00:01:13,520 --> 00:01:16,670
And in this case, the status is going to be five hundred.

17
00:01:16,940 --> 00:01:20,140
And effectively, that is just a general server error.

18
00:01:20,540 --> 00:01:27,590
And I said I'm purposely using just a general server error status Code V five hundred one, since we'll

19
00:01:27,590 --> 00:01:35,060
discuss errors in greater detail later in the project and then we can go with a Jason and I'll set up

20
00:01:35,060 --> 00:01:38,750
the message properly and I'll set it equal to my error.

21
00:01:39,140 --> 00:01:48,650
So now if we save and if I go back and if I send one more time, you'll notice that instead of hanging,

22
00:01:48,890 --> 00:01:50,680
we actually get a response.

23
00:01:50,930 --> 00:01:53,290
And in here I can see that the status is five hundred.

24
00:01:53,600 --> 00:01:57,290
So it's definitely our response at this point in time.

25
00:01:57,290 --> 00:01:59,470
You probably have two questions.

26
00:01:59,840 --> 00:02:08,840
First, do we really need to wrap all our logic in every controller and try and catch, since I can

27
00:02:08,840 --> 00:02:15,740
give you a hint of that in the remaining controllers will also use the functions and we'll stick a weight

28
00:02:15,740 --> 00:02:16,610
in front of them.

29
00:02:17,120 --> 00:02:21,260
And the short answer is yes, for timing, we'll have to do that.

30
00:02:21,380 --> 00:02:27,230
But that eventually, by the end of the project, I'll show you multiple ways how we can simplify that

31
00:02:27,590 --> 00:02:32,780
and how we can all of this boilerplate code for try and catch you.

32
00:02:32,780 --> 00:02:40,760
And your next question probably is, OK, we get the response, but this error seems a bit lengthy.

33
00:02:41,150 --> 00:02:42,200
Can we make it shorter?

34
00:02:42,470 --> 00:02:44,750
And my answer again is yes, we can.

35
00:02:45,260 --> 00:02:48,410
And the most basic approach is going to look something like this.

36
00:02:49,010 --> 00:02:55,460
Instead of setting message properly equal to an error parameter, just simply set it equal to generic.

37
00:02:55,680 --> 00:02:59,470
There was another string value as far as more complex set up.

38
00:02:59,720 --> 00:03:05,120
I'll discuss it at the end of the project since I want to keep moving along with the rest of the functionality.

39
00:03:05,540 --> 00:03:09,680
As far as my set up, I will keep sending this giant object in that way.

40
00:03:09,890 --> 00:03:12,650
I can show exact error messages we're getting.

41
00:03:12,890 --> 00:03:18,770
And as far as our API, I think we're actually in pretty good shape where we can create a task.

42
00:03:19,070 --> 00:03:25,700
We do have some basic validations and if there is any kind of her, well, we simply sent back five

43
00:03:25,700 --> 00:03:30,860
hundred that there is a server error and we send back error message.

44
00:03:30,860 --> 00:03:35,540
Yes, quite long one, but at least it's better than leaving the user hanging.

45
00:03:35,960 --> 00:03:43,640
And if we want to try out of the max length and simply go here with name and then I'll just add some

46
00:03:43,640 --> 00:03:46,490
gibberish just so it's more than 20 characters.

47
00:03:46,910 --> 00:03:48,440
So let me type it out here.

48
00:03:48,680 --> 00:03:55,730
And then once I say again, we get the error message where we have a name cannot be more than twenty

49
00:03:55,730 --> 00:03:58,250
characters that you did for this video.

50
00:03:58,490 --> 00:04:01,040
And now we're ready to move on to our next topic.

