public void SendFabricOauthError() { FabOauthError foe = new FabOauthError(); foe.error = "Test"; foe.error_description = "Desc"; var wr = new TestWebResponse(GetStream(foe)); var we = new WebException("Error", null, WebExceptionStatus.UnknownError, wr); vMoqHttpProv .Setup(x => x.GetResponse(vHttpReq)) .Throws(we); var req = NewFabricRequest <FabOauthAccess>(); try { req.Send(vContext, SessionType.Default); Assert.Fail("WebException expected, but not thrown."); } catch (FabricErrorException errEx) { Assert.Null(errEx.RespError, "Error should be null."); Assert.NotNull(errEx.OauthError, "OauthError should be filled."); Assert.AreEqual(foe.error, errEx.OauthError.error, "Incorrect OauthError.error."); Assert.AreEqual(foe.error_description, errEx.OauthError.error_description, "Incorrect OauthError.error_description."); } catch (Exception e) { Assert.Fail("WebException expected: " + e); } }
/*--------------------------------------------------------------------------------------------*/ private static string OnAccessExecption(IApiRequest pReq, IApiResponse pResp, Exception pEx) { FabOauthError fabErr; if (pEx is OauthException) { fabErr = (pEx as OauthException).OauthError; } else { fabErr = FabOauthError.ForInternalServerError(); Log.Fatal("Unhandled OAuth Access Exception", pEx); } pResp.Status = System.Net.HttpStatusCode.Forbidden; return(fabErr.ToJson()); }
/*--------------------------------------------------------------------------------------------*/ protected virtual FabricResponse <T> GetFabricResponse(IClientContext pContext) { string fullPath = pContext.Config.ApiPath + Path + (Query != null ? "?" + Query : ""); pContext.LogInfo("Request initiated..."); //// try { pContext.LogInfo("Request Path: " + Method + " " + Path); pContext.LogInfo("Request URL: " + fullPath); IFabricHttpResponse wr = GetHttpWebResponse(fullPath); string data = StreamToString(wr.GetResponseStream()); pContext.LogDebug("Request Response: " + data); return(new FabricResponse <T>(JsonSerializer.DeserializeFromString <T>(data))); } catch (WebException we) { if (we.Response == null) { throw new Exception("No Fabric response from " + Method + " " + fullPath); } string data = StreamToString(we.Response.GetResponseStream()); bool isOauthErr = (data.Length > 9 && data.Substring(0, 9) == "{\"error\":"); bool isRespErr = typeof(FabResponse).IsAssignableFrom(typeof(T)); pContext.LogDebug("Request Error: " + data + " (IsError=" + isRespErr + ", IsOauthError=" + isOauthErr + ")"); if (isRespErr) { FabResponse frErr = JsonSerializer.DeserializeFromString <FabResponse>(data); return(new FabricResponse <T>(frErr, we)); } if (isOauthErr) { FabOauthError oerr = JsonSerializer.DeserializeFromString <FabOauthError>(data); return(new FabricResponse <T>(oerr, we)); } throw; } }
/*--------------------------------------------------------------------------------------------*/ private static bool OnLoginException(IApiRequest pReq, IApiResponse pResp, Exception pEx) { FabOauthError fabErr; if (pEx is OauthException) { fabErr = (pEx as OauthException).OauthError; } else { fabErr = FabOauthError.ForInternalServerError(); Log.Fatal("Unhandled OAuth Login Exception", pEx); } string redirUri = pReq.GetQueryValue(LoginRedirectUriParam, false); string state = pReq.GetQueryValue(LoginStateParam, false); pResp.RedirectUrl = BuildRedirectUri(fabErr, redirUri, state); return(true); }
/*--------------------------------------------------------------------------------------------*/ public FabricResponse(FabOauthError pOauthError, Exception pExcep) { OauthError = pOauthError; Excep = pExcep; }
/*--------------------------------------------------------------------------------------------*/ private static string BuildRedirectUri(FabOauthError pErr, string pRedir, string pState) { return(pRedir + "?error=" + pErr.Error + "&error_description=" + pErr.ErrorDesc.Replace(' ', '+') + "&state=" + pState); }
/*--------------------------------------------------------------------------------------------*/ /// <summary /> public FabricErrorException(FabOauthError pOauthError) : base(pOauthError.error + " / " + pOauthError.error_description) { OauthError = pOauthError; }
//////////////////////////////////////////////////////////////////////////////////////////////// /*--------------------------------------------------------------------------------------------*/ public OauthException(string pName, string pDesc) : base(pName + " / " + pDesc) { OauthError = new FabOauthError(); OauthError.Error = pName; OauthError.ErrorDesc = pDesc; }