JavaScriptException.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. public override string ToString()
  36. {
  37. return _errorObject.ToString();
  38. }
  39. public Jint.Parser.Location Location { get; set; }
  40. public int LineNumber { get { return null == Location ? 0 : Location.Start.Line; } }
  41. public int Column { get { return null == Location ? 0 : Location.Start.Column; } }
  42. }
  43. }