public IFilteredSpecificError <TSpecificError, TError, TSuccess> WhenError <TSpecificError>() where TSpecificError : TError => ErrorContent .Select <TError, IFilteredSpecificError <TSpecificError, TError, TSuccess> >( item => new ResolvedFilteredSpecificError <TSpecificError, TError, TSuccess>(item)) .DefaultIfEmpty( new UnresolvedFilteredSpecificError <TSpecificError, TError, TSuccess>( SuccessContent.SingleOrDefault())) .Single();
public async Task DeserializeResponseAsync_WrongUrl() { var client = new HttpClient(); var response = await client.GetAsync("https://services.odata.org/TripPinRESTierService/Robert"); var(Result, ErrorContent) = await response.DeserializeResponseAsync <dynamic>(); ErrorContent.Should().NotBeNullOrEmpty(); //Result.Should().BeNull(); }
public async Task DeserializeResponseAsync_SingleEntity() { var client = new HttpClient(); var response = await client.GetAsync("https://services.odata.org/TripPinRESTierService/People"); var(Result, ErrorContent) = await response.DeserializeResponseAsync <ODataV4List <ExpandoObject> >(); ErrorContent.Should().BeNullOrEmpty(); Result.Should().NotBeNull(); }
public async Task SingleEntity_DeserializesProperly() { var client = new HttpClient(); var response = await client.GetAsync("https://services.odata.org/TripPinRESTierService/People('russellwhyte')"); var(Result, ErrorContent) = await response.DeserializeResponseAsync <ODataV4PrimitiveResult <ExpandoObject> >(); ErrorContent.Should().BeNullOrEmpty(); Result.Should().NotBeNull(); Result.ODataContext.Should().NotBeNullOrWhiteSpace(); }
/// <summary> /// Initializes a new instance of the <see cref="ApiResponse"/> class. /// </summary> /// <param name="content">The content.</param> /// <param name="error">The error.</param> /// <exception cref="System.ArgumentNullException">error;The argument error cannot be null.</exception> public ApiResponse(ErrorContent error) { if (error == null) { throw new ArgumentNullException("error", "The argument error cannot be null."); } Errors = new List <ErrorContent>() { error }.ToArray(); }
public async Task ListWithCount_DeserializesProperly() { var client = new HttpClient(); var response = await client.GetAsync("https://services.odata.org/TripPinRESTierService/People?$count=true"); var(Result, ErrorContent) = await response.DeserializeResponseAsync <ODataV4List <ExpandoObject> >(); ErrorContent.Should().BeNullOrEmpty(); Result.Should().NotBeNull(); Result.ODataCount.Should().NotBe(0); Result.ODataCount.Should().Be(Result.Items.Count); Result.ODataContext.Should().NotBeNullOrWhiteSpace(); }
public IActionResult Map(Exception ex) { if (ex is TestConflictException) { var errorContent = new ErrorContent { Code = "testcode", Message = ex.Message }; var conflictResult = new ObjectResult(errorContent); conflictResult.StatusCode = StatusCodes.Status409Conflict; return(conflictResult); } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="ApiException"/> class. /// </summary> /// <param name="errorCode">HTTP status code.</param> /// <param name="message">Error message.</param> /// <param name="errorContent">Error content.</param> public ApiException(int errorCode, string message, string errorContent = null) : this(errorCode, message) { if (errorContent != null) { try { ErrorContent = JToken.Parse(errorContent); ServerException = ErrorContent.ToObject <ServerException>(); if (ServerException.Message == null) { ServerException = null; Error = ErrorContent["error"]?.ToString(); } } catch { ErrorContent = message; } } }
/// <summary> /// APIs the response. /// </summary> /// <param name="controller">The controller.</param> /// <param name="content">The content.</param> /// <returns></returns> public virtual ApiResult <ApiResponse> ApiBadRequestResult(ErrorContent error) { return(new ApiResult <ApiResponse>(Request, new ApiResponse(error), null, HttpStatusCode.BadRequest)); }
public IFilteredSuccess <TError, TSuccess> WhenSuccess() => SuccessContent .Select <TSuccess, IFilteredSuccess <TError, TSuccess> >( item => new ResolvedFilteredSuccess <TError, TSuccess>(item)) .DefaultIfEmpty(new UnresolvedFilteredSuccess <TError, TSuccess>(ErrorContent.SingleOrDefault())) .Single();
public ErrorContent(ErrorContent error) { Message = error.Message; ErrorType = error.ErrorType; }
/// <summary> /// Initializes a new instance of the <see cref="ApiException"/> class. /// </summary> /// <param name="errorCode">HTTP status code.</param> /// <param name="message">Error message.</param> /// <param name="errorContent">Error content.</param> public ApiException(HttpStatusCode errorCode, string message, dynamic errorContent = null) : base(message) { this.ErrorCode = errorCode; this.ErrorContent = errorContent; }
public virtual IActionResult Map(Exception ex) { foreach (var hook in _mapperHooks) { var result = hook.Map(ex); if (result != null) { return(result); } } if (ex is IValidationException <object> ) { var typedEx = ex as IValidationException <object>; // null to bypass the typed constructor var errorContent = new ValidationErrorContent <object>(null) { ValidationMessages = typedEx.ValidationMessages }; return(new BadRequestObjectResult(errorContent)); } if (ex is EntityNotFoundException) { var typedEx = ex as EntityNotFoundException; if (string.IsNullOrEmpty(typedEx.Message)) { return(new NotFoundResult()); } var errorContent = new ErrorContent { Code = typedEx.ErrorCode, Message = typedEx.Message }; return(new NotFoundObjectResult(errorContent)); } if (ex is UnauthorizedException) { return(new UnauthorizedResult()); } if (ex is ForbiddenException) { return(new ForbidResult()); } if (ex is ExceptionWithFeedback) { var typedEx = ex as ExceptionWithFeedback; var errorContent = new ErrorContent { Code = typedEx.ErrorCode, Message = typedEx.Message }; return(new InternalServerObjectResult(errorContent)); } _logger.LogTrace($"Exception of type {ex.GetType().Name} was mapped by the catch all mapper."); var message = "Something went wrong. Contact support and give them the identifier found below."; // if development ==> expose exception message if (_hostingEnvironment.IsDevelopment()) { message = ex.Message; } // catch all (just Exception) var catchAllErrorContent = new ErrorContent { Code = "", Message = message }; return(new InternalServerObjectResult(catchAllErrorContent)); }
/// <summary>Returns the hash code for this <see cref="StringValidationResult" />.</summary> /// <returns>The hash code for this <see cref="StringValidationResult" />.</returns> public override int GetHashCode() => IsValid.GetHashCode() * 31 + ErrorContent.GetHashCode();