Jump to content

Can't get PageNotFoundException() to work


JayGee
 Share

Recommended Posts

I'm using URL segments to pull some dynamic content into a page. The template uses the URL segment variable to check the content exists in another part of the site tree, if not I'm trying to throw a 404. Looking at other threads on here I think the below should do the job.

throw new PageNotFoundException();

But I'm getting the following error:

Fatal Error: Uncaught Error: Class 'PageNotFoundException' not found in...

Is anyone able to shed any light on how I'm using this wrong? The code is currently in an include which seemed to have previously triggered some debate, but I get the same error even if I move it to the top of the parent page.

I've also created a plain 404 template that doesn't include any other template elements just to rule out a code loop.

Link to comment
Share on other sites

13 minutes ago, wbmnfktr said:

You're my guru today! Lol! ?

Yes have seen this guide and have used segments loads of times. I know the script it working and breaking at the right places because if I drop the throw new PageNotFoundException(); call and just echo out a message instead it works fine.

It's just the 404 call that seems to error. Doesn't seem to be able to find the class.

Here's the snippet if it helps:

//URL Routing for category pages and tags
if($currentTemplate == 'category') {

    ###################################################
    ## Routing only required for category and tag pages
    ###################################################

	//Segment 1 == category/sector
	$currentCat = $sanitizer->text($input->urlSegment1);
	//Segment 2 == content tag e.g. marketing
	$currentTag = $sanitizer->text($input->urlSegment2);

    //Check requested category exists (in hidden categories structure)
    //404 if not, otherwise continue rendering page
	$catPage = $pages->get('/categories/')->children("name=$currentCat,include=hidden,limit=1");
	$catPage = $catPage[0];

    if(!$catPage->id) {
        throw new PageNotFoundException(); 
    }
     
}

Probably doing something stupid.

Link to comment
Share on other sites

2 minutes ago, wbmnfktr said:

You use PageNotFoundException() while I only know and see Wire404Exception() in examples.

Does it make any difference?

Nope, gives the same error:

Fatal Error: Uncaught Error: Class 'Wire404Exception' not found in...

 

Link to comment
Share on other sites

2 minutes ago, wbmnfktr said:

So... I just played with it a bit.

Does NOT work: throw new PageNotFoundException(); 
Does Work: throw new Wire404Exception();

Tested it on a ProcessWire 3.0.133 installation. Are you maybe running the DEV 3.0.140 version?

Interesting - yes am running the latest Dev version for this project as want to play with the new cookies api ? - shouldn't be an issue should it? Unless something has been changed/deprecated?

Link to comment
Share on other sites

The 3.0.140 brought some issues with 3rd party modules and I try to avoid the DEV version at the moment for that reason. Therefore this was just a guess but maybe the DEV version also affects core features like this. Try the latest Master (3.0.123) if possible. If that's the fix we have found the suspect.

By now your code snippet seems fine to me - at least the modified (other if statements and Wire404Exception) version works as expected here.

  • Like 1
Link to comment
Share on other sites

1 minute ago, wbmnfktr said:

The 3.0.140 brought some issues with 3rd party modules and I try to avoid the DEV version at the moment for that reason. Therefore this was just a guess but maybe the DEV version also affects core features like this. Try the latest Master (3.0.123) if possible. If that's the fix we have found the suspect.

By now your code snippet seems fine to me - at least the modified (other if statements and Wire404Exception) version works as expected here.

Yes, you're probably right. I do usually use Master for most projects, just working on something that allows for a little more experimentation. I'll try it on another install and see if it is the dev version causing this. Thanks for your assistance (again!)

@ryan - don't know if you can think of anything off the top of your head in the latest dev that might cause the issues above? If I can confirm it later in another install I log it on github.

Link to comment
Share on other sites

The same error is being discussed in this thread where throwing a 404 exception in an include file throws an exception (well, a different one). In that thread it seems that there are different scenarios that can trigger this.

What I'd suggest right now is to install the Tracy module if you haven't yet to give you a better look at what's happening.

  • Like 1
Link to comment
Share on other sites

1 hour ago, Guy Incognito said:

It's just the 404 call that seems to error. Doesn't seem to be able to find the class.

Is your include file namespaced? If not,

throw new \ProcessWire\PageNotFoundException();

should do the trick. Or adding "namespace ProcessWire;" at the top, obviously.

  • Like 2
Link to comment
Share on other sites

12 minutes ago, BitPoet said:

Is your include file namespaced? If not,

Ok - good call on the namespacing.

Can confirm the following combo of using wire404Exception() instead of PageNotFoundException() and namespacing works as follow:

throw new \ProcessWire\wire404Exception();

Thanks @wbmnfktr and @BitPoet - got there in there end!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...