I'm quite new to vibe.d and I am somewhat stuck in the error handling machinery of vibe.d. I hope that you will be able to push me along in the right direction. Any help would be appreciated.
Currently, I am working on a REST API which returns a range of different JSON formatted structs. This is pretty much identical to the example on http://vibed.org/docs:
import vibe.core.core;
import vibe.core.log;
import vibe.http.router;
import vibe.http.server;
import vibe.web.rest;
struct Weather {
string text;
double temperature; // °C
}
interface MyAPI {
// GET /weather -> responds {"text": "...", "temperature": ...}
Weather getWeather();
// PUT /location -> accepts {"location": "..."}
@property void location(string location);
// GET /location -> responds "..."
@property string location();
}
class MyAPIImplementation : MyAPI {
private {
string m_location;
}
Weather getWeather() { return Weather("sunny", 25); }
@property void location(string location) { m_location = location; }
@property string location() { return m_location; }
}
shared static this()
{
auto router = new URLRouter;
router.registerRestInterface(new MyAPIImplementation);
auto settings = new HTTPServerSettings;
settings.port = 8080;
listenHTTP(settings, router);
// create a client to talk to the API implementation over the REST interface
runTask({
auto client = new RestInterfaceClient!MyAPI("http://127.0.0.1:8080/");
auto weather = client.getWeather();
logInfo("Weather: %s, %s °C", weather.text, weather.temperature);
client.location = "Paris";
logInfo("Location: %s", client.location);
});
}
My problem is that the endpoints, e.g. getWeather()
may need to communicate that an error has occurred.
Assuming that Weather getWeather()
gets a properly formatted GET request, how do I return a JSON formatted error struct in stead of a Weather struct if some internal check in getWeather()
fails?