1
00:00:01,701 --> 00:00:04,390
Now, as a final step here,

2
00:00:04,390 --> 00:00:08,980
in the getSinglePost action in the post-controller,

3
00:00:08,980 --> 00:00:12,560
I wanna add a little bit of error handling here

4
00:00:12,560 --> 00:00:14,700
when we create a post.

5
00:00:14,700 --> 00:00:19,700
As mentioned, in the Node + MongoDB section,

6
00:00:19,980 --> 00:00:22,190
way earlier in the course,

7
00:00:22,190 --> 00:00:25,060
there is a problem with Express.js

8
00:00:25,060 --> 00:00:30,040
related to errors caused by asynchronous operations.

9
00:00:30,040 --> 00:00:34,250
Those don't reach the default error-handling middleware

10
00:00:34,250 --> 00:00:36,060
out of the box,

11
00:00:36,060 --> 00:00:37,010
which is a pity.

12
00:00:37,010 --> 00:00:39,000
It would be better if it would work like this,

13
00:00:39,000 --> 00:00:40,320
but it doesn't.

14
00:00:40,320 --> 00:00:43,500
Therefore, theoretically, whenever you are working

15
00:00:43,500 --> 00:00:47,310
with asynchronous code and with asynchronous operations,

16
00:00:47,310 --> 00:00:49,920
you should add your own error handling

17
00:00:49,920 --> 00:00:54,050
and force Express to trigger its error-handling middleware

18
00:00:54,050 --> 00:00:56,330
if things go wrong.

19
00:00:56,330 --> 00:00:57,640
Now, here I'll only do it

20
00:00:57,640 --> 00:01:00,050
for this getSinglePost case, though,

21
00:01:00,050 --> 00:01:02,290
because there I can easily show you

22
00:01:02,290 --> 00:01:06,010
where our application would break otherwise.

23
00:01:06,010 --> 00:01:10,110
If I do log in real quick here with a valid user

24
00:01:10,110 --> 00:01:11,830
and I view a post,

25
00:01:11,830 --> 00:01:13,700
that, of course, works.

26
00:01:13,700 --> 00:01:15,570
But if I mess with the post id

27
00:01:15,570 --> 00:01:17,510
and I add a couple of characters,

28
00:01:17,510 --> 00:01:20,800
then this fails and the server crashes.

29
00:01:20,800 --> 00:01:21,633
And the reason for this

30
00:01:21,633 --> 00:01:24,220
is that we don't even make it to this check

31
00:01:24,220 --> 00:01:27,190
where we would return a 404 page

32
00:01:27,190 --> 00:01:30,560
because when we create the post here already,

33
00:01:30,560 --> 00:01:35,500
we actually try to convert the id to this ObjectId thing.

34
00:01:35,500 --> 00:01:38,830
And it turns out that this ObjectId constructor

35
00:01:38,830 --> 00:01:41,670
throws an error if it receives an id

36
00:01:41,670 --> 00:01:44,980
that does not follow the MongoDB id format

37
00:01:44,980 --> 00:01:47,393
which my self-entered id doesn't.

38
00:01:48,390 --> 00:01:51,340
Now, normally, when code throws an error,

39
00:01:51,340 --> 00:01:55,740
the default Express error-handler would become active.

40
00:01:55,740 --> 00:01:59,247
But since in the post-controller,

41
00:01:59,247 --> 00:02:02,370
getSinglePost is an async function,

42
00:02:02,370 --> 00:02:05,260
this actually is all wrapped into a promise,

43
00:02:05,260 --> 00:02:08,389
and this overall is an asynchronous operation,

44
00:02:08,389 --> 00:02:11,090
and there, unfortunately, errors

45
00:02:11,090 --> 00:02:14,560
generated by or inside of such async operations

46
00:02:14,560 --> 00:02:18,193
are not handled by the Express error-handling middleware.

47
00:02:19,290 --> 00:02:22,840
To still ensure we reach the error-handling middleware,

48
00:02:22,840 --> 00:02:26,580
we instead have to manually handle the error first

49
00:02:26,580 --> 00:02:29,983
and then call next and forward the error.

50
00:02:30,910 --> 00:02:33,380
So, therefore, we can use try-catch here

