Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Wrap the retrieval of HTTP context in try/catch since it might fail i…
Browse files Browse the repository at this point in the history
…n circumstances where the HTTP context is available, but not yet bootstrapped (like in ASP.NET Application Start and such).
  • Loading branch information
asbjornu committed Nov 19, 2014
1 parent 524d754 commit 278f890
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/app/SharpRaven/Data/SentryRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public SentryUser GetUser()
if (!HasHttpContext)
return null;

return new SentryUser((IPrincipal)this.httpContext.User)
return new SentryUser(this.httpContext.User as IPrincipal)
{
IpAddress = this.httpContext.Request.UserHostAddress
};
Expand All @@ -169,26 +169,34 @@ public SentryUser GetUser()

private static dynamic GetHttpContext()
{
var systemWeb = AppDomain.CurrentDomain
.GetAssemblies()
.FirstOrDefault(assembly => assembly.FullName.StartsWith("System.Web,"));
try
{
var systemWeb = AppDomain.CurrentDomain
.GetAssemblies()
.FirstOrDefault(assembly => assembly.FullName.StartsWith("System.Web,"));

if (systemWeb == null)
return null;
if (systemWeb == null)
return null;

var httpContextType = systemWeb.GetExportedTypes()
.FirstOrDefault(type => type.Name == "HttpContext");
var httpContextType = systemWeb.GetExportedTypes()
.FirstOrDefault(type => type.Name == "HttpContext");

if (httpContextType == null)
return null;
if (httpContextType == null)
return null;

var currentHttpContextProperty = httpContextType.GetProperty("Current",
BindingFlags.Static | BindingFlags.Public);
var currentHttpContextProperty = httpContextType.GetProperty("Current",
BindingFlags.Static | BindingFlags.Public);

if (currentHttpContextProperty == null)
return null;
if (currentHttpContextProperty == null)
return null;

return currentHttpContextProperty.GetValue(null, null);
return currentHttpContextProperty.GetValue(null, null);
}
catch (Exception exception)
{
Console.WriteLine("An error occurred while retrieving the HTTP context: {0}", exception);
return null;
}
}


Expand Down

0 comments on commit 278f890

Please sign in to comment.