コード例 #1
0
            public Task Invoke(HttpContext context)
            {
                if (!context.Request.Path.StartsWithSegments(new PathString("/api/v1")))
                {
                    return(_next(context));
                }

                ResponseBase responseObj = null;

                try
                {
                    var t = _next(context);
                    return(t);
                }
                catch (StatusCodeException scException)
                {
                    responseObj = ResponseBase.BadResponse(scException.StatusCode);
                }
                catch (MultiStatusCodeException mscException)
                {
                    responseObj = ResponseBase.BadResponse(mscException.Codes);
                }
                catch (ArgumentException)
                {
                    responseObj = ResponseBase.BadResponse(StatusCode.ArgumentsIncorrect);
                }
                catch (NotImplementedException)
                {
                    responseObj = ResponseBase.BadResponse(StatusCode.NotImplementFunction);
                }
                catch
                {
                    responseObj = ResponseBase.BadResponse(StatusCode.UnknownError);
                }
                var toWrite = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(responseObj));

                context.Response.ContentType = "application/json; charset=utf-8";
                context.Response.Body.Write(toWrite, 0, toWrite.Length);
                return(Task.CompletedTask);
            }
コード例 #2
0
        public void OnException(ExceptionContext context)
        {
            _logger.LogWarning(new EventId(eventId++), context.Exception,
                               JsonConvert.SerializeObject(new
            {
                Action = context.ActionDescriptor.DisplayName
            }, Formatting.Indented));

            ResponseBase responseObj = null;

            switch (context.Exception)
            {
            case StatusCodeException scException:
                responseObj = ResponseBase.BadResponse(scException.StatusCode);
                break;

            case MultiStatusCodeException mscException:
                responseObj = ResponseBase.BadResponse(mscException.Codes);
                break;

            case ArgumentException argException:
                responseObj = ResponseBase.BadResponse(StatusCode.ArgumentsIncorrect);
                break;

            case NotImplementedException niException:
                responseObj = ResponseBase.BadResponse(StatusCode.NotImplementFunction);
                break;

            default:
                responseObj = ResponseBase.BadResponse(StatusCode.UnknownError);
                break;
            }
            var toWrite = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(responseObj));

            context.HttpContext.Response.ContentType = "application/json; charset=utf-8";
            context.HttpContext.Response.Body.Write(toWrite, 0, toWrite.Length);
            context.ExceptionHandled = true;
        }