PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/error.prototype.js

https://bitbucket.org/marcuspope/verbotenjs
JavaScript | 153 lines | 98 code | 24 blank | 31 comment | 27 complexity | f0f7954be4e1799cebc707bb7bb02004 MD5 | raw file
  1. /*
  2. ////////////////////////////////////////////////////////////////////////////////
  3. ______ ____ __ __
  4. / ____/_____________ _____ / __ \_________ / /_____ / /___ ______ ___ _____
  5. / __/ / ___/ ___/ __ \/ ___/ / /_/ / ___/ __ \/ __/ __ \/ __/ / / / __ \/ _ \/ ___/
  6. / /___/ / / / / /_/ / / / ____/ / / /_/ / /_/ /_/ / /_/ /_/ / /_/ / __(__ )
  7. /_____/_/ /_/ \____/_/ /_/ /_/ \____/\__/\____/\__/\__, / .___/\___/____/
  8. /____/_/
  9. ////////////////////////////////////////////////////////////////////////////////
  10. */
  11. (function() {
  12. Error.prototype.matches = function(err) {
  13. if (err.isNumber()) return this.number == err;
  14. if (err.isError()) return this.number == err.number;
  15. };
  16. Error.prototype.toString = function() {
  17. //Returns a better string representation of an error object
  18. //@todo: enhance logic for browser based stack traces and information.
  19. //@ref: https://github.com/occ/TraceKit
  20. //@note: this used to include a stack trace, but that spins JSON.stringify into an IL,
  21. // if you'd like to view a stack trace, just use console.trace() instead
  22. return 'ERROR: ' + this.message;
  23. };
  24. Error.prototype.trace = function() {
  25. //@desc: returns the stack trace as an array of function names
  26. if (this.stack) {
  27. return this.stack.lines();
  28. }
  29. else {
  30. var cur = arguments.callee.caller;
  31. var stack = [];
  32. var i = 50;
  33. while(cur && i--) {
  34. stack.push(cur.id());
  35. cur = cur.caller;
  36. }
  37. return stack;
  38. }
  39. };
  40. Error.prototype.ignore = function() {
  41. //returns true for known "non-errors" used for workflow purposes
  42. if (this.description == "break" || this.exit == true) {
  43. return true;
  44. }
  45. };
  46. Error.prototype.debug = function(code) {
  47. //prompt user for debugger hook
  48. code = code || "[no code provided]";
  49. //@todo: need to implement with callback handler because that's how nodejs does confirms, until then I'm disabling it.
  50. if (false && confirm("An error has occurred:\n\n" + this.toString() + "\n\n" + code + "\n\nWould you like to debug?")) {
  51. //if the user clicks yes - hit debug statement
  52. debugger;
  53. return true;
  54. }
  55. return false;
  56. };
  57. Error.prototype.bubble = function() {
  58. if (this.bbl) {
  59. this.count--;
  60. if (this.count <= 0) {
  61. return true;
  62. }
  63. throw this;
  64. }
  65. };
  66. Error.prototype.handle = function() {
  67. //attempts to handle all errors
  68. //will return undefined if nothing further to do
  69. //will return a value if the error was intended to return a value (each iterators)
  70. //will rethrow the error if it was intended to be reported via the .report attribute
  71. if (this.ignore()) {
  72. return this.retval;
  73. }
  74. if (this.bubble()) {
  75. return this.retval;
  76. }
  77. if (this.debug()) return null;
  78. if (this.die) {
  79. throw this; //Keep throwing until we die!
  80. }
  81. if (this.report != null || this.die) {
  82. delete this.report;
  83. throw this;
  84. }
  85. else {
  86. if (gc.debug) {
  87. alert(this.toString());
  88. }
  89. }
  90. return true;
  91. };
  92. Error.toss = function(m) {
  93. //alias for throwing a new exception
  94. throw new Error(m);
  95. };
  96. Error.prototype.toss = function(m) {
  97. //A gentler type of throw
  98. //Designed to throw a newly defined Error inside things like var definitions or array indicies
  99. this.report = true;
  100. this.extra = m;
  101. this.handle();
  102. };
  103. Error.prototype.pass = function(f) {
  104. //pass an error to the provided error handling function {f}
  105. if (f) {
  106. f(this);
  107. }
  108. };
  109. Object.prototype.pass =
  110. Object.prototype.toss = function() {
  111. //noop: this is designed to reduce complexity of the node framework's error handling architecture
  112. };
  113. Error.prototype.print = function() {
  114. return " ERROR: " + this.number + " - " + (this.description || this.message);
  115. };
  116. Error.notImplemented = function() {
  117. //helper error for functions yet to be implemented
  118. Error.toss("Function Not Implemented: " + closure().caller.id());
  119. };
  120. Error.invalidArgument = function() {
  121. Error.toss(
  122. "Function " + closure().caller.id() + " does not implement logic for parameters: " +
  123. arguments.ea(function(v) {
  124. return v.isa() + " : " + v.serialize();
  125. }).join("\r\n")
  126. );
  127. };
  128. })();