1 00:00:00,330 --> 00:00:03,460 Okay, we need to communicate a little bit better 2 00:00:03,460 --> 00:00:05,880 with our users about the state of their experience. 3 00:00:05,880 --> 00:00:09,570 And of course, because the only state the exist is what we've made 4 00:00:09,570 --> 00:00:13,800 appeared to be stateful, we're gonna need to jump through some hoops to get there. 5 00:00:13,800 --> 00:00:17,430 A common approach to solve the problem of keeping the user informed about their 6 00:00:17,430 --> 00:00:21,120 actions is to use what is known as a flash message. 7 00:00:21,120 --> 00:00:23,450 You've seen them, they're usually displayed in a box on a page. 8 00:00:23,450 --> 00:00:26,528 It says something like this, see the message here that says, 9 00:00:26,528 --> 00:00:28,640 all right your details were updated. 10 00:00:28,640 --> 00:00:32,240 Now most fully featured frameworks provide this nice to have feature, but 11 00:00:32,240 --> 00:00:36,250 some micro-frameworks, ours in particular,don't quite have those yet. 12 00:00:36,250 --> 00:00:38,560 But that doesn't mean we can't craft our own. 13 00:00:38,560 --> 00:00:42,380 The really nice thing that our micro-framework provides is the handling 14 00:00:42,380 --> 00:00:43,820 of exceptions. 15 00:00:43,820 --> 00:00:45,120 Let's improve the user experience. 16 00:00:46,240 --> 00:00:50,840 Okay, so first, let's take a quick look at the built-in exception handling. 17 00:00:50,840 --> 00:00:54,550 So we kicked off a few of our own custom NotFoundExceptions. 18 00:00:54,550 --> 00:00:55,870 Let's go to the definition here. 19 00:00:55,870 --> 00:00:58,430 So we're gonna go to the NotFoundException, and I'm gonna 20 00:00:58,430 --> 00:01:03,470 click in here and I'm gonna do a Cmd+B, which is gonna go to the declaration. 21 00:01:03,470 --> 00:01:06,740 And then it's going to zip over to where we did it before which is in this find by 22 00:01:06,740 --> 00:01:07,540 slug method. 23 00:01:07,540 --> 00:01:10,920 Okay, so anytime we findByslug is called and 24 00:01:10,920 --> 00:01:13,910 it can't find that slug it's gonna throw a NotFoundException. 25 00:01:13,910 --> 00:01:15,800 But, that's what we want to have happen. 26 00:01:15,800 --> 00:01:19,940 So, let's go ahead and we'll handle this NotFoundException 27 00:01:19,940 --> 00:01:22,850 when it happens because right now it just kinda a gross page right. 28 00:01:22,850 --> 00:01:25,650 So lets flip over to our main method. 29 00:01:26,950 --> 00:01:29,700 And let's scroll down here to the bottom. 30 00:01:31,070 --> 00:01:36,150 Now the name of the, Its a static method and, 31 00:01:36,150 --> 00:01:38,930 as you might expect its called exception. 32 00:01:40,070 --> 00:01:42,790 And then it takes the class that were gonna try to catch. 33 00:01:42,790 --> 00:01:45,000 So again that is our not found exception. 34 00:01:47,480 --> 00:01:51,180 And we'll do .class because we'll get the handle on the class. 35 00:01:51,180 --> 00:01:56,100 And then it takes a handler here, and the handler will return an exception and then 36 00:01:56,100 --> 00:01:59,520 of course our request and our response, so it's very similar to the other handlers. 37 00:02:01,790 --> 00:02:03,850 Okay, when something's not found, 38 00:02:03,850 --> 00:02:07,100 the proper way to tell the client is that they got a 404. 39 00:02:07,100 --> 00:02:09,930 Now, we can make this page look however we want, but 40 00:02:09,930 --> 00:02:15,300 we wanna make sure that we send the header, the status of 404. 41 00:02:15,300 --> 00:02:17,730 It's an http status code. 42 00:02:19,740 --> 00:02:23,730 Okay, now unfortunately this exception handler 43 00:02:23,730 --> 00:02:26,840 isn't set up using this template style like we were doing before right? 44 00:02:26,840 --> 00:02:30,800 Where there's the three parameters and we pass this new handlebars template engine, 45 00:02:30,800 --> 00:02:34,750 but that doesn't mean we can't use the template engine still. 46 00:02:34,750 --> 00:02:39,262 So this will help actually demystify what was happening using that so 47 00:02:39,262 --> 00:02:42,392 what we can do, is we can create our own engine. 48 00:02:42,392 --> 00:02:45,492 So let's do that we'll a handlebars templating engine. 49 00:02:45,492 --> 00:02:51,751 We'll say engine = new HandlebarsTemplateEngine. 50 00:02:51,751 --> 00:02:55,490 And really the only thing this is doing is calling render. 51 00:02:55,490 --> 00:02:57,650 There's a thing on the engine called render. 52 00:02:57,650 --> 00:03:05,050 And it returns html, so we'll = engine say it's String html- .render. 53 00:03:05,050 --> 00:03:07,780 This is what was passed in here, right? 54 00:03:07,780 --> 00:03:11,540 So it takes whatever was here, creates a new one of these and 55 00:03:11,540 --> 00:03:14,120 then pushes this into the render method. 56 00:03:14,120 --> 00:03:15,250 So we'll just push that in here. 57 00:03:15,250 --> 00:03:18,100 So we'll say new ModelAndView. 58 00:03:20,020 --> 00:03:23,750 And we don't have a model right now so we'll just say null. 59 00:03:23,750 --> 00:03:26,730 But we're going to pass in a new template that we're going to build here in 60 00:03:26,730 --> 00:03:29,710 a second, so not found.hbs. 61 00:03:29,710 --> 00:03:32,280 Remember, if stuff starts getting a little out of control, 62 00:03:32,280 --> 00:03:35,150 you can always add it to a new line, everything will still be happy. 63 00:03:37,150 --> 00:03:37,650 There we go. 64 00:03:39,010 --> 00:03:40,980 Okay, so now we have html and string and 65 00:03:40,980 --> 00:03:43,450 we need to just set the body of the response. 66 00:03:43,450 --> 00:03:47,478 Now, you don't return from this exception handler, you set the body. 67 00:03:47,478 --> 00:03:51,100 Here we go. 68 00:03:51,100 --> 00:03:53,660 Okay, so, let's go make our new template. 69 00:03:53,660 --> 00:03:56,939 Make a new file 70 00:04:01,638 --> 00:04:04,889 Called not found.hps. 71 00:04:07,550 --> 00:04:12,650 Okay, and we will again, everything will start working like normal, 72 00:04:12,650 --> 00:04:14,000 cuz we're using that engine. 73 00:04:14,000 --> 00:04:19,113 So we'll say content, partial content, and 74 00:04:19,113 --> 00:04:27,207 let's set the h1 to be what the You can do something clever in here. 75 00:04:27,207 --> 00:04:32,173 So we couldn't find what 76 00:04:32,173 --> 00:04:36,669 you were looking for. 77 00:04:40,541 --> 00:04:44,400 Of course we could also override that title if we wanted to. 78 00:04:44,400 --> 00:04:49,870 Say partial title and not found. 79 00:04:52,870 --> 00:04:58,560 And of course we need to get our base in here to be implemented. 80 00:05:00,710 --> 00:05:02,570 So we can definitely make this prettier and 81 00:05:02,570 --> 00:05:05,350 push more information into the model that displays on this page. 82 00:05:05,350 --> 00:05:07,250 There's some more in the teacher's notes. 83 00:05:07,250 --> 00:05:11,660 Okay, so let's make sure that we caught our exception, so I'm gonna go over and 84 00:05:11,660 --> 00:05:17,380 I'm gonna start my server, and say Run Main. 85 00:05:19,690 --> 00:05:21,370 Okay, so he's running. 86 00:05:21,370 --> 00:05:25,970 Okay, so let's go over to the page and I'm gonna go ahead and log in. 87 00:05:25,970 --> 00:05:31,100 Let's go ahead and let's make a new 88 00:05:32,760 --> 00:05:36,650 Idea here, again we Spark Testing 89 00:05:36,650 --> 00:05:39,580 now that Spark Testing exists I'm going to click on Spark Testing. 90 00:05:40,880 --> 00:05:45,630 And if I come and I change this just to be a little bit off, 91 00:05:45,630 --> 00:05:48,690 we're going to change the slug to be spelled wrong and press enter. 92 00:05:48,690 --> 00:05:49,880 Now I'll get our what the page. 93 00:05:49,880 --> 00:05:55,840 And you'll see that there is a status code, a 404 not found, awesome. 94 00:05:57,810 --> 00:06:01,710 Now, if we wanted more specific pages, we'd just make more specific exceptions. 95 00:06:01,710 --> 00:06:03,950 It's pretty straightforward, right? 96 00:06:03,950 --> 00:06:06,930 So now that we've explored the built in messaging, let's take a quick break right 97 00:06:06,930 --> 00:06:10,140 here, and then we'll drive right back into making our own flash messages.