Exception.hx 2.8 KB

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