JavaScriptException.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using Esprima;
  3. using Jint.Native;
  4. using Jint.Native.Error;
  5. namespace Jint.Runtime
  6. {
  7. public class JavaScriptException : Exception
  8. {
  9. private readonly JsValue _errorObject;
  10. public JavaScriptException(ErrorConstructor errorConstructor) : base("")
  11. {
  12. _errorObject = errorConstructor.Construct(Arguments.Empty);
  13. }
  14. public JavaScriptException(ErrorConstructor errorConstructor, string message)
  15. : base(message)
  16. {
  17. _errorObject = errorConstructor.Construct(new JsValue[] { message });
  18. }
  19. public JavaScriptException(JsValue error)
  20. : base(GetErrorMessage(error))
  21. {
  22. _errorObject = error;
  23. }
  24. private static string GetErrorMessage(JsValue error)
  25. {
  26. if (error.IsObject())
  27. {
  28. var oi = error.AsObject();
  29. var message = oi.Get("message").AsString();
  30. return message;
  31. }
  32. else
  33. return string.Empty;
  34. }
  35. public JsValue Error { get { return _errorObject; } }
  36. public override string ToString()
  37. {
  38. return _errorObject.ToString();
  39. }
  40. public Location Location { get; set; }
  41. public int LineNumber { get { return null == Location ? 0 : Location.Start.Line; } }
  42. public int Column { get { return null == Location ? 0 : Location.Start.Column; } }
  43. }
  44. }