Exception.hx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package haxe;
  2. import flash.errors.Error;
  3. @:coreApi
  4. class Exception extends NativeException {
  5. public var message(get,never):String;
  6. public var stack(get,never):CallStack;
  7. public var previous(get,never):Null<Exception>;
  8. public var native(get,never):Any;
  9. @:noCompletion var __exceptionStack:Null<CallStack>;
  10. @:noCompletion var __nativeStack:String;
  11. @:noCompletion @:ifFeature("haxe.Exception.get_stack") var __skipStack:Int;
  12. @:noCompletion var __nativeException:Error;
  13. @:noCompletion var __previousException:Null<Exception>;
  14. static function caught(value:Any):Exception {
  15. if(Std.isOfType(value, Exception)) {
  16. return value;
  17. } else if(Std.isOfType(value, Error)) {
  18. return new Exception((value:Error).message, null, value);
  19. } else {
  20. return new ValueException(value, null, value);
  21. }
  22. }
  23. static function thrown(value:Any):Any {
  24. if(Std.isOfType(value, Exception)) {
  25. return (value:Exception).native;
  26. } else if(Std.isOfType(value, Error)) {
  27. return value;
  28. } else {
  29. var e = new ValueException(value);
  30. e.__shiftStack();
  31. return e;
  32. }
  33. }
  34. public function new(message:String, ?previous:Exception, ?native:Any) {
  35. super(message);
  36. __previousException = previous;
  37. if(native != null && Std.isOfType(native, Error)) {
  38. __nativeException = native;
  39. __nativeStack = NativeStackTrace.normalize((native:Error).getStackTrace());
  40. } else {
  41. __nativeException = cast this;
  42. __nativeStack = NativeStackTrace.callStack();
  43. }
  44. }
  45. function unwrap():Any {
  46. return __nativeException;
  47. }
  48. public function toString():String {
  49. return message;
  50. }
  51. public function details():String {
  52. return inline CallStack.exceptionToString(this);
  53. }
  54. @:noCompletion
  55. @:ifFeature("haxe.Exception.get_stack")
  56. inline function __shiftStack():Void {
  57. __skipStack++;
  58. }
  59. function get_message():String {
  60. return (cast this:Error).message;
  61. }
  62. function get_previous():Null<Exception> {
  63. return __previousException;
  64. }
  65. final function get_native():Any {
  66. return __nativeException;
  67. }
  68. function get_stack():CallStack {
  69. return switch __exceptionStack {
  70. case null:
  71. __exceptionStack = NativeStackTrace.toHaxe(__nativeStack, __skipStack);
  72. case s: s;
  73. }
  74. }
  75. }
  76. @:dox(hide)
  77. @:native('flash.errors.Error')
  78. extern class NativeException {
  79. @:noCompletion @:flash.property private var errorID(get,never):Int;
  80. // @:noCompletion private var message:Dynamic;
  81. @:noCompletion private var name:Dynamic;
  82. @:noCompletion private function new(?message:Dynamic, id:Dynamic = 0):Void;
  83. @:noCompletion private function getStackTrace():String;
  84. @:noCompletion private function get_errorID():Int;
  85. }