I am trying to display error message if req was too short. Here is code:
import std.stdio;
import vibe.d;
Database mydatabase;
void main()
{
// ...
router.get("*", &myStuff); // all other request
listenHTTP(settings, router);
runApplication();
}
@errorDisplay!showPageNotFound
void myStuff(HTTPServerRequest req, HTTPServerResponse res) // I need this to handle any accessed URLs
{
if(req.path.length > 10)
{
// ...
}
else
{
throw new Exception("Nothing do not found");
}
}
void showPageNotFound(string _error = null)
{
render!("error.dt", _error);
}
The error is:
source\app.d(80,2): Error: template instance app.showPageNotFound.render!("error.dt", _error).render!("app", "app.showPageNotFound") error instantiating
If I am doing:
void showPageNotFound(string _error = null)
{
res.render!("error.dt", _error);
}
I am getting error:
Error: undefined identifier 'res'
If you look at the error above the
error instantiatingone, you'll see thatvibe.dtries to callinitmethod of the parent class whererender!is called, however your code doesn't have a parent class.This means that currently you can't render any templates in functions called by
errorDisplaythat are outside a class. In fact, when passing&((new NewWebService).myStufftorouter.any,errorDisplaydoesn't work at all (bug?). All examples invibe.drepository use a class witherrorDisplay.You could wrap
getStuffandshowPageNotFoundinside a class, but thenrouter.any("*", ...is not a possibility, as it's for individual functions only, and@pathattribute doesn't support wildcards when used withregisterWebInterface.Solution to this would be instead of throwing exception, render the error inside
myStuff. Albeit a poor one, as I think you wanted to useerrorDisplay.A better solution would be implementing functionality in
vibe.dto passreqparameter to functions called byerrorDisplay(and fixing a bug? thaterrorDisplaycan't be used outside a class), or even better, support wildcards in@pathwhen used withregisterWebInterface.