51
00:02:33,380 --> 00:02:36,630
to try creating a post.

52
00:02:36,630 --> 00:02:39,470
And for that, I'll create a variable named post

53
00:02:39,470 --> 00:02:42,990
to which I'll assign a value inside of this try block.

54
00:02:42,990 --> 00:02:46,180
And then I catch any errors that might be thrown

55
00:02:46,180 --> 00:02:49,140
by the operation that lives in the try block,

56
00:02:49,140 --> 00:02:51,200
in this case the creation of the post

57
00:02:51,200 --> 00:02:54,403
where this conversion of the id will actually happen.

58
00:02:56,110 --> 00:03:00,410
If that generates an error, then we can catch it here.

59
00:03:00,410 --> 00:03:02,780
And then what we can do here

60
00:03:02,780 --> 00:03:05,760
is we can use this third parameter

61
00:03:05,760 --> 00:03:08,530
which we get in all middleware functions,

62
00:03:08,530 --> 00:03:12,350
and route handlers are just middleware functions at the end,

63
00:03:12,350 --> 00:03:14,563
which is this next function.

64
00:03:15,480 --> 00:03:18,380
We can call next and pass the error to it,

65
00:03:18,380 --> 00:03:20,470
and this will then reach

66
00:03:20,470 --> 00:03:23,460
the Express default error-handling middleware,

67
00:03:23,460 --> 00:03:25,293
but we have to do it manually.

68
00:03:26,590 --> 00:03:28,770
I also will return this

69
00:03:28,770 --> 00:03:31,240
or return thereafter, doesn't make a difference,

70
00:03:31,240 --> 00:03:35,000
because I wanna ensure that this other code doesn't execute

71
00:03:35,000 --> 00:03:36,190
because it wouldn't work

72
00:03:36,190 --> 00:03:38,673
if we already failed to create an id.

73
00:03:40,030 --> 00:03:42,080
With that, if I now save that,

74
00:03:42,080 --> 00:03:46,623
if I now reload this invalid id, I get An error occurred!

75
00:03:47,550 --> 00:03:50,430
If I instead wanna get my 404 page,

76
00:03:50,430 --> 00:03:55,430
then I can, instead of calling next, simply manually render

77
00:03:56,500 --> 00:03:58,960
this 404 page like this.

78
00:03:58,960 --> 00:04:00,200
So that would be an option.

79
00:04:00,200 --> 00:04:04,550
It's up to you what you wanna do if this error occurs.

80
00:04:04,550 --> 00:04:09,550
So if I do that and reload, I still get An error occurred!,

81
00:04:09,770 --> 00:04:13,290
because actually here I don't have a 404 page anymore.

82
00:04:13,290 --> 00:04:17,029
If I quickly create a 404.ejs file, though,

83
00:04:17,029 --> 00:04:20,873
and I say Not found! in here,

84
00:04:23,000 --> 00:04:25,510
Could not find the resource,

85
00:04:25,510 --> 00:04:26,853
then you will see,

86
00:04:28,270 --> 00:04:30,880
maybe I'll also set the title to Not found!,

87
00:04:30,880 --> 00:04:33,600
then you will see that if I save this,

88
00:04:33,600 --> 00:04:36,433
I added this template, now if I reload, we see that.

89
00:04:37,740 --> 00:04:40,580
So that's how we can then handle the error ourselves.

90
00:04:40,580 --> 00:04:43,980
Again, theoretically, you should consider doing that

91
00:04:43,980 --> 00:04:45,900
for all your tasks,

92
00:04:45,900 --> 00:04:50,610
for all the actions where you reach out to the database,

93
00:04:50,610 --> 00:04:54,510
because all those things can theoretically fail.

94
00:04:54,510 --> 00:04:57,560
However, to keep that code fairly structured,

95
00:04:57,560 --> 00:04:59,320
I will not do it here.

96
00:04:59,320 --> 00:05:02,080
We will again see proper error handling

97
00:05:02,080 --> 00:05:03,970
in the next course section, though,

98
00:05:03,970 --> 00:05:06,290
which is about that main course project

99
00:05:06,290 --> 00:05:08,630
where we build this online shop.

100
00:05:08,630 --> 00:05:11,493
But this is a specific case you should be aware of.

