/// <summary>
 /// Initializes a new instance of the <see cref="JsException"/> class
 /// </summary>
 /// <param name="message">The error message</param>
 /// <param name="innerException">The exception that is the cause of the current exception</param>
 protected JsException(string message, Exception innerException)
     : base(message, innerException)
 {
     if (message != null)
     {
         _code = (JsErrorCode) HResult;
     }
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsException"/> class with serialized data
		/// </summary>
		/// <param name="info">The object that holds the serialized data</param>
		/// <param name="context">The contextual information about the source or destination</param>
		protected JsException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
			if (info != null)
			{
				_errorCode = (JsErrorCode)info.GetUInt32("ErrorCode");
			}
		}
Exemple #3
0
        public static void ThrowFor(JsErrorCode errorCode)
        {
            Debug.Assert(errorCode != JsErrorCode.JsNoError);

            Action throwAction;
            if (!ErrorMap.TryGetValue(errorCode, out throwAction))
            {
                throwAction = () => { throw new Exception($"Unrecognized JavaScript error {errorCode} (0x{errorCode:x8})"); };
            }

            throwAction();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsFatalException"/> class
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 public JsFatalException(JsErrorCode errorCode)
     : base(errorCode)
 {
 }
Exemple #5
0
        public static void CheckForScriptExceptionOrThrow(JsErrorCode errorCode, JavaScriptEngine engine)
        {
            if (errorCode == JsErrorCode.JsErrorScriptException)
            {
                engine.OnRuntimeExceptionRaised();
                return;
            }

            if (errorCode != JsErrorCode.JsNoError)
                ThrowFor(errorCode);
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsEngineException"/> class
		/// with a specified error message
		/// </summary>
		/// <param name="errorCode">The error code returned</param>
		/// <param name="message">The error message</param>
		public JsEngineException(JsErrorCode errorCode, string message)
			: base(errorCode, message)
		{ }
 /// <summary>
 ///     Initializes a new instance of the <see cref="JsScriptException"/> class.
 /// </summary>
 /// <param name="code">The error code returned.</param>
 /// <param name="error">The JavaScript error object.</param>
 public JsScriptException(JsErrorCode code, JsValue error) :
     this(code, error, "Error")
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsScriptException"/> class
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 public JsScriptException(JsErrorCode errorCode)
     : this(errorCode, "JavaScript Exception")
 {
 }
        /// <summary>
        /// Throws if a native method returns an error code.
        /// </summary>
        /// <param name="error">The error.</param>
        internal static void ThrowIfError(JsErrorCode error)
        {
            if (error != JsErrorCode.NoError)
            {
                switch (error)
                {
                case JsErrorCode.InvalidArgument:
                    throw new JsUsageException(error, "Invalid argument.");

                case JsErrorCode.NullArgument:
                    throw new JsUsageException(error, "Null argument.");

                case JsErrorCode.NoCurrentContext:
                    throw new JsUsageException(error, "No current context.");

                case JsErrorCode.InExceptionState:
                    throw new JsUsageException(error, "Runtime is in exception state.");

                case JsErrorCode.NotImplemented:
                    throw new JsUsageException(error, "Method is not implemented.");

                case JsErrorCode.WrongThread:
                    throw new JsUsageException(error, "Runtime is active on another thread.");

                case JsErrorCode.RuntimeInUse:
                    throw new JsUsageException(error, "Runtime is in use.");

                case JsErrorCode.BadSerializedScript:
                    throw new JsUsageException(error, "Bad serialized script.");

                case JsErrorCode.InDisabledState:
                    throw new JsUsageException(error, "Runtime is disabled.");

                case JsErrorCode.CannotDisableExecution:
                    throw new JsUsageException(error, "Cannot disable execution.");

                case JsErrorCode.AlreadyDebuggingContext:
                    throw new JsUsageException(error, "Context is already in debug mode.");

                case JsErrorCode.HeapEnumInProgress:
                    throw new JsUsageException(error, "Heap enumeration is in progress.");

                case JsErrorCode.ArgumentNotObject:
                    throw new JsUsageException(error, "Argument is not an object.");

                case JsErrorCode.InProfileCallback:
                    throw new JsUsageException(error, "In a profile callback.");

                case JsErrorCode.InThreadServiceCallback:
                    throw new JsUsageException(error, "In a thread service callback.");

                case JsErrorCode.CannotSerializeDebugScript:
                    throw new JsUsageException(error, "Cannot serialize a debug script.");

                case JsErrorCode.AlreadyProfilingContext:
                    throw new JsUsageException(error, "Already profiling this context.");

                case JsErrorCode.IdleNotEnabled:
                    throw new JsUsageException(error, "Idle is not enabled.");

                case JsErrorCode.OutOfMemory:
                    throw new JsEngineException(error, "Out of memory.");

                case JsErrorCode.ScriptException:
                {
                    JsErrorCode innerError = JsGetAndClearException(out JsValue errorObject);

                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }
                    throw JsScriptException.FromError(error, errorObject, "Script error");
                }

                case JsErrorCode.ScriptCompile:
                {
                    JsErrorCode innerError = JsGetAndClearException(out JsValue errorObject);

                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }
                    throw JsScriptException.FromError(error, errorObject, "Compile error");
                }

                case JsErrorCode.ScriptTerminated:
                    throw new JsScriptException(error, JsValue.Invalid, "Script was terminated.");

                case JsErrorCode.ScriptEvalDisabled:
                    throw new JsScriptException(error, JsValue.Invalid, "Eval of strings is disabled in this runtime.");

                case JsErrorCode.Fatal:
                    throw new JsFatalException(error);

                default:
                    throw new JsFatalException(error);
                }
            }
        }
