From b06cc3eb09fe3183057c311b278ccaae857c2bfb Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Sun, 5 Sep 2010 16:44:28 +0000 Subject: [PATCH] Exception logging implemented; user-friendly exceptions display --- Builder/IISMainHandler/build.txt | 2 +- Common/UserContext.cs | 8 ++++ IISMainHandler/WebContext.cs | 13 +++++++ IISMainHandler/handlers/AbstractGetHandler.cs | 23 +++++++++--- .../handlers/request/AbstractPostHandler.cs | 33 +++++++++++------ .../handlers/response/ConversationHandler.cs | 2 +- templates/Full/Exception.xslt | 37 +++++++++++++++++++ templates/Lite/Exception.xslt | 37 +++++++++++++++++++ 8 files changed, 136 insertions(+), 19 deletions(-) create mode 100644 templates/Full/Exception.xslt create mode 100644 templates/Lite/Exception.xslt diff --git a/Builder/IISMainHandler/build.txt b/Builder/IISMainHandler/build.txt index 20f199a..6a333d3 100644 --- a/Builder/IISMainHandler/build.txt +++ b/Builder/IISMainHandler/build.txt @@ -1 +1 @@ -1075 \ No newline at end of file +1107 \ No newline at end of file diff --git a/Common/UserContext.cs b/Common/UserContext.cs index 5534a8f..ba5f80a 100644 --- a/Common/UserContext.cs +++ b/Common/UserContext.cs @@ -60,6 +60,14 @@ namespace FLocal.Common { ); } + public static XElement ToXml(this Exception exception) { + return new XElement("exception", + new XElement("type", exception.GetType().FullName), + new XElement("message", exception.Message), + new XElement("trace", exception.StackTrace) + ); + } + } } diff --git a/IISMainHandler/WebContext.cs b/IISMainHandler/WebContext.cs index d334b5e..966d66f 100644 --- a/IISMainHandler/WebContext.cs +++ b/IISMainHandler/WebContext.cs @@ -7,6 +7,7 @@ using FLocal.Core; using FLocal.Common.dataobjects; using FLocal.Common.actions; using System.Xml.Linq; +using System.IO; namespace FLocal.IISHandler { class WebContext : Common.UserContext { @@ -150,5 +151,17 @@ namespace FLocal.IISHandler { } } + public void LogError(Exception e) { + using(StreamWriter writer = new StreamWriter(Common.Config.instance.dataDir + "Logs\\" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + "." + Guid.NewGuid() + ".txt")) { + writer.WriteLine("Exception: " + e.GetType().FullName); + writer.WriteLine(e.Message); + if(e is FLocalException) { + writer.WriteLine(((FLocalException)e).FullStackTrace); + } else { + writer.WriteLine(e.StackTrace); + } + } + } + } } diff --git a/IISMainHandler/handlers/AbstractGetHandler.cs b/IISMainHandler/handlers/AbstractGetHandler.cs index 8fdf8c1..4acb975 100644 --- a/IISMainHandler/handlers/AbstractGetHandler.cs +++ b/IISMainHandler/handlers/AbstractGetHandler.cs @@ -14,21 +14,32 @@ namespace FLocal.IISHandler.handlers { abstract protected IEnumerable getSpecificData(WebContext context); + protected IEnumerable getCommonData(WebContext context) { + return new XElement[] { + new XElement("title", Config.instance.AppInfo), + new XElement("current", DateTime.Now.ToXml()), + context.exportSession(), + context.userSettings.skin.exportToXml(), + new XElement("currentUrl", "/" + String.Join("/", context.requestParts) + "/"), + }; + } + private XDocument getData(WebContext context) { return new XDocument( new XElement("root", this.getSpecificData(context), - new XElement("title", Config.instance.AppInfo), - new XElement("current", DateTime.Now.ToXml()), - context.exportSession(), - context.userSettings.skin.exportToXml(), - new XElement("currentUrl", "/" + String.Join("/", context.requestParts) + "/") + this.getCommonData(context) ) ); } public void Handle(WebContext context) { - context.httpresponse.Write(context.Transform(this.templateName, this.getData(context))); + try { + context.httpresponse.Write(context.Transform(this.templateName, this.getData(context))); + } catch(Exception e) { + context.LogError(e); + context.httpresponse.Write(context.Transform("Exception.xslt", new XDocument(new XElement("root", this.getCommonData(context), e.ToXml())))); + } } } diff --git a/IISMainHandler/handlers/request/AbstractPostHandler.cs b/IISMainHandler/handlers/request/AbstractPostHandler.cs index 57f823e..a6a7212 100644 --- a/IISMainHandler/handlers/request/AbstractPostHandler.cs +++ b/IISMainHandler/handlers/request/AbstractPostHandler.cs @@ -27,28 +27,39 @@ namespace FLocal.IISHandler.handlers.request { abstract protected XElement[] Do(WebContext context); + protected IEnumerable getCommonData(WebContext context) { + return new XElement[] { + new XElement("title", Config.instance.AppInfo), + new XElement("timestamp", DateTime.Now.Ticks.ToString()), + context.userSettings.skin.exportToXml(), + context.exportSession(), + }; + } + private XDocument getData(WebContext context) { return new XDocument( new XElement("root", this.Do(context), - new XElement("title", Config.instance.AppInfo), - new XElement("timestamp", DateTime.Now.Ticks.ToString()), - context.userSettings.skin.exportToXml(), - context.exportSession() + this.getCommonData(context) ) ); } public void Handle(WebContext context) { - Uri referer = context.httprequest.UrlReferrer; - if(referer == null || referer.Host != context.httprequest.Url.Host) { - throw new System.Web.HttpException(403, "Wrong referer"); - } + try { + Uri referer = context.httprequest.UrlReferrer; + if(referer == null || referer.Host != context.httprequest.Url.Host) { + throw new System.Web.HttpException(403, "Wrong referer"); + } - if(this.shouldBeGuest && context.session != null) throw new FLocalException("Should be guest"); - if(this.shouldBeLoggedIn && context.session == null) throw new FLocalException("Should be anonymous"); - context.httpresponse.Write(context.Transform(this.templateName, this.getData(context))); + if(this.shouldBeGuest && context.session != null) throw new FLocalException("Should be guest"); + if(this.shouldBeLoggedIn && context.session == null) throw new FLocalException("Should be anonymous"); + context.httpresponse.Write(context.Transform(this.templateName, this.getData(context))); + } catch(Exception e) { + context.LogError(e); + context.httpresponse.Write(context.Transform("Exception.xslt", new XDocument(new XElement("root", this.getCommonData(context), e.ToXml())))); + } } } diff --git a/IISMainHandler/handlers/response/ConversationHandler.cs b/IISMainHandler/handlers/response/ConversationHandler.cs index f4c0fe1..ba824d9 100644 --- a/IISMainHandler/handlers/response/ConversationHandler.cs +++ b/IISMainHandler/handlers/response/ConversationHandler.cs @@ -46,7 +46,7 @@ namespace FLocal.IISHandler.handlers.response { new ComparisonCondition( PMMessage.TableSpec.instance.getIdSpec(), ComparisonType.LESSTHAN, - int.Parse(context.requestParts[2].PHPSubstring(1)).ToString() + int.Parse(context.requestParts[4].PHPSubstring(1)).ToString() ) ) ) diff --git a/templates/Full/Exception.xslt b/templates/Full/Exception.xslt new file mode 100644 index 0000000..8516af9 --- /dev/null +++ b/templates/Full/Exception.xslt @@ -0,0 +1,37 @@ + + + + Ошибка + + + + + +
+ + + + + + + +
+ Произошла ошибка +
+ Тип ошибки: + +
+ Сообщение об ошибке: + +
+
+ +
+										
+									
+
+
+
+
+ +
\ No newline at end of file diff --git a/templates/Lite/Exception.xslt b/templates/Lite/Exception.xslt new file mode 100644 index 0000000..8516af9 --- /dev/null +++ b/templates/Lite/Exception.xslt @@ -0,0 +1,37 @@ + + + + Ошибка + + + + + +
+ + + + + + + +
+ Произошла ошибка +
+ Тип ошибки: + +
+ Сообщение об ошибке: + +
+
+ +
+										
+									
+
+
+
+
+ +
\ No newline at end of file