public async Task WriteErrorResponseAsync(
            HttpContext context,
            HttpStatusCode code,
            IDictionary <string, object> inputErrors,
            CustomErrorCode customErrorCode,
            string customMessageOverride)
        {
            var response = context.Response;

            response.ContentType = "application/json; charset=utf8";
            response.StatusCode  = (int)code;

            await response
            .WriteAsync(JsonConvert.SerializeObject(new
            {
                error = new ErrorResponse
                {
                    Message      = string.IsNullOrWhiteSpace(customMessageOverride) ? customErrorCode.GetEnumDescription() : customMessageOverride,
                    LogstashLink = _env.IsDevelopment() ? "Логи не доступны для среды разработки" : GetLogstashLink(context),
                    InputErrors  = inputErrors,
                    Code         = customErrorCode,
                },
            }))
            .ConfigureAwait(false);
        }
Beispiel #2
0
        public static void AssertCustomException(Action func, CustomErrorCode code)
        {
            try
            {
                func.Invoke();
            }
            catch (CustomException e)
            {
                var exceptionThrown = e.ErrorCode != null && (CustomErrorCode)e.ErrorCode == code;

                if (!exceptionThrown)
                {
                    throw new AssertFailedException(
                              $"An exception of type {typeof(CustomException)} was expected, but not thrown, when {code}");
                }
            }
        }
        /// <summary>
        /// Обрабатываем exception
        /// Если Request к /api, то возвращаем JSON
        /// Если Request к MVC, то редиректим на страницу, указанную в appsettings
        /// </summary>
        private async Task HandleErrorResponse(
            HttpContext context, HttpStatusCode code,
            IDictionary <string, object> inputErrors, CustomErrorCode customErrorCode,
            string customMessageOverride)
        {
            if (context.IsApiRequest() || context.Request.IsAjaxRequest())
            {
                await _responseService.WriteErrorResponseAsync(context, code, inputErrors, customErrorCode, customMessageOverride);
            }
            else
            {
                var redirectPath = context.Request.PathBase +
                                   _options.RedirectRelativePath +
                                   context.Request.QueryString;

                context.Response.Redirect(redirectPath);
            }
        }
Beispiel #4
0
 public ConflictWithCurrentStateException(CustomErrorCode customErrorCode)
     : base(customErrorCode)
 {
 }
Beispiel #5
0
 public ConflictWithCurrentStateException(string message, CustomErrorCode customErrorCode)
     : base(message, customErrorCode)
 {
 }
Beispiel #6
0
 public IEModel(string msg, CustomErrorCode status, T payload)
 {
     this.CustomMessage   = msg;
     this.ErrorStatusCode = status;
     this.Payload         = payload;
 }
Beispiel #7
0
 public IEModel(string msg, CustomErrorCode status)
 {
     this.CustomMessage   = msg;
     this.ErrorStatusCode = status;
     this.Payload         = default;
 }
 public NotFoundException(CustomErrorCode customErrorCode
                          ) : base(customErrorCode)
 {
 }
 public NotFoundException(string message, CustomErrorCode customErrorCode
                          ) : base(message, customErrorCode)
 {
 }
 public BadUserInputException(CustomErrorCode customErrorCode) : base(customErrorCode.GetEnumDescription())
 {
     CustomErrorCode = customErrorCode;
 }
 public BadUserInputException(string message, CustomErrorCode customErrorCode) : base(message)
 {
     CustomErrorCode = customErrorCode;
 }