Пример #1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////
        /*--------------------------------------------------------------------------------------------*/
        protected void OnUnhandledException(IApiResponse pResp, Exception pEx)
        {
            var fr = new FabResponse <FabObject>();

            fr.Error   = FabError.ForInternalServerError();
            fr.TotalMs = pResp.GetTimerMilliseconds();

            pResp.SetJsonWith(fr);
            pResp.Status    = HttpStatusCode.InternalServerError;
            pResp.Unhandled = pEx;

            Log.Fatal("Unhandled exception: " + pEx.Message, pEx);
        }
Пример #2
0
        /*--------------------------------------------------------------------------------------------*/
        private Response HandleError(NancyContext pContext, Exception pException)
        {
            Log.Fatal("Error at " + pContext.Request.Path, pException);

            var fr = new FabResponse <FabObject>();

            fr.Error = FabError.ForInternalServerError();

            var jr = new JsonResponse(fr, new ServiceStackJsonSerializer());

            jr.StatusCode = HttpStatusCode.InternalServerError;
            return(jr);
        }
Пример #3
0
        /*--------------------------------------------------------------------------------------------*/
        public void Handle(HttpStatusCode pCode, NancyContext pContext)
        {
            var err = new FabError();

            err.Code    = (int)FabFault.Code.HttpError;
            err.Name    = FabFault.Code.HttpError + "";
            err.Message = pContext.Response.StatusCode + " (" + (int)pContext.Response.StatusCode + ") " +
                          "for API request: " + pContext.Request.Method + " " + pContext.Request.Path;

            var fr = new FabResponse <FabObject>();

            fr.Error = err;

            var jr = new JsonResponse(fr, new ServiceStackJsonSerializer());

            jr.StatusCode     = pCode;
            pContext.Response = jr;
        }
Пример #4
0
        /*--------------------------------------------------------------------------------------------*/
        public override IApiResponse Execute()
        {
            var resp = new ApiResponse();

            resp.Status = HttpStatusCode.OK;
            resp.StartTimer();

            try {
                ApiReq.OpCtx.Auth.ExecuteOauth();

                IList <T> list = vGetResponse();
                resp.StopTimer();

                FabResponse <T> fr = NewFabResponse();
                fr.Data    = list;
                fr.TotalMs = resp.GetTimerMilliseconds();

                resp.SetJsonWith(fr);
            }
            catch (Exception e) {
                FabError err = OnException(e);
                resp.StopTimer();

                if (err == null)
                {
                    OnUnhandledException(resp, e);
                }
                else
                {
                    var fr = new FabResponse <FabObject>();
                    fr.TotalMs = resp.GetTimerMilliseconds();
                    fr.Error   = err;
                    resp.SetJsonWith(fr);
                    resp.Status = (err.Code == (int)FabFault.Code.AuthorizationRequired ?
                                   HttpStatusCode.Unauthorized : HttpStatusCode.BadRequest);
                }
            }

            resp.LogResponse(ApiReq);
            return(resp);
        }
Пример #5
0
        public void SendFabricError()
        {
            var fe = new FabError();

            fe.Name    = "Test";
            fe.Message = "Desc";
            fe.Code    = 500;

            var fr = new FabResponse();

            fr.Error = fe;

            var wr = new TestWebResponse(GetStream(fr.ToJson()));
            var we = new WebException("Error", null, WebExceptionStatus.UnknownError, wr);

            vMoqHttpProv
            .Setup(x => x.GetResponse(vHttpReq))
            .Throws(we);

            var req = NewFabricRequest <FabResponse <FabApp> >();

            try {
                req.Send(vContext, SessionType.Default);
                Assert.Fail("WebException expected, but not thrown.");
            }
            catch (FabricErrorException errEx) {
                Assert.NotNull(errEx.RespError, "Error should be filled.");
                Assert.Null(errEx.OauthError, "OauthError should be null.");
                Assert.AreEqual(fe.Code, errEx.RespError.Error.Code, "Incorrect Error.Code.");
                Assert.AreEqual(fe.Name, errEx.RespError.Error.Name, "Incorrect Error.Name.");
                Assert.AreEqual(fe.Message, errEx.RespError.Error.Message, "Incorrect Error.Message.");
            }
            catch (Exception e) {
                Assert.Fail("WebException expected: " + e);
            }
        }
Пример #6
0
 ////////////////////////////////////////////////////////////////////////////////////////////////
 /*--------------------------------------------------------------------------------------------*/
 public FabResponseExecutor(IApiRequest pReq, Func <IList <T> > pGetResponse) : base(pReq)
 {
     vGetResponse   = pGetResponse;
     OnException    = (e => (e is FabFault ? FabError.ForFault(e as FabFault) : null));
     NewFabResponse = (() => new FabResponse <T>());
 }
Пример #7
0
        /*--------------------------------------------------------------------------------------------*/
        private FabResponse <FabBatchResult> ThreadAddItemsToFabric(IList <T> pBatch, long pIndex)
        {
            var f = new FabricClient();

            f.AppDataProvSession.RefreshTokenIfNecessary();
            f.UseDataProviderPerson = true;

            if (!f.AppDataProvSession.IsAuthenticated)
            {
                throw new Exception("Could not authenticate.");
            }

            if (vDebug)
            {
                ThreadPrint(pIndex, "Starting batch...");
            }

            TBatchNew[] newBatchItems       = GetNewBatchList(pBatch, pIndex);
            FabResponse <FabBatchResult> fr = AddToFabric(f, newBatchItems);

            if (fr == null)
            {
                vFailureCount += vBatchSize;
                throw new Exception(" - FabResponse is null.");
            }

            if (fr.Error != null)
            {
                FabError e = fr.Error;
                vFailureCount += vBatchSize;
                throw new Exception(" - FabError " + e.Code + ": " + e.Name + " / " + e.Message);
            }

            if (fr.Data == null)
            {
                vFailureCount += vBatchSize;
                throw new Exception(" - FabResponse.Data is null.");
            }

            foreach (FabBatchResult br in fr.Data)
            {
                if (br.Error == null)
                {
                    continue;
                }

                vFailureCount++;

                CommIo.Print(" # ERROR: " + br.Error.Name + " (" + br.Error.Code + "): " + br.Error.Message +
                             " [" + br.BatchId + " / " + br.ResultId + "]");

                //Enables "repair" mode

                /*if ( br.Error.Name == "UniqueConstraintViolation" ) {
                 *      const string idStr = "ArtifactId=";
                 *      string msg = br.Error.Message;
                 *      int idIndex = msg.IndexOf(idStr);
                 *
                 *      if ( idIndex != -1 ) {
                 *              idIndex += idStr.Length;
                 *              int dotIndex = msg.IndexOf(".", idIndex);
                 *              string classId = msg.Substring(idIndex, dotIndex-idIndex);
                 *              ThreadPrint(pIndex, "Repair: "+br.BatchId+", "+b.Id+", "+classId);
                 *
                 *              var e2 = new Data.Domain.Export();
                 *              e2.Batch = b;
                 *              e2.FabricId = long.Parse(classId);
                 *              SetItemTypeId(sess, e2, (int)br.BatchId);
                 *              sess.Save(e2);
                 *      }
                 * }*/
            }

            if (vDebug)
            {
                foreach (FabBatchResult br in fr.Data)
                {
                    ThreadPrint(pIndex, " * Export success: " + vTypeName + " " +
                                br.BatchId + " == Fab " + br.ResultId);
                }
            }

            return(fr);
        }