Пример #1
0
        /// <summary>
        /// Fluxo que controla as exceptions geradas na aplicação
        /// </summary>
        /// <param name="context"></param>
        /// <param name="exception"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private Task HandleExceptionAsync(HttpContext context, Exception exception, ApiExceptionOptions options)
        {
            var statusCode = GetHttpStatusCodeByException(exception);

            var problemDetail = new ProblemDetail()
            {
                Id       = Guid.NewGuid().ToString(),
                HttpCode = statusCode,
                Title    = "Erro ocorrido na Api"
            };

            options.AddResponseDetails?.Invoke(context, exception, problemDetail);

            var innerException = GetInnerExceptionMessage(exception);

            var level = options.DetermineLogLevel?.Invoke(exception) ?? LogLevel.Error;

            _logger.Log(level, exception, $"BAD ERROR!! {innerException} -- {problemDetail.Id}");

            var result = JsonSerializer.Serialize(problemDetail);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = statusCode;
            return(context.Response.WriteAsync(result));
        }
Пример #2
0
        public async Task <ActionResult <ProblemDetail> > PostProblemDetail(ProblemDetail problemDetail)
        {
            _context.ProblemDetails.Add(problemDetail);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetProblemDetail", new { id = problemDetail.TokenId }, problemDetail));
        }
Пример #3
0
        public async Task <IActionResult> PutProblemDetail(int id, ProblemDetail problemDetail)
        {
            if (id != problemDetail.TokenId)
            {
                return(BadRequest());
            }

            _context.Entry(problemDetail).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProblemDetailExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #4
0
 public static void UpdateApiErrorResponse(HttpContext context, Exception exception, ProblemDetail problemDetail)
 {
     if (exception.GetType().Name == typeof(SqlException).Name)
     {
         problemDetail.Detalhe = "Exception de Banco de dados";
     }
     if (exception.GetType().Name == typeof(Exception).Name)
     {
         problemDetail.Detalhe = "Exception de fluxo";
     }
 }