Error pages in modules

Still working on updating my files to a new version of Silverstripe. We have written a little module that does a very important job for us and I am making it work. How do I make incorrect URIs go to the 404 error page?

Eg. My module has routing configurations for a/, a/b, a/b/c, etc.

What if instead someone does a/x/y.

The 404 page works outside of my module. What do I need to do to throw the incorrect URI to a 404?

This seems to be similar:
https://forum.silverstripe.org/t/url-routing/349

Did not solve my issue

This also seems similar
https://forum.silverstripe.org/t/controllers-routes-and-home-page/468/17?u=katbl

Also did not solve the issue

My routes now looks like the below:

 ---
 Name: ttecapproutes
 After:
   - '#rootroutes'
   - '#coreroutes'
   - '#graphqlroutes'
   - '#modelascontrollerroutes'
 ---
 
 SilverStripe\Control\Director:
   rules:
     'software//$Action/$ID/$Name': 'TTecSoftwareUpdateAPIController'
     'product': 'TTecSoftwareUpdateAPIController'

Error I am receiving:
ERROR [UNKNOWN TYPE, ERRNO 404]: IN GET /myx-project/public//software/x Line in Trace ===== SilverStripe\Dev\CliDebugView->renderTrace() DetailedErrorFormatter.php:119 SilverStripe\Logging\DetailedErrorFormatter->output(404, , , , ) DetailedErrorFormatter.php:54 SilverStripe\Logging\DetailedErrorFormatter->format(Array) HTTPResponse.php:424 SilverStripe\Control\HTTPResponse->outputBody() HTTPResponse.php:352 SilverStripe\Control\HTTPResponse->output() index.php:25

It’s a little difficult without seeing your code, but generally you can just return an http error from your controller to throw the 404 in a given condition.

eg. return $this->httpError(404);

Other scenarios may be caught automatically by the controller logic, but it depends a little on the url handlers / functions you have configured

// General approach:
throw new HTTPResponse_Exception('Invalid request', 404);
// ... or, if you are already inside a controller:
$this->httpError(404, 'Invalid request');

It sounds like you are already getting a 404 error and you explicitly want the error to return the full HTML markup of your 404 error page - is that correct?
I assume you already are using the silverstripe/errorpage module, and your 404 error page is an error page from that module.

Given that… the error text you’ve given shows that it was a CLI request. What happens if you access your route in the browser?

Yes

To the best of my knowledge.

Not quite sure what you mean here.

I can throw a 404 and get the same response as per the error message above. I can navigate the correct URIs well touch wood. The 404 error page works well outside of my route for this module. It just doesn’t work in the module.

Sorry about the long silence on this one.
I’ve tried using $this->httpError(404) in a local project and it correctly uses the 404 error page. I think there’s not enough information about your specific module set up to help here but I’m also not sure what additional information to ask for :sweat_smile:

You are a genius. That does indeed work. For anyone else, I was using $response = new HTTPResponse(’’, 404); return $response and it didn’t work.

Basically everyone told you to use $this->httpError.

1 Like