RejectedSoftware Forums

Sign up

Returning JSON error for REST API

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?

Re: Returning JSON error for REST API

On Mon, 11 Apr 2016 06:54:28 GMT, Jesper Tholstrup wrote:

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?

The current error handling scheme relies on exceptions. You'd usually throw a HTTPStatusException with the appropriate status code and error message. But at some point, this will have to get extended to enable custom formatted JSON in the response, as well as to enable error returns without exceptions (mostly for performance reasons).

Re: Returning JSON error for REST API

BTW, the JSON error that will be written in case of an Exception looks like this:

{
    "statusMessage": "..."
}

Where the string is sourced from e.msg.

Re: Returning JSON error for REST API

On Wed, 13 Apr 2016 08:59:55 GMT, Sönke Ludwig wrote:

BTW, the JSON error that will be written in case of an Exception looks like this:

{
    "statusMessage": "..."
}

Where the string is sourced from e.msg.

Hi Sönke,

Thanks for taking the time - I will move ahead with the default response.

vibe.d is actually a pretty cool framework.