public static void ThrowIfNotNull(this ErrorEnvelope e) { if (e != null) { var ex = ParseError(e); throw ex; } }
private async Task RespondWithMethodResult(HttpResponse response, ITaskResult taskResult, ISerializer serializer, bool useEnvelope, bool compress) { if (taskResult.IsCanceled) { response.StatusCode = DasyncHttpCodes.Canceled; response.Headers.Add(DasyncHttpHeaders.TaskResult, "canceled"); } else if (taskResult.IsFaulted()) { response.StatusCode = DasyncHttpCodes.Faulted; response.Headers.Add(DasyncHttpHeaders.TaskResult, "faulted"); } else { response.StatusCode = DasyncHttpCodes.Succeeded; response.Headers.Add(DasyncHttpHeaders.TaskResult, "succeeded"); } var responseStream = response.Body; if (compress) { response.Headers.Add("Content-Encoding", "gzip"); responseStream = new GZipStream(responseStream, CompressionLevel.Optimal, leaveOpen: true); } response.ContentType = "application/" + serializer.Format; if (useEnvelope) { response.Headers.Add(DasyncHttpHeaders.Envelope, "result"); await SerializeAsync(serializer, responseStream, taskResult); } else { if (taskResult.IsFaulted()) { // TODO: allow custom transformer var errorEnvelope = new ErrorEnvelope { Error = taskResult.Exception.ToError() }; await SerializeAsync(serializer, responseStream, errorEnvelope); } else if (taskResult.IsSucceeded()) { if (taskResult.Value != null) { await SerializeAsync(serializer, responseStream, taskResult.Value); } } } if (compress) { responseStream.Dispose(); } }
public ActionResult VZDetail(string _id, FormCollection form) { string id = _id; var content = ReadRequestBody(this.Request); //return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { debug=content, error = "Not implemented yet. Come back tomorrow." }), "application/json"); if (string.IsNullOrEmpty(id)) { return(new HttpStatusCodeResult(404)); } if (!System.Diagnostics.Debugger.IsAttached && !(Framework.ApiAuth.IsApiAuth(this, parameters: new Framework.ApiCall.CallParameter[] { new Framework.ApiCall.CallParameter("id", id) }).Authentificated) ) { return(new HttpStatusCodeResult(401)); } else { var authId = Framework.ApiAuth.IsApiAuth(this); var idxConn = HlidacStatu.Lib.ES.Manager.GetESClient_VerejneZakazkyNaProfiluConverted(); HlidacStatu.Lib.Data.VZ.VerejnaZakazka vz = null; try { vz = Newtonsoft.Json.JsonConvert.DeserializeObject <Lib.Data.VZ.VerejnaZakazka>(content); if (vz == null) { ErrorEnvelope ee = new ErrorEnvelope() { Data = content, Error = "null data", UserId = authId.ApiCall.User, apiCallJson = Newtonsoft.Json.JsonConvert.SerializeObject(authId) ?? null }; ee.Save(idxConn); return(Content(Newtonsoft.Json.JsonConvert.SerializeObject( new { error = "data is empty" } ), "application/json")); } vz.Save(idxConn); return(Content(Newtonsoft.Json.JsonConvert.SerializeObject( new { result = "ok" } ), "application/json")); } catch (Exception e) { ErrorEnvelope ee = new ErrorEnvelope() { Data = content, Error = e.ToString(), UserId = authId.ApiCall.User, apiCallJson = Newtonsoft.Json.JsonConvert.SerializeObject(authId) ?? null }; ee.Save(idxConn); return(Content(Newtonsoft.Json.JsonConvert.SerializeObject( new { error = "deserialization error", descr = e.ToString() } ), "application/json")); } } }
private async Task RespondWithRoutineResult(HttpContext context, TaskResult taskResult, bool isDasyncJsonRequest) { if (taskResult.IsCanceled) { context.Response.StatusCode = DasyncHttpCodes.Canceled; if (isDasyncJsonRequest) { context.Response.ContentType = "application/dasync+json"; _dasyncJsonSerializer.Serialize(context.Response.Body, taskResult); } return; } else if (taskResult.IsFaulted) { context.Response.StatusCode = DasyncHttpCodes.Faulted; if (isDasyncJsonRequest) { context.Response.ContentType = "application/dasync+json"; _dasyncJsonSerializer.Serialize(context.Response.Body, taskResult); } else { context.Response.ContentType = "application/json"; var errorEnvelope = new ErrorEnvelope { Error = ExceptionToErrorConverter.Convert(taskResult.Exception) }; await context.Response.Body.WriteAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(errorEnvelope, JsonSettings))); } return; } else { context.Response.StatusCode = DasyncHttpCodes.Succeeded; if (isDasyncJsonRequest) { context.Response.ContentType = "application/dasync+json"; _dasyncJsonSerializer.Serialize(context.Response.Body, taskResult); } else { context.Response.ContentType = "application/json"; await context.Response.Body.WriteAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(taskResult.Value, JsonSettings))); } return; } }
private static Exception ParseError(ErrorEnvelope e) { if (e.ErrorType == "Relativity.Services.Objects.Exceptions.EventHandlerFailedException") { return(new EventHandlerFailedException(e.Message)); } else if (e.ErrorType == "Relativity.Services.Exceptions.ValidationException") { return(new ValidationException(e.Message)); } else if (e.ErrorType == "ReasonPhrase") { return(new ReasonPhraseException(e.Message)); } var str = Newtonsoft.Json.JsonConvert.SerializeObject(e); return(new System.Exception(str)); }
public static async Task <ErrorEnvelope> EnsureSuccessAsync(this HttpResponseMessage message) { if (!message.IsSuccessStatusCode) { var text = await message.Content.ReadAsStringAsync(); var error = Newtonsoft.Json.JsonConvert.DeserializeObject <ErrorEnvelope>(text); if (error == null && string.IsNullOrEmpty(message.ReasonPhrase)) { error = new ErrorEnvelope { ErrorType = "ReasonPhrase", Message = message.ReasonPhrase }; } return(error); } return(null); }
public async Task Get_GetById_ReturnsNotFound() { // ARRANGE string customerId = CommonFunctions.GenerateId(); _customerReadService.Stub(x => x.GetByIdAsync(customerId)).Return(Task.FromResult(null as CustomerDetailDto)); _subject.MockRequest(HttpMethod.Get, new HttpRouteValueDictionary { { "controller", "Customer" } }); // ACT HttpResponseMessage result = await _subject.Get(customerId); // ASSERT Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound)); ErrorEnvelope envelope = await result.Content.ReadAsAsync <ErrorEnvelope>(); Assert.That(envelope.Error.Code, Is.EqualTo(ErrorResponseConstants.ERROR_CUSTOMER_NOT_FOUND)); Assert.That(envelope.Error.Message, Is.EqualTo(ErrorResponseConstants.ERROR_CUSTOMER_NOT_FOUND_DESCRIPTION)); _customerReadService.AssertWasCalled(x => x.GetByIdAsync(customerId)); }