コード例 #1
0
        public override void OnException(HttpActionExecutedContext context)
        {
            HttpStatusCode status = HttpStatusCode.InternalServerError;

            var exType = context.Exception.GetType();

            if (exType == typeof(UnauthorizedAccessException))
            {
                status = HttpStatusCode.Unauthorized;
            }
            else if (exType == typeof(ArgumentException))
            {
                status = HttpStatusCode.NotFound;
            }

            var apiError = new ApiMessageError()
            {
                message = context.Exception.Message
            };

            // create a new response and attach our ApiError object
            // which now gets returned on ANY exception result
            var errorResponse = context.Request.CreateResponse <ApiMessageError>(status, apiError);

            //var errorResponse = context.Request.CreateResponse(HttpStatusCode.BadRequest,
            //                                context.Exception.GetBaseException().Message);
            context.Response = errorResponse;

            base.OnException(context);
        }
コード例 #2
0
        public override void OnException(HttpActionExecutedContext context)
        {
            HttpStatusCode status = HttpStatusCode.InternalServerError;

            var exType = context.Exception.GetType();

            if (exType == typeof(UnauthorizedAccessException))
                status = HttpStatusCode.Unauthorized;
            else if (exType == typeof(ArgumentException))
                status = HttpStatusCode.NotFound;

            var apiError = new ApiMessageError() { message = context.Exception.Message };

            // create a new response and attach our ApiError object
            // which now gets returned on ANY exception result
            var errorResponse = context.Request.CreateResponse<ApiMessageError>(status, apiError);
            //var errorResponse = context.Request.CreateResponse(HttpStatusCode.BadRequest,
            //                                context.Exception.GetBaseException().Message);
            context.Response = errorResponse;

            base.OnException(context);
        }