Exemple #10
0
        public static void ThrowIfError(JsErrorCode error)
        {
            if (error != JsErrorCode.NoError)
            {
                switch (error)
                {
                case JsErrorCode.InvalidArgument:
                    throw new JsUsageException(error, "Invalid argument.");

                case JsErrorCode.NullArgument:
                    throw new JsUsageException(error, "Null argument.");

                case JsErrorCode.NoCurrentContext:
                    throw new JsUsageException(error, "No current context.");

                case JsErrorCode.InExceptionState:
                    throw new JsUsageException(error, "Runtime is in exception state.");

                case JsErrorCode.NotImplemented:
                    throw new JsUsageException(error, "Method is not implemented.");

                case JsErrorCode.WrongThread:
                    throw new JsUsageException(error, "Runtime is active on another thread.");

                case JsErrorCode.RuntimeInUse:
                    throw new JsUsageException(error, "Runtime is in use.");

                case JsErrorCode.BadSerializedScript:
                    throw new JsUsageException(error, "Bad serialized script.");

                case JsErrorCode.InDisabledState:
                    throw new JsUsageException(error, "Runtime is disabled.");

                case JsErrorCode.CannotDisableExecution:
                    throw new JsUsageException(error, "Cannot disable execution.");

                case JsErrorCode.AlreadyDebuggingContext:
                    throw new JsUsageException(error, "Context is already in debug mode.");

                case JsErrorCode.HeapEnumInProgress:
                    throw new JsUsageException(error, "Heap enumeration is in progress.");

                case JsErrorCode.ArgumentNotObject:
                    throw new JsUsageException(error, "Argument is not an object.");

                case JsErrorCode.InProfileCallback:
                    throw new JsUsageException(error, "In a profile callback.");

                case JsErrorCode.InThreadServiceCallback:
                    throw new JsUsageException(error, "In a thread service callback.");

                case JsErrorCode.CannotSerializeDebugScript:
                    throw new JsUsageException(error, "Cannot serialize a debug script.");

                case JsErrorCode.AlreadyProfilingContext:
                    throw new JsUsageException(error, "Already profiling this context.");

                case JsErrorCode.IdleNotEnabled:
                    throw new JsUsageException(error, "Idle is not enabled.");

                case JsErrorCode.OutOfMemory:
                    throw new JsEngineException(error, "Out of memory.");

                case JsErrorCode.ScriptException:
                {
                    JsErrorCode innerError = LibChakraCore.JsGetAndClearException(out JavaScriptValueSafeHandle errorObject);

                    //The following throws a really bad error, but if you set and get the error again, it works, but with no actual metadata :-/
                    //JsErrorCode innerError = LibChakraCore.JsGetAndClearExceptionWithMetadata(out JavaScriptValueSafeHandle metadataObject);
                    //innerError = LibChakraCore.JsGetPropertyIdFromName("exception", out JavaScriptPropertyIdSafeHandle propertyId);
                    //innerError = LibChakraCore.JsGetProperty(metadataObject, propertyId, out JavaScriptValueSafeHandle errorObject);

                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }

                    innerError = LibChakraCore.JsSetException(errorObject);
                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }

                    throw new JsScriptException(error, errorObject, "Script threw an exception.");
                }

                case JsErrorCode.ScriptCompile:
                {
                    JsErrorCode innerError = LibChakraCore.JsGetAndClearException(out JavaScriptValueSafeHandle errorObject);

                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }

                    innerError = LibChakraCore.JsSetException(errorObject);
                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }

                    throw new JsScriptException(error, errorObject, "Compile error.");
                }

                case JsErrorCode.ScriptTerminated:
                    throw new JsScriptException(error, JavaScriptValueSafeHandle.Invalid, "Script was terminated.");

                case JsErrorCode.ScriptEvalDisabled:
                    throw new JsScriptException(error, JavaScriptValueSafeHandle.Invalid, "Eval of strings is disabled in this runtime.");

                case JsErrorCode.Fatal:
                    throw new JsFatalException(error);

                default:
                    throw new JsFatalException(error);
                }
            }
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsScriptException"/> class
		/// </summary>
		/// <param name="errorCode">The error code returned</param>
		/// <param name="error">The JavaScript error object</param>
		public JsScriptException(JsErrorCode errorCode, JsValue error)
			: this(errorCode, error, "JavaScript Exception")
		{ }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsException"/> class
 /// with a specified error message
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 /// <param name="message">The error message</param>
 public JsException(JsErrorCode errorCode, string message)
     : base(message)
 {
     _errorCode = errorCode;
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="JsException"/> class.
 /// </summary>
 /// <param name="code">The error code returned.</param>
 /// <param name="message">The error message.</param>
 public JsException(JsErrorCode code, string message) :
     base(message)
 {
     ErrorCode = code;
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsUsageException"/> class
		/// </summary>
		/// <param name="errorCode">The error code returned</param>
		public JsUsageException(JsErrorCode errorCode)
			: base(errorCode)
		{ }
		/// <summary>
		/// Initializes a new instance of the <see cref="EdgeJsScriptException"/> class
		/// with a specified error message
		/// </summary>
		/// <param name="errorCode">The error code returned</param>
		/// <param name="error">The JavaScript error object</param>
		/// <param name="message">The error message</param>
		public EdgeJsScriptException(JsErrorCode errorCode, EdgeJsValue error, string message)
			: base(errorCode, message)
		{
			_error = error;
		}
Exemple #16
0
        public static void ThrowIfIs(JsErrorCode errorCode)
        {
            Debug.Assert(errorCode != JsErrorCode.JsErrorScriptException);

            if (errorCode != JsErrorCode.JsNoError)
                throw new Exception(errorCode.ToString());
        }
Exemple #17
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="JsUsageException"/> class.
 /// </summary>
 /// <param name="code">The error code returned.</param>
 public JsUsageException(JsErrorCode code) :
     this(code, "A fatal exception has occurred in a JavaScript runtime")
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsException"/> class
 /// </summary>
 /// <param name="code">The error code returned</param>
 /// <param name="message">The error message</param>
 public JsException(JsErrorCode code, string message)
     : base(message)
 {
     _code = code;
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsException"/> class
		/// with a specified error message
		/// </summary>
		/// <param name="errorCode">The error code returned</param>
		/// <param name="message">The error message</param>
		public JsException(JsErrorCode errorCode, string message)
			: base(message)
		{
			_errorCode = errorCode;
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="JsScriptException"/> class
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 /// <param name="message">The error message</param>
 public JsScriptException(JsErrorCode errorCode, string message)
     : this(errorCode, JsValue.Invalid, message)
 {
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="IeJsScriptException"/> class
		/// </summary>
		/// <param name="code">The error code returned</param>
		/// <param name="error">The JavaScript error object</param>
		/// <param name="message">The error message</param>
		public IeJsScriptException(JsErrorCode code, IeJsValue error, string message)
			: base(code, message)
		{
			_error = error;
		}
Exemple #22
0
 public JsFatalException(JsErrorCode code, string message) :
     base(code, message)
 {
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="JsEngineException"/> class.
 /// </summary>
 /// <param name="code">The error code returned.</param>
 /// <param name="message">The error _message.</param>
 public JsEngineException(JsErrorCode code, string message) :
     base(code, message)
 {
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsFatalException"/> class
		/// </summary>
		/// <param name="errorCode">The error code returned</param>
		public JsFatalException(JsErrorCode errorCode)
			: base(errorCode)
		{ }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsEngineException"/> class
		/// </summary>
		/// <param name="errorCode">The error code returned</param>
		public JsEngineException(JsErrorCode errorCode)
			: base(errorCode)
		{ }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsFatalException"/> class
		/// with a specified error message
		/// </summary>
		/// <param name="errorCode">The error code returned</param>
		/// <param name="message">The error message</param>
		public JsFatalException(JsErrorCode errorCode, string message)
			: base(errorCode, message)
		{ }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsEngineException"/> class
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 public JsEngineException(JsErrorCode errorCode)
     : base(errorCode)
 {
 }
Exemple #28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsUsageException"/> class
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 public JsUsageException(JsErrorCode errorCode)
     : base(errorCode)
 {
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="JsScriptException"/> class.
 /// </summary>
 /// <param name="code">The error code returned.</param>
 /// <param name="error">The JavaScript error object.</param>
 /// <param name="message">The error message.</param>
 public JsScriptException(JsErrorCode code, JsValue error, string message) :
     base(code, message)
 {
     Error = error;
 }
        private static JsValue CreateErrorFromWrapperException(WrapperException exception)
        {
            var         originalException = exception.InnerException as JsException;
            JsErrorCode errorCode         = originalException != null ?
                                            originalException.ErrorCode : JsErrorCode.NoError;
            string description = exception.Description;

            JsValue innerErrorValue = JsErrorHelpers.CreateError(description);

            innerErrorValue.SetProperty("description", JsValue.FromString(description), true);

            JsValue metadataValue = JsValue.CreateObject();

            var scriptException = exception as WrapperScriptException;

            if (scriptException != null)
            {
                string type         = scriptException.Type;
                string documentName = scriptException.DocumentName;
                int    lineNumber   = scriptException.LineNumber;
                if (lineNumber > 0)
                {
                    lineNumber--;
                }
                int columnNumber = scriptException.ColumnNumber;
                if (columnNumber > 0)
                {
                    columnNumber--;
                }
                string sourceFragment = scriptException.SourceFragment;

                innerErrorValue.SetProperty("name", JsValue.FromString(type), true);

                var runtimeException = scriptException as WrapperRuntimeException;
                if (runtimeException != null)
                {
                    var    errorNumber = (int)errorCode;
                    string callStack   = runtimeException.CallStack;
                    string messageWithTypeAndCallStack = CoreErrorHelpers.GenerateScriptErrorMessage(type,
                                                                                                     description, callStack);

                    innerErrorValue.SetProperty("number", JsValue.FromInt32(errorNumber), true);
                    if (!string.IsNullOrWhiteSpace(callStack))
                    {
                        innerErrorValue.SetProperty("stack", JsValue.FromString(messageWithTypeAndCallStack), true);
                    }
                }
                else
                {
                    innerErrorValue.SetProperty("url", JsValue.FromString(documentName), true);
                    innerErrorValue.SetProperty("line", JsValue.FromInt32(lineNumber), true);
                    innerErrorValue.SetProperty("column", JsValue.FromInt32(columnNumber), true);
                    innerErrorValue.SetProperty("source", JsValue.FromString(sourceFragment), true);
                }

                metadataValue.SetProperty("url", JsValue.FromString(documentName), true);
                metadataValue.SetProperty("line", JsValue.FromInt32(lineNumber), true);
                metadataValue.SetProperty("column", JsValue.FromInt32(columnNumber), true);
                metadataValue.SetProperty("source", JsValue.FromString(sourceFragment), true);
            }

            innerErrorValue.SetProperty("metadata", metadataValue, true);

            JsValue errorValue = JsErrorHelpers.CreateError(description);

            errorValue.SetProperty("innerException", innerErrorValue, true);

            return(errorValue);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsUsageException"/> class
 /// </summary>
 /// <param name="code">The error code returned</param>
 /// <param name="message">The error message</param>
 public JsUsageException(JsErrorCode code, string message)
     : base(code, message)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsScriptException"/> class
 /// with a specified error message
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 /// <param name="metadata">The JavaScript error metadata</param>
 /// <param name="message">The error message</param>
 internal JsScriptException(JsErrorCode errorCode, JsValue metadata, string message)
     : base(errorCode, message)
 {
     _metadata = metadata;
 }
Exemple #33
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsUsageException"/> class
 /// with a specified error message
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 /// <param name="message">The error message</param>
 public JsUsageException(JsErrorCode errorCode, string message)
     : base(errorCode, message)
 {
 }
Exemple #34
0
 public static void ThrowIfIs(JsErrorCode errorCode)
 {
     if (errorCode != JsErrorCode.JsNoError)
         ThrowFor(errorCode);
 }
Exemple #35
0
        private static WrapperException WrapJsException(OriginalException originalException,
                                                        string defaultDocumentName = null)
        {
            WrapperException wrapperException;
            JsErrorCode      errorCode    = originalException.ErrorCode;
            string           description  = originalException.Message;
            string           message      = description;
            string           type         = string.Empty;
            string           documentName = defaultDocumentName ?? string.Empty;
            int    lineNumber             = 0;
            int    columnNumber           = 0;
            string callStack      = string.Empty;
            string sourceFragment = string.Empty;

            var originalScriptException = originalException as OriginalScriptException;

            if (originalScriptException != null)
            {
                JsValue metadataValue = originalScriptException.Metadata;

                if (metadataValue.IsValid)
                {
                    JsValue     errorValue     = metadataValue.GetProperty("exception");
                    JsValueType errorValueType = errorValue.ValueType;

                    if (errorValueType == JsValueType.Error ||
                        errorValueType == JsValueType.Object)
                    {
                        JsPropertyId innerErrorPropertyId = JsPropertyId.FromString("innerException");
                        if (errorValue.HasProperty(innerErrorPropertyId))
                        {
                            JsValue      innerErrorValue    = errorValue.GetProperty(innerErrorPropertyId);
                            JsPropertyId metadataPropertyId = JsPropertyId.FromString("metadata");

                            if (innerErrorValue.HasProperty(metadataPropertyId))
                            {
                                errorValue    = innerErrorValue;
                                metadataValue = innerErrorValue.GetProperty(metadataPropertyId);
                            }
                        }

                        JsValue messagePropertyValue = errorValue.GetProperty("message");
                        string  localDescription     = messagePropertyValue.ConvertToString().ToString();
                        if (!string.IsNullOrWhiteSpace(localDescription))
                        {
                            description = localDescription;
                        }

                        JsValue namePropertyValue = errorValue.GetProperty("name");
                        type = namePropertyValue.ValueType == JsValueType.String ?
                               namePropertyValue.ToString() : string.Empty;

                        JsPropertyId descriptionPropertyId = JsPropertyId.FromString("description");
                        if (errorValue.HasProperty(descriptionPropertyId))
                        {
                            JsValue descriptionPropertyValue = errorValue.GetProperty(descriptionPropertyId);
                            localDescription = descriptionPropertyValue.ConvertToString().ToString();
                            if (!string.IsNullOrWhiteSpace(localDescription))
                            {
                                description = localDescription;
                            }
                        }

                        if (type == JsErrorType.Syntax)
                        {
                            errorCode = JsErrorCode.ScriptCompile;
                        }
                        else
                        {
                            JsPropertyId numberPropertyId = JsPropertyId.FromString("number");
                            if (errorValue.HasProperty(numberPropertyId))
                            {
                                JsValue numberPropertyValue = errorValue.GetProperty(numberPropertyId);
                                int     errorNumber         = numberPropertyValue.ValueType == JsValueType.Number ?
                                                              numberPropertyValue.ToInt32() : 0;
                                errorCode = (JsErrorCode)errorNumber;
                            }
                        }

                        JsPropertyId urlPropertyId = JsPropertyId.FromString("url");
                        if (metadataValue.HasProperty(urlPropertyId))
                        {
                            JsValue urlPropertyValue = metadataValue.GetProperty(urlPropertyId);
                            string  url = urlPropertyValue.ValueType == JsValueType.String ?
                                          urlPropertyValue.ToString() : string.Empty;
                            if (url != "undefined")
                            {
                                documentName = url;
                            }
                        }

                        JsPropertyId linePropertyId = JsPropertyId.FromString("line");
                        if (metadataValue.HasProperty(linePropertyId))
                        {
                            JsValue linePropertyValue = metadataValue.GetProperty(linePropertyId);
                            lineNumber = linePropertyValue.ValueType == JsValueType.Number ?
                                         linePropertyValue.ToInt32() + 1 : 0;
                        }

                        JsPropertyId columnPropertyId = JsPropertyId.FromString("column");
                        if (metadataValue.HasProperty(columnPropertyId))
                        {
                            JsValue columnPropertyValue = metadataValue.GetProperty(columnPropertyId);
                            columnNumber = columnPropertyValue.ValueType == JsValueType.Number ?
                                           columnPropertyValue.ToInt32() + 1 : 0;
                        }

                        string       sourceLine       = string.Empty;
                        JsPropertyId sourcePropertyId = JsPropertyId.FromString("source");
                        if (metadataValue.HasProperty(sourcePropertyId))
                        {
                            JsValue sourcePropertyValue = metadataValue.GetProperty(sourcePropertyId);
                            sourceLine = sourcePropertyValue.ValueType == JsValueType.String ?
                                         sourcePropertyValue.ToString() : string.Empty;
                            sourceFragment = TextHelpers.GetTextFragmentFromLine(sourceLine, columnNumber);
                        }

                        JsPropertyId stackPropertyId = JsPropertyId.FromString("stack");
                        if (errorValue.HasProperty(stackPropertyId))
                        {
                            JsValue stackPropertyValue          = errorValue.GetProperty(stackPropertyId);
                            string  messageWithTypeAndCallStack = stackPropertyValue.ValueType == JsValueType.String ?
                                                                  stackPropertyValue.ToString() : string.Empty;
                            string messageWithType = errorValue.ConvertToString().ToString();
                            string rawCallStack    = messageWithTypeAndCallStack
                                                     .TrimStart(messageWithType)
                                                     .TrimStart("Error")
                                                     .TrimStart(new char[] { '\n', '\r' })
                            ;
                            string callStackWithSourceFragment = string.Empty;

                            ErrorLocationItem[] callStackItems = CoreErrorHelpers.ParseErrorLocation(rawCallStack);
                            if (callStackItems.Length > 0)
                            {
                                ErrorLocationItem firstCallStackItem = callStackItems[0];
                                firstCallStackItem.SourceFragment = sourceFragment;

                                documentName = firstCallStackItem.DocumentName;
                                lineNumber   = firstCallStackItem.LineNumber;
                                columnNumber = firstCallStackItem.ColumnNumber;
                                callStack    = CoreErrorHelpers.StringifyErrorLocationItems(callStackItems, true);
                                callStackWithSourceFragment = CoreErrorHelpers.StringifyErrorLocationItems(callStackItems);
                            }

                            message = CoreErrorHelpers.GenerateScriptErrorMessage(type, description,
                                                                                  callStackWithSourceFragment);
                        }
                        else
                        {
                            message = CoreErrorHelpers.GenerateScriptErrorMessage(type, description, documentName,
                                                                                  lineNumber, columnNumber, sourceFragment);
                        }
                    }
                    else if (errorValueType == JsValueType.String)
                    {
                        message     = errorValue.ToString();
                        description = message;
                    }
                    else
                    {
                        message     = errorValue.ConvertToString().ToString();
                        description = message;
                    }
                }

                WrapperScriptException wrapperScriptException;
                if (errorCode == JsErrorCode.ScriptCompile)
                {
                    wrapperScriptException = new WrapperCompilationException(message, EngineName, EngineVersion,
                                                                             originalScriptException);
                }
                else if (errorCode == JsErrorCode.ScriptTerminated)
                {
                    message     = CoreStrings.Runtime_ScriptInterrupted;
                    description = message;

                    wrapperScriptException = new WrapperInterruptedException(message,
                                                                             EngineName, EngineVersion, originalScriptException)
                    {
                        CallStack = callStack
                    };
                }
                else
                {
                    wrapperScriptException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                         originalScriptException)
                    {
                        CallStack = callStack
                    };
                }
                wrapperScriptException.Type           = type;
                wrapperScriptException.DocumentName   = documentName;
                wrapperScriptException.LineNumber     = lineNumber;
                wrapperScriptException.ColumnNumber   = columnNumber;
                wrapperScriptException.SourceFragment = sourceFragment;

                wrapperException = wrapperScriptException;
            }
            else
            {
                if (originalException is OriginalUsageException)
                {
                    wrapperException = new WrapperUsageException(message, EngineName, EngineVersion,
                                                                 originalException);
                }
                else if (originalException is OriginalEngineException)
                {
                    wrapperException = new WrapperEngineException(message, EngineName, EngineVersion,
                                                                  originalException);
                }
                else if (originalException is OriginalFatalException)
                {
                    wrapperException = new WrapperFatalException(message, EngineName, EngineVersion,
                                                                 originalException);
                }
                else
                {
                    wrapperException = new WrapperException(message, EngineName, EngineVersion,
                                                            originalException);
                }
            }

            wrapperException.Description = description;

            return(wrapperException);
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="JsScriptException"/> class.
 /// </summary>
 /// <param name="code">The error code returned.</param>
 /// <param name="error">The JavaScript error object.</param>
 public JsScriptException(JsErrorCode code, JavaScriptValueSafeHandle error) :
     this(code, error, "JavaScript Exception")
 {
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="JsException"/> class
		/// </summary>
		/// <param name="errorCode">The error code returned</param>
		public JsException(JsErrorCode errorCode)
			: this(errorCode, "A fatal exception has occurred in a JavaScript runtime")
		{ }
        /// <summary>
        ///     Initializes a new instance of the <see cref="JsScriptException"/> class.
        /// </summary>
        /// <param name="code">The error code returned.</param>
        /// <param name="error">The JavaScript error object.</param>
        /// <param name="message">The error message.</param>
        public JsScriptException(JsErrorCode code, JavaScriptValueSafeHandle error, string message) :
            base(code, message)
        {
            m_error   = error;
            m_message = message;

            //Don't use our helper errors class in order to prevent recursive errors.
            JsErrorCode innerError;

            //Get the error object type
            innerError = LibChakraCore.JsGetValueType(error, out JsValueType errorType);

            switch (errorType)
            {
            case JsValueType.Object:
                if (TryGetErrorProperty(error, ColumnNumberPropertyName, out int metadataColumnValue))
                {
                    ColumnNumber = metadataColumnValue;
                }

                if (TryGetErrorProperty(error, LineNumberPropertyName, out int metadataLineValue))
                {
                    LineNumber = metadataLineValue;
                }
                if (TryGetErrorProperty(error, "source", out string metadataScriptSourceValue))
                {
                    ScriptSource = metadataScriptSourceValue;
                }
                if (TryGetErrorProperty(error, "url", out string metadataUrlValue))
                {
                    Name = metadataUrlValue;
                }
                break;

            case JsValueType.Error:
                //Get the message of the Script Error.
                if (TryGetErrorProperty(error, MessagePropertyName, out string errrorMessageValue))
                {
                    m_message = errrorMessageValue;
                }

                if (TryGetErrorProperty(error, ColumnNumberPropertyName, out int errorColumnValue))
                {
                    ColumnNumber = errorColumnValue;
                }

                if (TryGetErrorProperty(error, LineNumberPropertyName, out int errorLineValue))
                {
                    LineNumber = errorLineValue;
                }

                if (TryGetErrorProperty(error, NamePropertyName, out string errorNameValue))
                {
                    Name = errorNameValue;
                }

                break;

            case JsValueType.String:
                m_message = Helpers.GetStringUtf8(error);
                break;
            }
        }
Exemple #39
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IeJsScriptException"/> class
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 /// <param name="error">The JavaScript error object</param>
 public IeJsScriptException(JsErrorCode errorCode, IeJsValue error)
     : this(errorCode, error, "JavaScript Exception")
 {
 }
Exemple #40
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IeJsScriptException"/> class
 /// with a specified error message
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 /// <param name="error">The JavaScript error object</param>
 /// <param name="message">The error message</param>
 public IeJsScriptException(JsErrorCode errorCode, IeJsValue error, string message)
     : base(errorCode, message)
 {
     _error = error;
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="IeJsScriptException"/> class
		/// </summary>
		/// <param name="code">The error code returned</param>
		/// <param name="error">The JavaScript error object</param>
		public IeJsScriptException(JsErrorCode code, IeJsValue error)
			: this(code, error, "JavaScript Exception")
		{ }
Exemple #42
0
        /// <summary>
        /// Throws if a native method returns an error code
        /// </summary>
        /// <param name="error">The error</param>
        public static void ThrowIfError(JsErrorCode error)
        {
            if (error != JsErrorCode.NoError)
            {
                switch (error)
                {
                    #region Usage

                case JsErrorCode.InvalidArgument:
                    throw new JsUsageException(error, "Invalid argument.");

                case JsErrorCode.NullArgument:
                    throw new JsUsageException(error, "Null argument.");

                case JsErrorCode.NoCurrentContext:
                    throw new JsUsageException(error, "No current context.");

                case JsErrorCode.InExceptionState:
                    throw new JsUsageException(error, "Runtime is in exception state.");

                case JsErrorCode.NotImplemented:
                    throw new JsUsageException(error, "Method is not implemented.");

                case JsErrorCode.WrongThread:
                    throw new JsUsageException(error, "Runtime is active on another thread.");

                case JsErrorCode.RuntimeInUse:
                    throw new JsUsageException(error, "Runtime is in use.");

                case JsErrorCode.BadSerializedScript:
                    throw new JsUsageException(error, "Bad serialized script.");

                case JsErrorCode.InDisabledState:
                    throw new JsUsageException(error, "Runtime is disabled.");

                case JsErrorCode.CannotDisableExecution:
                    throw new JsUsageException(error, "Cannot disable execution.");

                case JsErrorCode.HeapEnumInProgress:
                    throw new JsUsageException(error, "Heap enumeration is in progress.");

                case JsErrorCode.ArgumentNotObject:
                    throw new JsUsageException(error, "Argument is not an object.");

                case JsErrorCode.InProfileCallback:
                    throw new JsUsageException(error, "In a profile callback.");

                case JsErrorCode.InThreadServiceCallback:
                    throw new JsUsageException(error, "In a thread service callback.");

                case JsErrorCode.CannotSerializeDebugScript:
                    throw new JsUsageException(error, "Cannot serialize a debug script.");

                case JsErrorCode.AlreadyDebuggingContext:
                    throw new JsUsageException(error, "Context is already in debug mode.");

                case JsErrorCode.AlreadyProfilingContext:
                    throw new JsUsageException(error, "Already profiling this context.");

                case JsErrorCode.IdleNotEnabled:
                    throw new JsUsageException(error, "Idle is not enabled.");

                case JsErrorCode.CannotSetProjectionEnqueueCallback:
                    throw new JsUsageException(error, "Cannot set projection enqueue callback.");

                case JsErrorCode.CannotStartProjection:
                    throw new JsUsageException(error, "Cannot start projection.");

                case JsErrorCode.InObjectBeforeCollectCallback:
                    throw new JsUsageException(error, "In object before collect callback.");

                case JsErrorCode.ObjectNotInspectable:
                    throw new JsUsageException(error, "Object not inspectable.");

                case JsErrorCode.PropertyNotSymbol:
                    throw new JsUsageException(error, "Property not symbol.");

                case JsErrorCode.PropertyNotString:
                    throw new JsUsageException(error, "Property not string.");

                case JsErrorCode.InvalidContext:
                    throw new JsUsageException(error, "Invalid context.");

                case JsErrorCode.InvalidModuleHostInfoKind:
                    throw new JsUsageException(error, "Invalid module host info kind.");

                case JsErrorCode.ModuleParsed:
                    throw new JsUsageException(error, "Module parsed.");

                case JsErrorCode.NoWeakRefRequired:
                    throw new JsUsageException(error, "No weak reference is required, the value will never be collected.");

                case JsErrorCode.PromisePending:
                    throw new JsUsageException(error, "The `Promise` object is still in the pending state.");

                case JsErrorCode.ModuleNotEvaluated:
                    throw new JsUsageException(error, "Module was not yet evaluated when `JsGetModuleNamespace` was called.");

                    #endregion

                    #region Engine

                case JsErrorCode.OutOfMemory:
                    throw new JsEngineException(error, "Out of memory.");

                case JsErrorCode.BadFPUState:
                    throw new JsEngineException(error, "Bad the Floating Point Unit state.");

                    #endregion

                    #region Script

                case JsErrorCode.ScriptException:
                case JsErrorCode.ScriptCompile:
                {
                    JsValue     errorMetadata;
                    JsErrorCode innerError = NativeMethods.JsGetAndClearExceptionWithMetadata(out errorMetadata);

                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }

                    string message = error == JsErrorCode.ScriptCompile ?
                                     "Compile error." : "Script threw an exception.";

                    throw new JsScriptException(error, errorMetadata, message);
                }

                case JsErrorCode.ScriptTerminated:
                    throw new JsScriptException(error, JsValue.Invalid, "Script was terminated.");

                case JsErrorCode.ScriptEvalDisabled:
                    throw new JsScriptException(error, JsValue.Invalid, "Eval of strings is disabled in this runtime.");

                    #endregion

                    #region Fatal

                case JsErrorCode.Fatal:
                    throw new JsFatalException(error, "Fatal error.");

                case JsErrorCode.WrongRuntime:
                    throw new JsFatalException(error, "Wrong runtime.");

                    #endregion

                default:
                    throw new JsFatalException(error);
                }
            }
        }
		/// <summary>
		/// Throws if a native method returns an error code
		/// </summary>
		/// <param name="error">The error</param>
		public static void ThrowIfError(JsErrorCode error)
		{
			if (error != JsErrorCode.NoError)
			{
				switch (error)
				{
					case JsErrorCode.InvalidArgument:
						throw new JsUsageException(error, "Invalid argument.");

					case JsErrorCode.NullArgument:
						throw new JsUsageException(error, "Null argument.");

					case JsErrorCode.NoCurrentContext:
						throw new JsUsageException(error, "No current context.");

					case JsErrorCode.InExceptionState:
						throw new JsUsageException(error, "Runtime is in exception state.");

					case JsErrorCode.NotImplemented:
						throw new JsUsageException(error, "Method is not implemented.");

					case JsErrorCode.WrongThread:
						throw new JsUsageException(error, "Runtime is active on another thread.");

					case JsErrorCode.RuntimeInUse:
						throw new JsUsageException(error, "Runtime is in use.");

					case JsErrorCode.BadSerializedScript:
						throw new JsUsageException(error, "Bad serialized script.");

					case JsErrorCode.InDisabledState:
						throw new JsUsageException(error, "Runtime is disabled.");

					case JsErrorCode.CannotDisableExecution:
						throw new JsUsageException(error, "Cannot disable execution.");

					case JsErrorCode.AlreadyDebuggingContext:
						throw new JsUsageException(error, "Context is already in debug mode.");

					case JsErrorCode.HeapEnumInProgress:
						throw new JsUsageException(error, "Heap enumeration is in progress.");

					case JsErrorCode.ArgumentNotObject:
						throw new JsUsageException(error, "Argument is not an object.");

					case JsErrorCode.InProfileCallback:
						throw new JsUsageException(error, "In a profile callback.");

					case JsErrorCode.InThreadServiceCallback:
						throw new JsUsageException(error, "In a thread service callback.");

					case JsErrorCode.CannotSerializeDebugScript:
						throw new JsUsageException(error, "Cannot serialize a debug script.");

					case JsErrorCode.AlreadyProfilingContext:
						throw new JsUsageException(error, "Already profiling this context.");

					case JsErrorCode.IdleNotEnabled:
						throw new JsUsageException(error, "Idle is not enabled.");

					case JsErrorCode.OutOfMemory:
						throw new JsEngineException(error, "Out of memory.");

					case JsErrorCode.ScriptException:
						{
							IeJsValue errorObject;
							JsErrorCode innerError = IeNativeMethods.JsGetAndClearException(out errorObject);

							if (innerError != JsErrorCode.NoError)
							{
								throw new JsFatalException(innerError);
							}

							throw new IeJsScriptException(error, errorObject, "Script threw an exception.");
						}

					case JsErrorCode.ScriptCompile:
						{
							IeJsValue errorObject;
							JsErrorCode innerError = IeNativeMethods.JsGetAndClearException(out errorObject);

							if (innerError != JsErrorCode.NoError)
							{
								throw new JsFatalException(innerError);
							}

							throw new IeJsScriptException(error, errorObject, "Compile error.");
						}

					case JsErrorCode.ScriptTerminated:
						throw new IeJsScriptException(error, IeJsValue.Invalid, "Script was terminated.");

					case JsErrorCode.ScriptEvalDisabled:
						throw new IeJsScriptException(error, IeJsValue.Invalid, "Eval of strings is disabled in this runtime.");

					case JsErrorCode.Fatal:
						throw new JsFatalException(error);

					default:
						throw new JsFatalException(error);
				}
			}
		}
Exemple #44
0
        public static void ThrowFor(JsErrorCode errorCode)
        {
            Debug.Assert(errorCode != JsErrorCode.JsNoError);

            throw new Exception(errorCode.ToString());
        }