1
00:00:02,029 --> 00:00:04,000
Now we made huge progress,

2
00:00:04,000 --> 00:00:06,100
and we get the general sign up

3
00:00:06,100 --> 00:00:09,320
and authentication flows implemented,

4
00:00:09,320 --> 00:00:11,850
still there is room for improvement.

5
00:00:11,850 --> 00:00:14,120
For example, we have no validation,

6
00:00:14,120 --> 00:00:17,040
and we show no validation errors to the user

7
00:00:17,040 --> 00:00:19,730
if we enter incorrect data here

8
00:00:19,730 --> 00:00:22,680
or if we have deviating email addresses here

9
00:00:22,680 --> 00:00:25,760
when we sign up, so that's something we wanna add.

10
00:00:25,760 --> 00:00:26,920
But before we do that,

11
00:00:26,920 --> 00:00:30,500
I wanna come back to error handling in general.

12
00:00:30,500 --> 00:00:33,100
We did add the error-handler middleware

13
00:00:33,100 --> 00:00:35,290
a couple of minutes ago.

14
00:00:35,290 --> 00:00:39,320
But this middleware, which is executed by Express

15
00:00:39,320 --> 00:00:42,173
automatically if an error occurs, has a flow.

16
00:00:43,030 --> 00:00:45,940
Yes, Express will execute it automatically

17
00:00:45,940 --> 00:00:49,330
if some error occurs in any other middleware.

18
00:00:49,330 --> 00:00:53,360
But unfortunately, Express ignores errors

19
00:00:53,360 --> 00:00:57,940
that are occurring inside of asynchronous operations.

20
00:00:57,940 --> 00:01:00,690
So if I await the user sign up here

21
00:01:00,690 --> 00:01:04,680
and inside the signup method or some other function called

22
00:01:04,680 --> 00:01:08,000
inside of the sign up method; something goes wrong,

23
00:01:08,000 --> 00:01:11,693
the Express middleware handler will not become active.

24
00:01:12,800 --> 00:01:15,260
Therefore, we have to handle this manually,

25
00:01:15,260 --> 00:01:17,030
and we learned how we can do this

26
00:01:17,030 --> 00:01:21,303
early in the course as well: with help of try catch.

27
00:01:23,270 --> 00:01:27,510
Try catch allows us to implement our own error handling,

28
00:01:27,510 --> 00:01:31,060
and it allows us to try some operation.

29
00:01:31,060 --> 00:01:35,060
And we can use try catch when we use async await

30
00:01:35,060 --> 00:01:37,970
to then catch a potential error,

31
00:01:37,970 --> 00:01:40,770
which we receive as a parameter value here,

32
00:01:40,770 --> 00:01:43,000
in case this operation

33
00:01:43,000 --> 00:01:46,663
or some nested operation in there goes wrong.

34
00:01:48,180 --> 00:01:50,370
And then here in the catch case,

35
00:01:50,370 --> 00:01:54,440
to use the default Express error handling middleware,

36
00:01:54,440 --> 00:01:56,290
what we can do is we can use

37
00:01:56,290 --> 00:02:00,010
that third parameter value, this "next" function,

38
00:02:00,010 --> 00:02:04,120
which we always get in all our middleware functions,

39
00:02:04,120 --> 00:02:07,000
and our controller actions here are

40
00:02:07,000 --> 00:02:09,270
just middleware functions in the end.

41
00:02:09,270 --> 00:02:12,363
And we can call next here in catch,

42
00:02:13,630 --> 00:02:15,580
and pass the error to it,

43
00:02:15,580 --> 00:02:18,270
and then either return this

44
00:02:18,270 --> 00:02:21,780
or return thereafter, doesn't matter.

45
00:02:21,780 --> 00:02:24,180
So that's the next line doesn't execute

46
00:02:25,270 --> 00:02:27,660
because when we pass an error to next

