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 instantiating
one, you'll see that vibe.d
tries to call init
method of the parent class where render!
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 errorDisplay
that are outside a class. In fact, when passing &((new NewWebService).myStuff
to router.any
, errorDisplay
doesn't work at all (bug?). All examples in vibe.d
repository use a class with errorDisplay
.
You could wrap getStuff
and showPageNotFound
inside a class, but then router.any("*", ...
is not a possibility, as it's for individual functions only, and @path
attribute doesn't support wildcards when used with registerWebInterface
.
Solution to this would be instead of throwing exception, render the error inside myStuff
. Albeit a poor one, as I think you wanted to use errorDisplay
.
A better solution would be implementing functionality in vibe.d
to pass req
parameter to functions called by errorDisplay
(and fixing a bug? that errorDisplay
can't be used outside a class), or even better, support wildcards in @path
when used with registerWebInterface
.