JavaScriptException.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using Jint.Native;
  3. using Jint.Native.Error;
  4. namespace Jint.Runtime
  5. {
  6. public class JavaScriptException : Exception
  7. {
  8. private readonly JsValue _errorObject;
  9. public JavaScriptException(ErrorConstructor errorConstructor) : base("")
  10. {
  11. _errorObject = errorConstructor.Construct(Arguments.Empty);
  12. }
  13. public JavaScriptException(ErrorConstructor errorConstructor, string message)
  14. : base(message)
  15. {
  16. _errorObject = errorConstructor.Construct(new JsValue[] { message });
  17. }
  18. public JavaScriptException(JsValue error)
  19. : base(GetErrorMessage(error))
  20. {
  21. _errorObject = error;
  22. }
  23. private static string GetErrorMessage(JsValue error)
  24. {
  25. if (error.IsObject())
  26. {
  27. var oi = error.AsObject();
  28. var message = oi.Get("message").AsString();
  29. return message;
  30. }
  31. else
  32. return string.Empty;
  33. }
  34. public JsValue Error { get { return _errorObject; } }
  35. }
  36. }