public virtual void UpdateResponseWithTimeoutDetails(int executionTimeout, BrewResponse response)
        {
            response.StatusCode        = HttpStatusCode.BadRequest;
            response.StatusDescription =
                "The script being executed exceeded the maximum allowable timeout as set by an Administrator. ({0}ms).";
            response.ContentType = "text/html";

            var resultMessage = String.Format(JavaScriptTimeoutMessage, executionTimeout);

            response.Content = Encoding.UTF8.GetBytes(resultMessage);
        }
        /// <summary>
        /// Updates the response with details from the specified exception. If the exception is an aggregate exception, the inner exceptions are reported.
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="response"></param>
        public virtual void UpdateResponseWithExceptionDetails(Exception exception, BrewResponse response)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            while (exception is AggregateException)
            {
                var exceptions = (exception as AggregateException).InnerExceptions;

                exception = exceptions.First();
            }

            var message = exception.Message;

            if (message.StartsWith("EvalError:") ||
                message.StartsWith("InternalError:") ||
                message.StartsWith("RangeError:") ||
                message.StartsWith("ReferenceError:") ||
                message.StartsWith("EvalError:") ||
                message.StartsWith("SyntaxError:") ||
                message.StartsWith("TypeError:") ||
                message.StartsWith("URIError:"))
            {
                message = message.Replace("at ", "at<br/>");
            }

            var stack = exception.StackTrace;

            if (String.IsNullOrEmpty(stack) == false)
            {
                stack = stack.Replace("at ", "at<br/>");
            }

            response.StatusCode        = HttpStatusCode.BadRequest;
            response.StatusDescription = message;
            response.ContentType       = "text/html";

            var resultMessage = String.Format(ExceptionMessage, message, stack);

            response.Content = Encoding.UTF8.GetBytes(resultMessage);
        }
        public BaristaContext(BrewRequest request, BrewResponse response)
            : this()
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (response == null)
            {
                response = new BrewResponse();
            }

            Request  = request;
            Response = response;
        }
        protected virtual void Dispose(bool disposing)
        {
            if (m_disposed)
            {
                return;
            }

            if (m_disposed == false)
            {
                lock (m_disposeLock)
                {
                    if (!m_disposed)
                    {
                        Request    = null;
                        Response   = null;
                        m_disposed = true;
                    }
                }
            }
        }
        public virtual void UpdateResponseWithJavaScriptExceptionDetails(IScriptEngine engine, JavaScriptException exception, BrewResponse response)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            string message;
            var    stack    = String.Empty;
            var    rawStack = String.Empty;

            var javascriptExceptionMessage = JavaScriptExceptionMessage;

            if (TypeUtilities.IsString(exception.ErrorObject))
            {
                message = TypeConverter.ToString(exception.ErrorObject);
            }
            else if (exception.ErrorObject is ObjectInstance)
            {
                var errorObject = exception.ErrorObject as ObjectInstance;

                message = errorObject.GetPropertyValue("message") as string;
                if (message == null || message.IsNullOrWhiteSpace())
                {
                    message = engine.Stringify(exception.ErrorObject, null, 4);
                }
                else
                {
                    stack    = errorObject.GetPropertyValue("stack") as string;
                    rawStack = stack;
                    if (stack != null && stack.IsNullOrWhiteSpace() == false)
                    {
                        stack = stack.Replace("at ", "at<br/>");
                        javascriptExceptionMessage = JavaScriptExceptionMessageWithStackTrace;
                    }
                }
            }
            else
            {
                message = exception.ErrorObject.ToString();
            }

            response.StatusCode        = HttpStatusCode.BadRequest;
            response.StatusDescription = message;
            response.ContentType       = "text/html";

            string exceptionName = exception.Name;

            if (exceptionName.IsNullOrWhiteSpace())
            {
                exceptionName = "JavaScript Error";
            }

            var resultMessage = String.Format(javascriptExceptionMessage, exceptionName, message, exception.FunctionName, exception.LineNumber, exception.SourcePath, stack);

            response.Content = Encoding.UTF8.GetBytes(resultMessage);

            response.ExtendedProperties.Add("Exception_Message", message);
            response.ExtendedProperties.Add("Exception_Name", exceptionName);
            response.ExtendedProperties.Add("Exception_FunctionName", exception.FunctionName);
            response.ExtendedProperties.Add("Exception_LineNumber", exception.LineNumber.ToString(CultureInfo.InvariantCulture));
            response.ExtendedProperties.Add("Exception_SourcePath", exception.SourcePath);
            response.ExtendedProperties.Add("Exception_StackTrace", rawStack);
        }