2
0

Exception.hx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package haxe;
  2. //TODO: extend ::std::exception
  3. @:coreApi
  4. class Exception {
  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 __exceptionMessage:String;
  10. @:noCompletion var __exceptionStack:Null<CallStack>;
  11. @:noCompletion var __nativeStack:Array<String>;
  12. @:noCompletion @:ifFeature("haxe.Exception.get_stack") var __skipStack:Int = 0;
  13. @:noCompletion var __nativeException:Any;
  14. @:noCompletion var __previousException:Null<Exception>;
  15. static function caught(value:Any):Exception {
  16. if(Std.isOfType(value, Exception)) {
  17. return value;
  18. } else {
  19. var e = new ValueException(value, null, value);
  20. // Undo automatic __shiftStack()
  21. e.__unshiftStack();
  22. return e;
  23. }
  24. }
  25. static function thrown(value:Any):Any {
  26. if(Std.isOfType(value, Exception)) {
  27. return (value:Exception).native;
  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. __exceptionMessage = message;
  36. __previousException = previous;
  37. if(native != null) {
  38. __nativeStack = NativeStackTrace.exceptionStack();
  39. __nativeException = native;
  40. } else {
  41. __nativeStack = NativeStackTrace.callStack();
  42. __nativeException = this;
  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. @:noCompletion
  60. @:ifFeature("haxe.Exception.get_stack")
  61. inline function __unshiftStack():Void {
  62. __skipStack--;
  63. }
  64. function get_message():String {
  65. return __exceptionMessage;
  66. }
  67. function get_previous():Null<Exception> {
  68. return __previousException;
  69. }
  70. final function get_native():Any {
  71. return __nativeException;
  72. }
  73. function get_stack():CallStack {
  74. return switch __exceptionStack {
  75. case null: __exceptionStack = NativeStackTrace.toHaxe(__nativeStack, __skipStack);
  76. case s: s;
  77. }
  78. }
  79. }