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); } }
private static void RespondWithException(Response writeResponse, RestRequestException rre) { var body = new StringBuilder("{"); if (rre.RestErrorString != null) { body.AppendFormat("\"error\":\"{0}\",", rre.RestErrorString); } if (rre.RestErrorDescription != null) { body.AppendFormat("\"errordescription\":\"{0}\"", rre.RestErrorDescription); } body.Append("}"); writeResponse.SimpleResponse(rre.HttpStatusCode, "application/json", body.ToString()); }
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.Invoke(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); } }
private static void CallSafely(Request request, string payload, Response writeResponse, Func <Request, JSONValue, JSONValue> method) { try { JSONValue arg = null; if (payload.Trim().Length == 0) { arg = default(JSONValue); } else { try { arg = new JSONParser(request.Payload).Parse(); } catch (JSONParseException) { JSONHandler.ThrowInvalidJSONException(); } } writeResponse.SimpleResponse(HttpStatusCode.Ok, "application/json", method(request, arg).ToString()); } catch (JSONTypeException) { JSONHandler.ThrowInvalidJSONException(); } catch (KeyNotFoundException) { JSONHandler.RespondWithException(writeResponse, new RestRequestException { HttpStatusCode = HttpStatusCode.BadRequest }); } catch (RestRequestException rre) { JSONHandler.RespondWithException(writeResponse, rre); } catch (Exception arg2) { JSONHandler.RespondWithException(writeResponse, new RestRequestException { HttpStatusCode = HttpStatusCode.InternalServerError, RestErrorString = "InternalServerError", RestErrorDescription = "Caught exception while fulfilling request: " + arg2 }); } }
private static void RespondWithException(Response writeResponse, RestRequestException rre) { StringBuilder builder = new StringBuilder("{"); if (rre.RestErrorString != null) { builder.AppendFormat("\"error\":\"{0}\",", rre.RestErrorString); } if (rre.RestErrorDescription != null) { builder.AppendFormat("\"errordescription\":\"{0}\"", rre.RestErrorDescription); } builder.Append("}"); writeResponse.SimpleResponse(rre.HttpStatusCode, builder.ToString()); }