Skip to content

Commit

Permalink
added basic exception handling to sitecore ui
Browse files Browse the repository at this point in the history
  • Loading branch information
iamandycohen committed Dec 18, 2014
1 parent fd3ca94 commit 896f832
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 47 deletions.
10 changes: 10 additions & 0 deletions Hi.UrlRewrite/sitecore modules/Shell/UrlRewrite/Default.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
</div>
</div>
</div>

<div id="divInfo" runat="server" Visible="False">

</div>

<div id="divError" runat="server" Visible="False">
<pre id="txtError" runat="server">

</pre>
</div>

</form>

Expand Down
111 changes: 64 additions & 47 deletions Hi.UrlRewrite/sitecore modules/Shell/UrlRewrite/Default.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,74 +29,91 @@ protected void Page_Load(object sender, EventArgs e)
}
else
{
Page.Validate();

if (Page.IsValid)
try
{
divFormGroup.Attributes["class"] = "form-group";
divTable.Visible = true;
Page.Validate();

var rewriter = new UrlRewriter();
_db = Sitecore.Context.ContentDatabase;
if (Page.IsValid)
{
divFormGroup.Attributes["class"] = "form-group";
divTable.Visible = true;

var rulesEngine = new RulesEngine();
var inboundRules = rulesEngine.GetInboundRules(_db);
var rewriter = new UrlRewriter();
_db = Sitecore.Context.ContentDatabase;

ProcessRequestResult results;
var rulesEngine = new RulesEngine();
var inboundRules = rulesEngine.GetInboundRules(_db);

var requestUri = new Uri(txtUrl.Text);
var siteContext = SiteContextFactory.GetSiteContext(requestUri.Host, requestUri.AbsolutePath,
requestUri.Port);
ProcessRequestResult results;

using (new SiteContextSwitcher(siteContext))
using (new DatabaseSwitcher(_db))
{
results = rewriter.ProcessRequestUrl(new Uri(txtUrl.Text), inboundRules);
var requestUri = new Uri(txtUrl.Text);
var siteContext = SiteContextFactory.GetSiteContext(requestUri.Host, requestUri.AbsolutePath,
requestUri.Port);

}
using (new SiteContextSwitcher(siteContext))
using (new DatabaseSwitcher(_db))
{
results = rewriter.ProcessRequestUrl(new Uri(txtUrl.Text), inboundRules);

if (results == null)
{
return;
}
}

if (results == null)
{
divTable.Visible = false;
divInfo.Visible = true;
divInfo.InnerText = "Sorry, I couldn't find any rules to process. :(";
return;
}

resultsRepeater.DataSource = results.ProcessedResults;
resultsRepeater.DataBind();
resultsRepeater.DataSource = results.ProcessedResults;
resultsRepeater.DataBind();

var isAbort = results.FinalAction is AbortRequestAction;
var isCustomResponse = results.FinalAction is CustomResponseAction;
var isAbort = results.FinalAction is AbortRequestAction;
var isCustomResponse = results.FinalAction is CustomResponseAction;

if (isAbort)
{
txtFinalUrl.InnerText = "Aborted";
}
else if (isCustomResponse)
{
var customResponse = results.FinalAction as CustomResponseAction;
const string resultFormat = "Custom Response: {0} {1} {2}";
txtFinalUrl.InnerText = string.Format(resultFormat, customResponse.StatusCode,
customResponse.SubStatusCode, customResponse.ErrorDescription);
}
else
{
if (!results.MatchedAtLeastOneRule)
if (isAbort)
{
txtFinalUrl.InnerText = "No matches.";
txtFinalUrl.InnerText = "Aborted";
}
else if (isCustomResponse)
{
var customResponse = results.FinalAction as CustomResponseAction;
const string resultFormat = "Custom Response: {0} {1} {2}";
txtFinalUrl.InnerText = string.Format(resultFormat, customResponse.StatusCode,
customResponse.SubStatusCode, customResponse.ErrorDescription);
}
else
{
const string resultFormat = "Redirected to {0}.";
txtFinalUrl.InnerText = string.Format(resultFormat, results.RewrittenUri.ToString());
if (!results.MatchedAtLeastOneRule)
{
txtFinalUrl.InnerText = "No matches.";
}
else
{
const string resultFormat = "Redirected to {0}.";
txtFinalUrl.InnerText = string.Format(resultFormat, results.RewrittenUri.ToString());
}
}
}
}
else
{
if (!vldTxtUrl.IsValid)
else
{
divFormGroup.Attributes["class"] = "form-group has-error";
if (!vldTxtUrl.IsValid)
{
divFormGroup.Attributes["class"] = "form-group has-error";
}
}
}
catch (Exception ex)
{
divTable.Visible = false;
divInfo.Visible = false;
divError.Visible = true;
txtError.InnerText = @"Exception:
" + ex.Message;
}

}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 896f832

Please sign in to comment.