private static void CallSafely(Request request, string payload, Response writeResponse, Func <Request, JSONValue, JSONValue> method)
{
RestRequestException exception;
try
{
JSONValue value2 = 0;
if (payload.Trim().Length == 0)
{
value2 = new JSONValue();
}
else
{
try
{
value2 = new JSONParser(request.Payload).Parse();
}
catch (JSONParseException)
{
ThrowInvalidJSONException();
}
}
writeResponse.SimpleResponse(HttpStatusCode.Ok, method(request, value2).ToString());
}
catch (JSONTypeException)
{
ThrowInvalidJSONException();
}
catch (KeyNotFoundException)
{
exception = new RestRequestException {
HttpStatusCode = HttpStatusCode.BadRequest
};
RespondWithException(writeResponse, exception);
}
catch (RestRequestException exception2)
{
RespondWithException(writeResponse, exception2);
}
catch (Exception exception3)
{
exception = new RestRequestException {
HttpStatusCode = HttpStatusCode.InternalServerError,
RestErrorString = "InternalServerError",
RestErrorDescription = "Caught exception while fulfilling request: " + exception3
};
RespondWithException(writeResponse, exception);
}
}