private void ErrorPage(IRequest request, IResponse response, HttpException exception)
		{
			response.Status = exception.Code;
			response.Reason = exception.Message;
			response.Body.SetLength(0);

			var args = new ErrorPageEventArgs(HttpContext.Current, request, response);
			args.Exception = exception;
			ErrorPageRequested(this, args);

			// Add default error body.
			if (!args.IsHandled)
			{
				string htmlTemplate = "<html><head><title>{1}</title></head><body>Error {0}: {1}</body></html>";
				byte[] body = Encoding.UTF8.GetBytes(string.Format(htmlTemplate, (int) response.Status, response.Reason));
				response.Body.Write(body, 0, body.Length);
			}

			try
			{
				var generator = new ResponseWriter();
				generator.Send(HttpContext.Current, response);
			}
			catch (Exception err)
			{
#if DEBUG
				if (ExceptionThrown.GetInvocationList().Length == 1)
					throw;
#endif
				ExceptionThrown(this, new ExceptionEventArgs(err));
			}
		}
Beispiel #2
0
        private void OnRequest(object sender, RequestEventArgs e)
        {
            _server = this;

            Exception exception;
            try
            {
                ProcessingResult result = HandleRequest(e);
                if (result != ProcessingResult.Continue)
                    return;

                exception = null;
            }
            catch (HttpException err)
            {
                _logger.Error("Got an HTTP exception.", err);
                e.Response.Status = err.Code;
                e.Response.Reason = err.Message;
                exception = err;
            }
            catch (Exception err)
            {
                _logger.Error("Got an unhandled exception.", err);
                exception = err;
                e.Response.Status = HttpStatusCode.InternalServerError;
                e.Response.Reason = "Failed to process request.";
            }


            if (exception == null)
            {
                e.Response.Status = HttpStatusCode.NotFound;
                e.Response.Reason = "Requested resource is not found. Sorry ;(";
                exception = new HttpException(HttpStatusCode.NotFound, "Failed to find uri " + e.Request.Uri);
            }
            DisplayErrorPage(e.Context, exception);
            e.IsHandled = true;
        }