The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ErrorController.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Mail;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using Teknik.Areas.Error.ViewModels;
  9. using Teknik.Controllers;
  10. using Teknik.Filters;
  11. using Teknik.Utilities;
  12. using Teknik.Logging;
  13. using Teknik.Attributes;
  14. namespace Teknik.Areas.Error.Controllers
  15. {
  16. [TeknikAuthorize]
  17. public class ErrorController : DefaultController
  18. {
  19. [AllowAnonymous]
  20. public ActionResult Exception(Exception exception)
  21. {
  22. ViewBag.Title = "Exception - " + Config.Title;
  23. ViewBag.Description = "Just a boring 'ol exception. Nothing to see here, move along.";
  24. if (Response != null)
  25. {
  26. Response.StatusCode = 500;
  27. Response.TrySkipIisCustomErrors = true;
  28. }
  29. LogError(LogLevel.Error, "General Exception", exception);
  30. ErrorViewModel model = new ErrorViewModel();
  31. model.Exception = exception;
  32. return View("~/Areas/Error/Views/Error/Exception.cshtml", model);
  33. }
  34. [AllowAnonymous]
  35. public ActionResult General(Exception exception)
  36. {
  37. ViewBag.Title = "Http Exception - " + Config.Title;
  38. ViewBag.Description = "There has been a Http exception. Run!";
  39. if (Response != null)
  40. {
  41. Response.StatusCode = 500;
  42. Response.TrySkipIisCustomErrors = true;
  43. }
  44. LogError(LogLevel.Error, "General HTTP Exception", exception);
  45. ErrorViewModel model = new ErrorViewModel();
  46. model.Description = exception.Message;
  47. model.Exception = exception;
  48. return View("~/Areas/Error/Views/Error/General.cshtml", model);
  49. }
  50. [AllowAnonymous]
  51. public ActionResult Http401(Exception exception)
  52. {
  53. ViewBag.Title = "401 - " + Config.Title;
  54. ViewBag.Description = "Unauthorized";
  55. if (Response != null)
  56. {
  57. Response.StatusCode = 401;
  58. Response.TrySkipIisCustomErrors = true;
  59. }
  60. LogError(LogLevel.Error, "Unauthorized", exception);
  61. ErrorViewModel model = new ErrorViewModel();
  62. model.Exception = exception;
  63. return View("~/Areas/Error/Views/Error/Http401.cshtml", model);
  64. }
  65. [AllowAnonymous]
  66. public ActionResult Http403(Exception exception)
  67. {
  68. ViewBag.Title = "403 - " + Config.Title;
  69. ViewBag.Description = "Access Denied";
  70. if (Response != null)
  71. {
  72. Response.StatusCode = 403;
  73. Response.TrySkipIisCustomErrors = true;
  74. }
  75. LogError(LogLevel.Error, "Access Denied", exception);
  76. ErrorViewModel model = new ErrorViewModel();
  77. model.Exception = exception;
  78. return View("~/Areas/Error/Views/Error/Http403.cshtml", model);
  79. }
  80. [AllowAnonymous]
  81. public ActionResult Http404(Exception exception)
  82. {
  83. ViewBag.Title = "404 - " + Config.Title;
  84. ViewBag.Description = "Uh Oh, can't find it!";
  85. if (Response != null)
  86. {
  87. Response.StatusCode = 404;
  88. Response.TrySkipIisCustomErrors = true;
  89. }
  90. LogError(LogLevel.Warning, "Page Not Found", exception);
  91. ErrorViewModel model = new ErrorViewModel();
  92. model.Exception = exception;
  93. return View("~/Areas/Error/Views/Error/Http404.cshtml", model);
  94. }
  95. [AllowAnonymous]
  96. public ActionResult Http500(Exception exception)
  97. {
  98. Session["Exception"] = exception;
  99. ViewBag.Title = "500 - " + Config.Title;
  100. ViewBag.Description = "Something Borked";
  101. if (Response != null)
  102. {
  103. Response.StatusCode = 500;
  104. Response.TrySkipIisCustomErrors = true;
  105. }
  106. LogError(LogLevel.Error, "Server Error", exception);
  107. ErrorViewModel model = new ErrorViewModel();
  108. model.Exception = exception;
  109. return View("~/Areas/Error/Views/Error/Http500.cshtml", model);
  110. }
  111. [HttpPost]
  112. [AllowAnonymous]
  113. [ValidateAntiForgeryToken]
  114. public ActionResult SubmitErrorReport(SubmitReportViewModel model)
  115. {
  116. try
  117. {
  118. string exceptionMsg = model.Exception;
  119. // Try to grab the actual exception that occured
  120. object exceptionObj = Session["Exception"];
  121. if (exceptionObj != null)
  122. {
  123. Exception ex = (Exception) exceptionObj;
  124. exceptionMsg = string.Format(@"
  125. Exception: {0}
  126. Source: {1}
  127. Stack Trace:
  128. {2}
  129. ", ex.GetFullMessage(true), ex.Source, ex.StackTrace);
  130. }
  131. // Let's also email the message to support
  132. SmtpClient client = new SmtpClient();
  133. client.Host = Config.ContactConfig.EmailAccount.Host;
  134. client.Port = Config.ContactConfig.EmailAccount.Port;
  135. client.EnableSsl = Config.ContactConfig.EmailAccount.SSL;
  136. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  137. client.UseDefaultCredentials = true;
  138. client.Credentials = new System.Net.NetworkCredential(Config.ContactConfig.EmailAccount.Username, Config.ContactConfig.EmailAccount.Password);
  139. client.Timeout = 5000;
  140. MailMessage mail = new MailMessage(new MailAddress(Config.NoReplyEmail, Config.NoReplyEmail), new MailAddress(Config.SupportEmail, "Teknik Support"));
  141. mail.Sender = new MailAddress(Config.ContactConfig.EmailAccount.EmailAddress);
  142. mail.Subject = "[Exception] Application Exception Occured";
  143. mail.Body = @"
  144. An exception has occured at: " + model.CurrentUrl + @"
  145. ----------------------------------------
  146. User Message:
  147. " + model.Message + @"
  148. ----------------------------------------
  149. " + exceptionMsg;
  150. mail.BodyEncoding = UTF8Encoding.UTF8;
  151. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
  152. client.Send(mail);
  153. }
  154. catch (Exception ex)
  155. {
  156. return Json(new { error = "Error submitting report. Exception: " + ex.Message });
  157. }
  158. return Json(new { result = "true" });
  159. }
  160. private void LogError(LogLevel level, string message, Exception exception)
  161. {
  162. if (Request != null)
  163. {
  164. if (Request.Url != null)
  165. {
  166. message += " | Url: " + Request.Url.AbsoluteUri;
  167. }
  168. if (Request.UrlReferrer != null)
  169. {
  170. message += " | Referred Url: " + Request.Url.AbsoluteUri;
  171. }
  172. message += " | Method: " + Request.HttpMethod;
  173. message += " | User Agent: " + Request.UserAgent;
  174. }
  175. Logger.WriteEntry(level, message, exception);
  176. }
  177. }
  178. }