private static async Task HandleExceptionAsync(HttpContext context, Exception exception, ILogEngine logEngine)
        {
            var code  = HttpStatusCode.InternalServerError;
            var error = exception.Message;

            if (exception is ResourceExistedException)
            {
                code = HttpStatusCode.Conflict;
            }
            else if (exception is PaymentRequiredException)
            {
                code = HttpStatusCode.PaymentRequired;
            }
            else if (exception is UnauthorizedException)
            {
                code = HttpStatusCode.Unauthorized;
            }
            else if (exception is NotAllowedException)
            {
                code = HttpStatusCode.Forbidden;
            }
            else if (exception is NotFoundException)
            {
                code = HttpStatusCode.NotFound;
            }
            else if (exception is InvalidParameterException)
            {
                code = HttpStatusCode.BadRequest;
            }
            else if (exception is DbUpdateConcurrencyException)
            {
                code = HttpStatusCode.RequestTimeout; error = "Db update concurrent conflict. Please retry";
            }
            else
            {
                logEngine.LogException(exception);
                error = "Unexpected error";
            }

            var result = JsonConvert.SerializeObject(new { error = error });

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)code;
            await context.Response.WriteAsync(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Записывает в журнал сообщение об исключении
        /// </summary>
        /// <param name="type">Тип журнала</param>
        /// <param name="level">Уровень сообщения</param>
        /// <param name="exception">Исключение</param>
        /// <param name="message">Сообщение</param>
        /// <param name="parameters">Параметры формирования сообщения</param>
        private static void LogException(LogType type, LogLevel level, Exception exception, string message, params object[] parameters)
        {
            try
            {
                ILogEngine logger = GetLogger(type);
                if (logger == null)
                {
                    return;
                }

                logger.LogException(level, exception, message, parameters);
            }
            catch (Exception le)
            {
                if (type != LogType.System)
                {
                    Logger.LogException(LogType.System, LogLevel.Error, le, "Ошибка при записи в журнал {0}", type);
                }

                throw;
            }
        }