PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/q/error.prototype.js

https://bitbucket.org/marcuspope/q.node
JavaScript | 123 lines | 77 code | 19 blank | 27 comment | 23 complexity | 0daccb7c2c6ab3c84700521463599dfc MD5 | raw file
Possible License(s): Unlicense
  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: branch logic for browser based error messages
  19. return '\n' +
  20. '\nERROR: ' + this.message +
  21. '\nSTACK: ' +
  22. this.stack;
  23. };
  24. Error.prototype.ignore = function() {
  25. //returns true for known "non-errors" used for workflow purposes
  26. if (this.description == "break" || this.exit == true) {
  27. return true;
  28. }
  29. };
  30. Error.prototype.debug = function(code) {
  31. //prompt user for debugger hook
  32. code = code || "[no code provided]";
  33. //BUGFIX: need to implement with callback handler, until then disabled
  34. if (false && confirm("An error has occurred:\n\n" + this.toString() + "\n\n" + code + "\n\nWould you like to debug?")) {
  35. //if the user clicks yes - hit debug statement
  36. debugger;
  37. return true;
  38. }
  39. return false;
  40. };
  41. Error.prototype.bubble = function() {
  42. if (this.bbl) {
  43. this.count--;
  44. if (this.count <= 0) {
  45. return true;
  46. }
  47. throw this;
  48. }
  49. };
  50. Error.prototype.handle = function() {
  51. //attempts to handle all errors
  52. //will return undefined if nothing further to do
  53. //will return a value if the error was intended to return a value (each iterators)
  54. //will rethrow the error if it was intended to be reported via the .report attribute
  55. if (this.ignore()) {
  56. return this.retval;
  57. }
  58. if (this.bubble()) {
  59. return this.retval;
  60. }
  61. if (this.debug()) return;
  62. if (this.die) {
  63. throw this; //Keep throwing until we die!
  64. }
  65. if (this.report != null || this.die) {
  66. delete this.report;
  67. throw this;
  68. }
  69. else {
  70. if (q.debug) {
  71. alert(this.toString());
  72. }
  73. }
  74. };
  75. Error.toss = function(m) {
  76. //alias for throwing a new exception
  77. throw new Error(m);
  78. };
  79. Error.prototype.toss = function(m) {
  80. //A gentler type of throw
  81. //Designed to throw a newly defined Error inside things like var definitions or array indicies
  82. this.report = true;
  83. this.extra = m;
  84. this.handle();
  85. };
  86. Error.prototype.pass = function(f) {
  87. //pass an error to the provided error handling function {f}
  88. if (f) {
  89. f(this);
  90. }
  91. };
  92. Object.prototype.pass =
  93. Object.prototype.toss = function() {
  94. //noop: this is designed to reduce complexity of the node framework's error handling architecture
  95. };
  96. Error.prototype.print = function() {
  97. return " ERROR: " + this.number + " - " + this.description;
  98. };
  99. Error.notImplemented = function() {
  100. //helper error for functions yet to be implemented
  101. Error.toss("Function Not Implemented: " + q.me().caller.id());
  102. };
  103. })();