Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Intro to Java Web Development with Spark Bells and Whistles Messaging

Timothy Boland
Timothy Boland
18,237 Points

The exception route we created work doesnt work for the / or for /ideas. Why?

For example, the following will not return the custom 404 page we created:

http://localhost:4567/ljsdflkj

http://localhost:4567/ideaslkjsdf

Can anyone tell me why?

The link that you provided doesn't work.

Timothy Boland
Timothy Boland
18,237 Points

In the course....you are building the app locally, so there are no live links...these are syntactic example links from the course...if you take the course, you will understand

Timothy Boland
Timothy Boland
18,237 Points

Hmm...im surprised there is no response to the question. Has anyone taken this course?

I for one do not understand what is being asked. If I can see some code I can try to point out what is wrong. Write down the code that you do not understand and paste it on here so we can see it.

3 Answers

Those routes never call the "findBySlug" method on CourseIdea that throws the exception we're handling. I believe if you wanted to catch those errors, then you might be able to create a catch-all route at the bottom of all the routes, just above the exception handlers in main, that matches any route the others haven't, and return a new custom exception. This pattern is standard boilerplate in an express app, and I'm pretty sure the same convention would apply here.

Hi Timothy, the exception route created for the course was made for NotFoundException class, which was only implemented within the CourseIdea model -> this model was only used for pages rendered under /ideas/ directory, so you will only see the custom error page we created for any pages you attempt under "http://localhost:4567/ideas/". You will get the standard 404 page by Jetty for all the other non-existing pages beyond /ideas/.

For example, you will see the not-found.hbs template for http://localhost:4567/ideas/fswou837 but NOT for http://localhost:4567/ideasfswou837.

Also, double-check and make sure you did not miss a single line from the exception block we did from the course video:

exception(NotFoundException.class, (exc, req, res) -> {
            res.status(404);
            HandlebarsTemplateEngine engine = new HandlebarsTemplateEngine();
            String html = engine.render(new ModelAndView(null,"not-found.hbs"));
            res.body(html);
        });

Hope it helped :)

Konstantinos Pedarakis
Konstantinos Pedarakis
21,301 Points

Hello everyone!

i just stuck a bit as well on this as Timothy Boland described. I have read Nicolas Hampton 's answer and i manage to do something like this.

get("/*", new Route() {
            @Override
            public Object handle(Request request, Response response) throws Exception {
                    HandlebarsTemplateEngine engine = new HandlebarsTemplateEngine();
                    String html = engine.render(new ModelAndView(null, "404.hbs"));
                    throw new NotFoundException(html);
            }
        });

and in the NotFoundException class i created a new constructor that takes as parameter a string. Like this... So it renders the 404 page.

public class NotFoundException extends RuntimeException {

    public NotFoundException() {
    }

    public NotFoundException(String message) {
        super(message);
    }
}

Tell me if this is a proper way to handle those requests Craig Dennis.

Thanks a lot guys! :)

Sławomir Lasik
Sławomir Lasik
7,792 Points

Does it work in your app? It does in mine ;) With a few changes. Keep in mind, that when you throw NotFoundException in /* route the

        exception(NotFoundException.class, (exception, request, response) -> {
            response.status(404);
            HandlebarsTemplateEngine engine = new HandlebarsTemplateEngine();
            String html = engine.render(new ModelAndView(null, "not-found.hbs"));
            response.body(html);
        });

will be called anyway. So either you change this handler to retrieve the html that u send via NotFoundException or...

create just html (not the whole page) for the before methods in main to catch the body if present of a potential exception or error or message to be presented to the end user. I think this what Craig had in mind.