Skip to content

Commit

Permalink
Show 404 page in production when a route is not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
perdjurner committed May 12, 2017
1 parent 3410c36 commit 4240345
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
17 changes: 5 additions & 12 deletions wheels/controller/processing.cfm
Expand Up @@ -137,18 +137,11 @@ public void function $callAction(required string action) {
if (FileExists(ExpandPath(local.file))) {
Throw(object=e);
} else {
if ($get("showErrorInformation")) {
Throw(
type="Wheels.ViewNotFound",
message="Could not find the view page for the `#arguments.action#` action in the `#variables.$class.name#` controller.",
extendedInfo="Create a file named `#LCase(arguments.action)#.cfm` in the `views/#LCase(ListChangeDelims(variables.$class.name, '/', '.'))#` directory (create the directory as well if it doesn't already exist)."
);
} else {
$header(statusCode=404, statustext="Not Found");
local.template = $get("eventPath") & "/onmissingtemplate.cfm";
$includeAndOutput(template=local.template);
abort;
}
$throwErrorOrShow404Page(
type="Wheels.ViewNotFound",
message="Could not find the view page for the `#arguments.action#` action in the `#variables.$class.name#` controller.",
extendedInfo="Create a file named `#LCase(arguments.action)#.cfm` in the `views/#LCase(ListChangeDelims(variables.$class.name, '/', '.'))#` directory (create the directory as well if it doesn't already exist)."
);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions wheels/dispatch/functions.cfm
Expand Up @@ -105,10 +105,10 @@ public struct function $findMatchingRoute(required string path, string requestMe
// Throw error if no route was found.
if (!StructKeyExists(local, "rv")) {
Throw(
$throwErrorOrShow404Page(
type="Wheels.RouteNotFound",
message="Wheels couldn't find a route that matched this request.",
extendedInfo="Make sure there is a route setup in your `config/routes.cfm` file that matches the `#arguments.path#` request."
message="Could not find a route that matched this request.",
extendedInfo="Make sure there is a route configured in your `config/routes.cfm` file that matches the `#arguments.path#` request."
);
}
Expand Down
25 changes: 22 additions & 3 deletions wheels/global/internal.cfm
Expand Up @@ -386,9 +386,13 @@ public string function $routeVariables() {
*/
public struct function $findRoute() {
// throw an error if a route with this name has not been set by developer in the config/routes.cfm file
if (application.wheels.showErrorInformation && !StructKeyExists(application.wheels.namedRoutePositions, arguments.route)) {
Throw(type="Wheels.RouteNotFound", message="Could not find the `#arguments.route#` route.", extendedInfo="Create a new route in `config/routes.cfm` with the name `#arguments.route#`.");
// Throw error if no route was found.
if (!StructKeyExists(application.wheels.namedRoutePositions, arguments.route)) {
$throwErrorOrShow404Page(
type="Wheels.RouteNotFound",
message="Could not find the `#arguments.route#` route.",
extendedInfo="Make sure there is a route configured in your `config/routes.cfm` file named `#arguments.route#`."
);
}
local.routePos = application.wheels.namedRoutePositions[arguments.route];
Expand Down Expand Up @@ -1092,4 +1096,19 @@ public string function $buildReleaseZip(string version=application.wheels.versio
return local.path;
}
/**
* Throw a developer friendly CFWheels error if set (typically in development mode).
* Otherwise show the 404 page for end users (typically in production mode).
*/
public void function $throwErrorOrShow404Page(required string type, required string message, string extendedInfo="") {
if ($get("showErrorInformation")) {
Throw(type=arguments.type, message=arguments.message, extendedInfo=arguments.extendedInfo);
} else {
$header(statusCode=404, statustext="Not Found");
local.template = $get("eventPath") & "/onmissingtemplate.cfm";
$includeAndOutput(template=local.template);
abort;
}
}
</cfscript>
10 changes: 5 additions & 5 deletions wheels/view/forms.cfm
Expand Up @@ -65,12 +65,12 @@ public string function startFormTag(
// if we have a route and method, tap
if (Len(arguments.route) && StructKeyExists(arguments, "method")) {
// throw a nice wheels error if the developer passes in a route that was not generated
if (application.wheels.showErrorInformation && !StructKeyExists(application.wheels.namedRoutePositions, arguments.route)) {
Throw(
// Throw error if no route was found.
if (!StructKeyExists(application.wheels.namedRoutePositions, arguments.route)) {
$throwErrorOrShow404Page(
type="Wheels.RouteNotFound",
message="Route Not Found",
extendedInfo="The route specified `#arguments.route#` does not exist!"
message="Could not find the `#arguments.route#` route.",
extendedInfo="Make sure there is a route configured in your `config/routes.cfm` file named `#arguments.route#`."
);
}
Expand Down

0 comments on commit 4240345

Please sign in to comment.