Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Letβs explore how to handle exceptions with grace and ease. You will write your own Flash messaging tool which you will use to inform your users one time over redirects.
Explore
- One way you to get around the duplication is by taking advantage of the request.attributes and creating a model object in a global before filter. That would allow you to do whatever injecting of the model that was common to all requests. In the controller you just then pull out request.attributes[βmodelβ] and add specific information, but it would have the flash message.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, we need to communicate
a little bit better
0:00
with our users about the state
of their experience.
0:03
And of course, because the only
state that exists is what we've made
0:05
appear to be stateful, we're gonna need
to jump through some hoops to get there.
0:09
A common approach to solve the problem
of keeping the user informed about their
0:13
actions is to use what is
known as a flash message.
0:17
You've seen them, they're usually
displayed in a box on a page.
0:21
It says something like this,
see the message here that says,
0:23
all right your details were updated.
0:26
Now most fully featured frameworks
provide this nice to have feature, but
0:28
some micro-frameworks, ours in
particular, don't quite have those yet.
0:32
But that doesn't mean
we can't craft our own.
0:36
The really nice thing that our
micro-framework provides is the handling
0:38
of exceptions.
0:42
Let's improve the user experience.
0:43
Okay, so first, let's take a quick look
at the built-in exception handling.
0:46
So we kicked off a few of our
own custom NotFoundExceptions.
0:50
Let's go to the definition here.
0:54
So we're gonna go to
the NotFoundException, and I'm gonna
0:55
click in here and I'm gonna do a Cmd+B,
which is gonna go to the declaration.
0:58
And then it's going to zip over to where
we did it before which is in this
1:03
findBySlug method.
1:05
Okay, so
anytime we findBySlug is called and
1:07
it can't find that slug it's
gonna throw a NotFoundException.
1:10
But, that's what we want to have happen.
1:13
So, let's go ahead and
we'll handle this NotFoundException
1:15
when it happens because right now
it just kinda a gross page right.
1:19
So let's flip over to our main method.
1:22
And let's scroll down here to the bottom.
1:26
Now the name of the,
its a static method and,
1:31
as you might expect it's called exception.
1:36
And then it takes the class
that we're gonna try to catch.
1:40
So again, that is our NotFoundException.
1:42
And we'll do .class because we'll
get the handle on the class.
1:47
And then it takes a handler here, and the
handler will return an exception and then
1:51
of course our request and our response, so
it's very similar to the other handlers.
1:56
Okay, when something's not found,
2:01
the proper way to tell the client
is that they got a 404.
2:03
Now, we can make this page
look however we want, but
2:07
we wanna make sure that we send
the header, the status of 404.
2:09
It's an http status code.
2:15
Okay, now unfortunately
this exception handler
2:19
isn't set up using this template style
like we were doing before right?
2:23
Where there's the three parameters and we
pass this new handlebars template engine,
2:26
but that doesn't mean we can't
use the template engine still.
2:30
So this will help actually demystify
what was happening using that so
2:34
what we can do,
is we can create our own engine.
2:39
So let's do that we'll
a handlebars templating engine.
2:42
We'll say engine = new
HandlebarsTemplateEngine.
2:45
And really the only thing this
is doing is calling render.
2:51
There's a thing on
the engine called render.
2:55
And it returns html, so we'll =
engine say it's String html- .render.
2:57
This is what was passed in here, right?
3:05
So it takes whatever was here,
creates a new one of these and
3:07
then pushes this into the render method.
3:11
So we'll just push that in here.
3:14
So we'll say new ModelAndView.
3:15
And we don't have a model right now so
we'll just say null.
3:20
But we're going to pass in a new template
that we're going to build here in
3:23
a second, so not-found.hbs.
3:26
Remember, if stuff starts
getting a little out of control,
3:29
you can always add it to a new line,
everything will still be happy.
3:32
There we go.
3:37
Okay, so now we have html and string and
3:39
we need to just set
the body of the response.
3:40
Now, you don't return from this
exception handler, you set the body.
3:43
Here we go.
3:47
Okay, so, let's go make our new template.
3:51
Make a new file
3:53
Called not-found.hps.
4:01
Okay, and we will again,
everything will start working like normal,
4:07
cuz we're using that engine.
4:12
So we'll say content, partial content, and
4:14
let's set the h1 to be what the You
can do something clever in here.
4:19
So we couldn't find what
4:27
you were looking for.
4:32
Of course we could also override
that title if we wanted to.
4:40
Say partial title and not found.
4:44
And of course we need to get our
base in here to be implemented.
4:52
So we can definitely
make this prettier and
5:00
push more information into the model
that displays on this page.
5:02
There's some more in the teacher's notes.
5:05
Okay, so let's make sure that we caught
our exception, so I'm gonna go over and
5:07
I'm gonna start my server,
and say Run Main.
5:11
Okay, so he's running.
5:19
Okay, so let's go over to the page and
I'm gonna go ahead and log in.
5:21
Let's go ahead and let's make a new
5:25
Idea here, again we Spark Testing
5:32
now that Spark Testing exists I'm
going to click on Spark Testing.
5:36
And if I come and
I change this just to be a little bit off,
5:40
we're going to change the slug to
be spelled wrong and press enter.
5:45
Now I'll get our what the page.
5:48
And you'll see that there is a status
code, a 404 not found, awesome.
5:49
Now, if we wanted more specific pages,
we'd just make more specific exceptions.
5:57
It's pretty straightforward, right?
6:01
So now that we've explored the built in
messaging, let's take a quick break right
6:03
here, and then we'll drive right back
into making our own flash messages.
6:06
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up