47
00:02:27,660 --> 00:02:30,120
and we execute next with that error,

48
00:02:30,120 --> 00:02:33,690
the default error handling middleware will become active,

49
00:02:33,690 --> 00:02:36,220
and in our case, that middleware will

50
00:02:36,220 --> 00:02:39,283
then render this 500 template.

51
00:02:40,740 --> 00:02:43,330
And then I don't wanna redirect in addition to it,

52
00:02:43,330 --> 00:02:45,400
which is why we have to return thereafter

53
00:02:45,400 --> 00:02:48,390
so that this code doesn't execute.

54
00:02:48,390 --> 00:02:51,720
But it's this kind of custom error handling logic

55
00:02:51,720 --> 00:02:53,760
that we wanna add to all the parts

56
00:02:53,760 --> 00:02:56,940
in our code that could possibly fail.

57
00:02:56,940 --> 00:03:01,710
For example, here when we get a user with the same email;

58
00:03:01,710 --> 00:03:05,120
I wanna try this:

59
00:03:05,120 --> 00:03:08,030
catch the error here

60
00:03:08,030 --> 00:03:12,343
and call next error if that fails.

61
00:03:14,830 --> 00:03:18,400
So here I call next error then if that fails,

62
00:03:18,400 --> 00:03:19,950
and then return thereafter.

63
00:03:19,950 --> 00:03:21,510
Because if that fails,

64
00:03:21,510 --> 00:03:24,623
we don't wanna execute any of our code in this action.

65
00:03:25,470 --> 00:03:28,990
And now with this approach, we just would face one problem:

66
00:03:28,990 --> 00:03:33,990
The existing user constant now is scoped to this try block.

67
00:03:34,280 --> 00:03:36,060
You'll learn about variable scoping

68
00:03:36,060 --> 00:03:37,560
before in this course as well,

69
00:03:37,560 --> 00:03:39,700
and it basically means that a variable

70
00:03:39,700 --> 00:03:43,310
or constant is only available inside of the block

71
00:03:43,310 --> 00:03:46,430
of curly braces in which it is defined;

72
00:03:46,430 --> 00:03:48,053
in this case, inside of try.

73
00:03:49,100 --> 00:03:51,670
But I wanna use existing user thereafter

74
00:03:51,670 --> 00:03:53,830
and to make it available thereafter.

75
00:03:53,830 --> 00:03:55,250
We simply have to define it

76
00:03:55,250 --> 00:03:57,650
as a variable in an outer block,

77
00:03:57,650 --> 00:04:00,120
and then assign a value in the inner block;

78
00:04:00,120 --> 00:04:02,850
that is allowed, you can access variables

79
00:04:02,850 --> 00:04:04,730
that are scoped to an outer block

80
00:04:04,730 --> 00:04:06,740
from inside the inner block;

81
00:04:06,740 --> 00:04:09,460
the other way around doesn't work though,

82
00:04:09,460 --> 00:04:11,630
which is why we can solve this problem like this,

83
00:04:11,630 --> 00:04:14,200
and then handle this potential error.

84
00:04:14,200 --> 00:04:17,913
And with that, we also improve our log in function here.

85
00:04:19,649 --> 00:04:21,760
We could add more error handling

86
00:04:21,760 --> 00:04:24,610
but comparing the password, actually shouldn't fail here.

87
00:04:24,610 --> 00:04:27,120
So therefore, that's the only error handling

88
00:04:27,120 --> 00:04:29,073
I wanna add here in this case.

89
00:04:30,370 --> 00:04:32,960
But with that, we improved our functions

90
00:04:32,960 --> 00:04:35,500
to add better error handling.

91
00:04:35,500 --> 00:04:39,760
With that done, we can now work on validating the user input

92
00:04:39,760 --> 00:04:41,850
and showing some feedback to the user.

93
00:04:41,850 --> 00:04:45,903
If, for example, different email addresses were provided.

