private static Task HandleExceptionAsync(HttpContext context, Exception ex, ILogger logger) { // throw ex; var code = ex switch { NotFoundException _ => HttpStatusCode.NotFound, InvalidCredentialException _ => HttpStatusCode.Forbidden, AuthenticationException _ => HttpStatusCode.Unauthorized, InvalidStateException _ => HttpStatusCode.BadRequest, IllegalArgumentException _ => HttpStatusCode.BadRequest, _ => HttpStatusCode.InternalServerError }; var logErrMsg = $"{ex.Message} - {context.Request.Path}{context.Request.QueryString}"; if (code == HttpStatusCode.InternalServerError) { logger.LogError(logErrMsg); } else { logger.LogInformation(logErrMsg); } var result = JsonConvert.SerializeObject(new { error = ex.Message }); context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)code; return(context.Response.WriteAsync(result)); } }
public void ShouldHaveDefaultConstructorThatUsesSimpleMessageProducerAndAssumesThatStringIsNotFormatted() { new CNPJValidator().AssertValid(validStringNotFormatted); try { new CNPJValidator().AssertValid(firstCheckDigitWrongNotFormatted); Assert.Fail("Test expected to throw exception"); } catch (InvalidStateException e) { InvalidStateException invalidStateException = e; AssertMessage(invalidStateException, INVALID_CHECK_DIGITS); } }
private void VerifyState(Status status, State expectedState) { if (status.State != expectedState) { if (status.State == State.Error) { var e = new InvalidStateException(expectedState, status.State, GetErrorString(status)); DeviceError(this, new ErrorEventArgs(e)); throw e; } else { throw new InvalidStateException(expectedState, status.State); } } }
public void InvalidStateException() { TestsContext.AssumeSystemTypeIsSerializable(); var state = new[] { new InvalidValue("blah", typeof(ExceptionSerializationFixture), "prop", null, null, null) }; var ex = new InvalidStateException(state); var dex = default(InvalidStateException); Assert.That( () => { dex = (InvalidStateException)SerializationHelper.Deserialize(SerializationHelper.Serialize(ex)); }, Throws.Nothing); Assert.That(dex, Is.Not.Null); Assert.That(dex.InvalidValues, Has.Length.EqualTo(1)); Assert.That( dex.InvalidValues[0], Is.Not.Null.And.Property(nameof(InvalidValue.Message)).EqualTo("blah")); Assert.That(dex.InvalidValues[0].EntityType, Is.EqualTo(typeof(ExceptionSerializationFixture))); Assert.That(dex.InvalidValues[0].PropertyName, Is.EqualTo("prop")); }
private void AssertMessage(InvalidStateException invalidStateException, string expected) { Assert.IsTrue(invalidStateException.GetInvalidMessages()[0].GetMessage().Contains(expected)); }
public static void InjectExceptionMessages(this ServiceOperationResult serviceOperationResult, InvalidStateException invalidStateException) { InjectInvalidValues(serviceOperationResult, invalidStateException.GetInvalidValues()); }
public static Exception ConvertToBrokenRulesException(this InvalidStateException invalidStateProblems) { BrokenRules brokenRules = invalidStateProblems.GetInvalidValues().ToBrokenRules(); return new BrokenRulesException("Problem writing changes to database. See BrokenRules", brokenRules); }
private void AssertMessage(InvalidStateException invalidStateException , String expected) { Assert.IsTrue(invalidStateException .GetErrors().Contains(expected)); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(InvalidStateException obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }