Ejemplo n.º 1
0
 public override void OnRequestError(IWebApiRequest request, WebApiError <Error> error)
 {
     #if UNITY_EDITOR
     if (error.Error != null)
     {
         Game.Logger.LogError("[WebAPI] Error: " + request.Uri, error.Error.Code + ": " + error.Error.Name, error.Error);
     }
     else
     {
         Game.Logger.LogError("[WebAPI] Error: " + request.Uri, error.RawErrorMessage);
     }
     #endif
 }
        protected virtual void OnException([NotNull] Exception e, [NotNull] ActionExecutedContext context)
        {
            var result = new WebApiErrorModel();

            var error = new WebApiError();

            if (e is UtManagerException)
            {
                error.Code = (int)((UtManagerException)e).Code;
            }
            error.Message = e.Message;

            result.Errors.Add(error);

            var actionResult = new ObjectResult(result);

            actionResult.StatusCode = (int)HttpStatusCode.InternalServerError;

            context.Result           = actionResult;
            context.ExceptionHandled = true;
        }
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            base.OnException(actionExecutedContext);
            Exception exception = actionExecutedContext.Exception;

            // Convert the exception into a WebApi error
            WebApiError error = new WebApiError(exception);

            // Log the exception
            this.log.Error(exception.Message, exception);

            // Remove the stack trace information from the error as the client application should not see this.
            error.StackTrace = null;

            WebApiServiceException webApiServiceException = exception as WebApiServiceException;

            if (webApiServiceException != null)
            {
                // Is this a validation exception?
                WebApiServiceValidationException webApiServiceValidationException = webApiServiceException as WebApiServiceValidationException;
                ModelStateDictionary             modelState = webApiServiceValidationException != null ? webApiServiceValidationException.ModelState : null;

                // Return the WebApi error in the service response
                error.Message          = exception.Message;
                error.ErrorCode        = webApiServiceException.ErrorCode;
                error.ValidationErrors = MapValidationErrors(modelState).ToList();

                // Return the specified status code
                actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(webApiServiceException.HttpStatusCode, error);
            }
            else
            {
                // Unknown exception, so return an internal server error
                error.ErrorCode = WebApiErrorCode.UnknownError;
                actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.InternalServerError, error);
            }
        }