Exception.hx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package haxe;
  2. import python.Exceptions.BaseException;
  3. import python.Exceptions.Exception in PyException;
  4. import python.lib.Traceback;
  5. import python.internal.UBuiltins;
  6. private typedef PyStackItem = python.Tuple.Tuple4<String, Int, String, String>;
  7. @:coreApi
  8. class Exception extends PyException {
  9. public var message(get,never):String;
  10. public var stack(get,never):CallStack;
  11. public var previous(get,never):Null<Exception>;
  12. public var native(get,never):Any;
  13. @:noCompletion var __exceptionStack:Null<CallStack>;
  14. @:noCompletion var __nativeStack:Array<PyStackItem>;
  15. @:noCompletion @:ifFeature("haxe.Exception.get_stack") var __skipStack:Int = 0;
  16. @:noCompletion var __nativeException:BaseException;
  17. @:noCompletion var __previousException:Null<Exception>;
  18. static function caught(value:Any):Exception {
  19. if(Std.isOfType(value, Exception)) {
  20. return value;
  21. } else if(Std.isOfType(value, BaseException)) {
  22. return new Exception(UBuiltins.str(value), null, value);
  23. } else {
  24. return new ValueException(value, null, value);
  25. }
  26. }
  27. static function thrown(value:Any):Any {
  28. if(Std.isOfType(value, Exception)) {
  29. return (value:Exception).native;
  30. } else if(Std.isOfType(value, BaseException)) {
  31. return value;
  32. } else {
  33. var e = new ValueException(value);
  34. e.__shiftStack();
  35. return e;
  36. }
  37. }
  38. public function new(message:String, ?previous:Exception, ?native:Any) {
  39. super(message);
  40. this.__previousException = previous;
  41. if(native != null && Std.isOfType(native, BaseException)) {
  42. __nativeException = native;
  43. __nativeStack = NativeStackTrace.exceptionStack();
  44. } else {
  45. __nativeException = cast this;
  46. __nativeStack = NativeStackTrace.callStack();
  47. }
  48. }
  49. function unwrap():Any {
  50. return __nativeException;
  51. }
  52. public function toString():String {
  53. return message;
  54. }
  55. public function details():String {
  56. return inline CallStack.exceptionToString(this);
  57. }
  58. @:noCompletion
  59. @:ifFeature("haxe.Exception.get_stack")
  60. inline function __shiftStack():Void {
  61. __skipStack++;
  62. }
  63. function get_message():String {
  64. return UBuiltins.str(this);
  65. }
  66. function get_previous():Null<Exception> {
  67. return __previousException;
  68. }
  69. final function get_native():Any {
  70. return __nativeException;
  71. }
  72. function get_stack():CallStack {
  73. return switch __exceptionStack {
  74. case null:
  75. __exceptionStack = NativeStackTrace.toHaxe(__nativeStack, __skipStack);
  76. case s: s;
  77. }
  78. }
  79. }