Exemple #1
0
        /// <summary>
        /// Handle Exception
        /// </summary>
        /// <param name="context"></param>
        public void OnException(ExceptionContext context)
        {
            var            data       = new Dictionary <string, string>();
            HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
            var            message    = String.Empty;

            if (context == null)
            {
                return;
            }

            var ex = context.Exception;

            TypeSwitch.Do(ex,
                          TypeSwitch.Case <ArgumentException>(() => { statusCode = HttpStatusCode.BadRequest; }),
                          TypeSwitch.Case <ArgumentNullException>(() => { statusCode = HttpStatusCode.BadRequest; }),
                          TypeSwitch.Case <ArgumentOutOfRangeException>(() => { statusCode = HttpStatusCode.BadRequest; }),
                          TypeSwitch.Case <KeyNotFoundException>(() => { statusCode = HttpStatusCode.NotFound; }),
                          TypeSwitch.Case <StackExchange.Redis.RedisConnectionException>(() => { statusCode = HttpStatusCode.InternalServerError; })
                          );

            HttpResponse response = context.HttpContext.Response;

            response.StatusCode  = (int)statusCode;
            response.ContentType = "application/json";

            var err = new Models.ErrorPayload()
            {
                StackTrace = ex.StackTrace,
                Message    = ex.Message,
                StatusCode = (int)statusCode
            };

            foreach (var d in data)
            {
                err.Data.Add(d.Key, d.Value);
            }

            var json = JsonConvert.SerializeObject(err);

            this._logger.LogError(ex, json);

            response.WriteAsync(json);
        }
        public void TestSerialize_ErrorPayload()
        {
            var d = new Dictionary <string, string>()
            {
                { "A", "a" },
                { "B", "b" }
            };

            var ep = new Models.ErrorPayload()
            {
                Message    = "message",
                StackTrace = "trace",
                StatusCode = 418,
            };

            foreach (var e in d)
            {
                ep.Data.Add(e.Key, e.Value);
            }

            TestJsonSerializationHelper.AssertJsonSerialization <Models.ErrorPayload>(TestModels.Context, ep);
        }
Exemple #3
0
        /// <summary>
        /// Handle Exception
        /// </summary>
        /// <param name="context">ExceptionContext</param>
        public void OnException(ExceptionContext context)
        {
            if (context == null)
            {
                return;
            }

            var data       = new Dictionary <string, string>();
            var statusCode = HttpStatusCode.InternalServerError;
            var message    = string.Empty;

            var ex = context.Exception;

            TypeSwitch.Do(
                ex,
                TypeSwitch.Case <ArgumentException>(() => { statusCode = HttpStatusCode.BadRequest; }),
                TypeSwitch.Case <ArgumentNullException>(() => { statusCode = HttpStatusCode.BadRequest; }),
                TypeSwitch.Case <ArgumentOutOfRangeException>(() => { statusCode = HttpStatusCode.BadRequest; }),
                TypeSwitch.Case <KeyNotFoundException>(() => { statusCode = HttpStatusCode.NotFound; }));

            var err = new Models.ErrorPayload()
            {
                Data       = data,
                StackTrace = ex.StackTrace,
                Message    = ex.Message,
                StatusCode = (int)statusCode,
            };

            this._logger.LogError(ex, err.ToString());

            var response = context.HttpContext.Response;

            response.StatusCode  = (int)statusCode;
            response.ContentType = "application/json";
            response.WriteAsync(JsonConvert.SerializeObject(err));
        }