Пример #1
0
        public async Task <IActionResult> GetPosts()
        {
            var posts = await _repository.GetPosts();

            if (posts == null)
            {
                _logger.LogError(null, "Posts Not Found");
                return(NotFound());
            }

            var result = _mapper.Map <List <PostEntity>, List <PostDTO> >(posts);

            return(Ok(result));
        }
Пример #2
0
        private async Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            var errorCode     = "error";
            var statusCode    = HttpStatusCode.BadRequest;
            var exceptionType = exception.GetType();

            switch (exception)
            {
            case Exception e when exceptionType == typeof(UnauthorizedAccessException):
                statusCode = HttpStatusCode.Unauthorized;
                break;

            case Exception e when exceptionType == typeof(ArgumentException):
                statusCode = HttpStatusCode.BadRequest;
                break;

            case Exception e when exceptionType == typeof(InvalidOperationException):
                statusCode = HttpStatusCode.NotFound;
                break;

            case Exception e when exceptionType == typeof(Exception):
                statusCode = HttpStatusCode.InternalServerError;
                break;
            }

            //ResponseDto<NullClassDto> response = new ResponseDto<NullClassDto>()
            //{
            //    Success = false,
            //    Error = new ErrorDto()
            //    {
            //        Code= (int)statusCode,
            //        Message = exception.Message
            //    },
            //    Payload = new PayloadDto<NullClassDto>()
            //};

            _loggerManager.LogError(exception.Message, exception);
            var response = new { code = errorCode, message = exception.Message };
            var payload  = JsonSerializer.Serialize(response);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)statusCode;
            await context.Response.WriteAsync(payload);
        }