Object getJsonResponse(String key, ResponseItem responseItem, RpcRequestItem requestItem) { var result = new Dictionary<string, object>(); if (key != null) { result.Add("id", key); } if (responseItem.getErrorCode() < 200 || responseItem.getErrorCode() >= 400) { result.Add("error", getErrorJson(responseItem)); } else { Object response = responseItem.getResponse(); if (response != null && response is DataCollection) { result.Add("data", ((DataCollection)response).entry); } else if (response != null && (response is IRestfulCollection)) { var map = new Dictionary<string, object>(); var collection = (IRestfulCollection)response; map.Add("startIndex", collection.startIndex); map.Add("totalResults", collection.totalResults); if (requestItem != null && requestItem.getCount() != null) { map.Add("itemsPerPage", requestItem.getCount()); } map.Add("list", collection.getEntry()); result.Add("data", map); } else { result.Add("data", response); } } return result; }
private Object getErrorJson(ResponseItem responseItem) { Dictionary<String, Object> error = new Dictionary<String, Object>(); error.Add("code", responseItem.getErrorCode()); String message = errorTitles[responseItem.getErrorCode()]; if (message == null) { message = responseItem.getErrorMessage(); } else { if (!string.IsNullOrEmpty(responseItem.getErrorMessage())) { message += ": " + responseItem.getErrorMessage(); } } if (!string.IsNullOrEmpty(message)) { error.Add("message", message); } if (responseItem.getResponse() != null) { error.Add("data", responseItem.getResponse()); } return error; }
protected override void sendError(HttpResponse servletResponse, ResponseItem responseItem) { int errorCode = responseItem.getErrorCode(); if (errorCode < 0) { // Map JSON-RPC error codes into HTTP error codes as best we can // TODO: Augment the error message (if missing) with a default switch (errorCode) { case -32700: case -32602: case -32600: // Parse error, invalid params, and invalid request errorCode = (int)HttpStatusCode.BadRequest; break; case -32601: // Procedure doesn't exist errorCode = (int)HttpStatusCode.NotImplemented; break; case -32603: default: // Internal server error, or any application-defined error errorCode = (int)HttpStatusCode.InternalServerError; break; } } servletResponse.ContentType = ContentTypes.OUTPUT_TEXT_CONTENT_TYPE; servletResponse.StatusCode = errorCode; servletResponse.StatusDescription = responseItem.getErrorMessage(); servletResponse.Output.Write(responseItem.getErrorMessage()); }