Jump to content

How to get 404 exception message inside template file?


abdus
 Share

Recommended Posts

While coding some templates I need to throw some Wire404Exceptions, which, expectedly, sets correct headers and renders 404 page. Since all these exceptions are extending Exception class, I can call its constructor with a message like this

<?php
$demoName = sanitizer()->pageName(input()->urlSegment1);
$demo = pages('template=demo, name=$sanitizedPagenameFrom');

if(!$demo->id) {
	throw new Wire404Exception("Demo named $demoName cannot be found, but check out its source page instead");
}

// inside template 404.php

// somehow get the error $message
echo "<h1>Page does not exist</h1>"
echo "<p>$message</p>"

What I want to do is to somehow catch this exception, or get inside 404 template the message that I set earlier in some module or another function, so that I can notify the user with a better message than "Error 404: Page not found".

There's some parts inside core that handles this sort of things but they're not utilized. 

<?php
// inside /wire/core/ProcessPageView.php

public function ___execute($internal = true) {
    // ...
    try {
	    // try to render the page
    } catch(Wire404Exception $e) {
        return $this->pageNotFound($page, $this->requestURL, false, '404 thrown during render');
    }
    // ...
}

protected function ___pageNotFound($page, $url, $triggerReady = false, $reason = '') {
	// reason is not used, or passed to 404 template
}

So, is there a way to manually catch the exception thrown? For instance, with Silex all exceptions can be intercepted using

<?php
$app = new Silex\Application();
$app->error(function (\Exception $e, $code) {
    exit('asd');
});

 

Link to comment
Share on other sites

Huh, turns out there's this method inside ProcessPageView class

<?php
// /wire/core/ProcessPageView.php
	/**
	 * Hook called when the pageview failed to finish due to an exception.
	 *
	 * Sends a copy of the exception that occurred.
	 *
 	 */
	public function ___failed(\Exception $e) {
		$this->wire()->setStatus(ProcessWire::statusFailed);
	}

which I should be able to hook and access the exception and message with

<?php
wire()->addHookBefore('ProcessPageView::failed', function(HookEvent $e) {
	$ex = $e->arguments(0);
	$message = $ex->getMessage();
})

But, it's not firing when hooked inside /site/ready.php. In fact it does not appear to be used anywhere inside the core at all.

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.

  • Similar Content

    • By SwimToWin
      I have a nested page structure that fails for users without superuser permissions:
      Works for superusers / non-superusers:
          - foo
          -- bar
          --- page (status: published)
          ---- page (status: published)
          --- page (status: published)
      Fails for non-superusers (Works for superusers):
          - foo
          -- bar
          --- page (status: published)
          ---- page (status: published)
          --- page (status: unpublished <- apparently the template structure fails when there's one unpublished page)
      Template:
      <?php foreach ($page->children('include=all') as $p): #Fails for non-superusers ?> <?=$p->render()?> <?php endforeach; ?>     a) Works for non-superusers when I grant Page Edit permissions (on the template) to their assigned role/s.
          b) When I remove 'include=all' or 'include=unpublished' then it also works for non-superusers:
      <?php foreach ($page->children() as $p): #Works for non-superusers ?> <?=$p->render()?> <?php endforeach; ?> Error message (non-superusers)
          Internal Server Error
              The server encountered an internal error or misconfiguration and was unable to complete your request.
              Error has been logged.
          /www/site/assets/logs/errors.txt
              /foo/bar/baz/    Error:     Exception: Page '/foo/bar/baz/quz/' is not currently viewable.
          /www/site/assets/logs/exceptions.txt
              /foo/bar/baz/    Page '/foo/bar/baz/quz/' is not currently viewable. (in /wire/modules/PageRender.module line 410)
      Debug (non-superusers)
          Error: Exception: Page '/foo/bar/baz/quz/' is not currently viewable. (in wire/modules/PageRender.module line 410)
          #0 wire/core/Wire.php (397): PageRender->___renderPage(Object(HookEvent))
          #1 wire/core/WireHooks.php (823): Wire->_callMethod('___renderPage', Array)
          #2 wire/core/Wire.php (465): WireHooks->runHooks(Object(PageRender), 'renderPage', Array)
          #3 wire/core/WireHooks.php (924): Wire->__call('renderPage', Array)
          #4 wire/core/Wire.php (465): WireHooks->runHooks(Object(Page), 'render', Array)
          #5 site/templates/template.php (191): Wire->__call('render', Array)
       
    • By Peter Knight
      Hi guys
      is there a checklist of items I should consider for diagnosing a series of pages which display a 404 error?
      Basically I have built a Blog and all my posts are under a folder called 'Posts'. Some of these pages successfully display while others display a 404 page. 
      This is what I have confirmed so far...
      1. All pages are within the same root folder 'Posts'
      2. All pages have the same template and fields completed
      3. All pages have the same properties under Settings > Who can access this page
      4. Pages have no redirects in place
      5. Pages have no special characters or anything funky in the URL slug
      There are no errors showing in the log either so I am stuck 😕
      Thanks
    • By horst
      Hey,
      I want to hook into the 404 handling of PW at the most earliest stage. Pleas can anybody point me to the hook or file that suits this best?
    • By dweeda
      I installed an SSL Certificate, then edited my .htaccess file:
        # -----------------------------------------------------------------------------------------------
        # 9. If you only want to allow HTTPS, uncomment the RewriteCond and RewriteRule lines below.
        # -----------------------------------------------------------------------------------------------
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      by uncommenting out the Rewrite lines.
      Now I get 404 error pages when I try go to any .../processwire-master/<pagename>/
      This includes my admin page at .../processwire-master/processwire/, so i can't get into my admin.
      What else do I need to do?
    • By joeck
      Hi Guys,
      I'm trying to do my first migration to the customers existing server (IIS 10) . I ran the site as a subdirectory on my website for test purposes (everything works fine).
      Following the tutorial of Joss, I tryed the site on a local xampp server to make sure, it also works on a root directory. So far so good, everything works.
      Now I moved the files (from the xampp) to the customers server. The root/index page is shown but for every subpage i get 404 Errors...
      Hence I followed the troubleshooting guide for not working URLs:
      On the first sight, the .htaccess file is not recognized, therefore I contacted the host support. They said, it is recognized but not all modules are supported in the processwire .htaccess file. I did the "öalskjfdoal" test in the .htaccess file and didn't get a 500 Error.... BUT the rewrite rule from the hosts support, to proof the file is read, DID work... The support claims, they do not provide debugging... so basically the .htaccess file is recognized and working, but not throwing any errors (for whatever reason).
      Working rewrite rule (from support):
      RewriteEngine On RewriteBase / RewriteRule ^test\.asp$ index.html [NC,L] RewriteRule ^test\.html$ konzept.html [NC,L] RewriteRule ^test2\.html$ team.html [NC,L] The support said, a couple modules are not supported in the htaccess file, the supported ones are listed here: http://www.helicontech.com/ape/ (I think mod_rewrite is supported)
      As I do not completely understand what exactly is happening in the htaccess file, I'm stuck. I tried all suggestions I found regarding this topic on the forum, but none of them solved the problem.
       
      .htaccess.txt
×
×
  • Create New...