PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/functions/errorfunc/trigger_error.js

http://github.com/kvz/phpjs
JavaScript | 118 lines | 86 code | 4 blank | 28 comment | 25 complexity | ac3af71c95dbf73bb26f0022e295b044 MD5 | raw file
  1. function trigger_error (error_msg, error_type) {
  2. // http://kevin.vanzonneveld.net
  3. // + original by: Brett Zamir (http://brett-zamir.me)
  4. // % note 1: Although this function should only allow the E_USER_ types, we'll allow the
  5. // % note 1: others here in order to be able to simulate more types (though should not trigger
  6. // % note 1: aggregates like E_STRICT or E_ALL).
  7. // % note 1: See also our experimental at() function (to mimic the @ error suppressor)
  8. // - depends on: echo
  9. // * example 1: trigger_error('This will just be a notice');
  10. // * returns 1: true
  11. // Fix: get to work with set_error_handler()'s handler when that is added
  12. var type = 0,
  13. i = 0,
  14. that = this,
  15. prepend = '',
  16. append = '';
  17. if (!error_type) {
  18. error_type = 'E_USER_NOTICE';
  19. }
  20. var ini_on = function (ini) {
  21. return that.php_js.ini[ini] && that.php_js.ini[ini].local_value && ((that.php_js.ini[ini].local_value.toString && that.php_js.ini[ini].local_value.toString().toLowerCase && (that.php_js.ini[ini].local_value.toString().toLowerCase() === 'on' || that.php_js.ini[ini].local_value.toString().toLowerCase() === 'true')) || parseInt(that.php_js.ini[ini].local_value, 10) === 1);
  22. };
  23. var display_errors = function (type) {
  24. return that.php_js.ini.error_reporting && (type & that.php_js.ini.error_reporting.local_value) && ini_on('display_errors');
  25. };
  26. var TYPES = { // Including all types for completeness, but should not trigger aggregates like E_STRICT or E_ALL
  27. E_ERROR: 1,
  28. // Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.
  29. E_WARNING: 2,
  30. // Run-time warnings (non-fatal errors). Execution of the script is not halted.
  31. E_PARSE: 4,
  32. // Compile-time parse errors. Parse errors should only be generated by the parser.
  33. E_NOTICE: 8,
  34. // Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
  35. E_CORE_ERROR: 16,
  36. // Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP.
  37. E_CORE_WARNING: 32,
  38. // Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP.
  39. E_COMPILE_ERROR: 64,
  40. // Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine.
  41. E_COMPILE_WARNING: 128,
  42. // Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine.
  43. E_USER_ERROR: 256,
  44. // User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error().
  45. E_USER_WARNING: 512,
  46. // User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error().
  47. E_USER_NOTICE: 1024,
  48. // User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error().
  49. E_STRICT: 2048,
  50. // Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
  51. E_RECOVERABLE_ERROR: 4096,
  52. // Catchable fatal error. It indicates that a probably dangerous error occured, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR.
  53. E_DEPRECATED: 8192,
  54. // Run-time notices. Enable this to receive warnings about code that will not work in future versions.
  55. E_USER_DEPRECATED: 16384,
  56. // User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error().
  57. E_ALL: 30719 // All errors and warnings, as supported, except of level E_STRICT in PHP < 6. in:32767, // PHP 6, in:30719, // PHP 5.3.x, in:6143, // PHP 5.2.x, previously:2047, //
  58. };
  59. if (typeof error_type === 'number') {
  60. type = error_type;
  61. } else { // Allow for a single string or an array of string flags
  62. error_type = [].concat(error_type);
  63. for (i = 0; i < error_type.length; i++) {
  64. // Resolve string input to bitwise
  65. if (TYPES[error_type[i]]) {
  66. type = type | TYPES[error_type[i]];
  67. }
  68. }
  69. }
  70. // BEGIN REDUNDANT
  71. this.php_js = this.php_js || {};
  72. this.php_js.ini = this.php_js.ini || {};
  73. // END REDUNDANT
  74. if (type & TYPES.E_USER_ERROR || type & TYPES.E_ERROR || type & TYPES.E_CORE_ERROR || type & TYPES.E_COMPILE_ERROR || type & TYPES.E_RECOVERABLE_ERROR || type & TYPES.E_PARSE) {
  75. if (ini_on('track_errors')) {
  76. this.$php_errormsg = error_msg; // Can assign to this global, as in PHP (see http://php.net/manual/en/reserved.variables.phperrormsg.php )
  77. }
  78. if (display_errors(type)) {
  79. prepend = this.php_js.ini.error_prepend_string ? this.php_js.ini.error_prepend_string : '';
  80. append = this.php_js.ini.error_append_string ? this.php_js.ini.error_append_string : '';
  81. this.echo(prepend + 'Error: ' + error_msg + ' ' + append);
  82. }
  83. var e = new Error(error_msg); // Might, for Mozilla, allow to somehow pass in a fileName and lineNumber (2nd and 3rd arguments to Error)
  84. e.type = type;
  85. this.php_js.last_error = {
  86. message: e.message,
  87. file: e.fileName,
  88. line: e.lineNumber,
  89. type: e.type
  90. }; // fileName and lineNumber presently not working (see note just above)
  91. throw e;
  92. }
  93. if (display_errors(type)) {
  94. switch (type) {
  95. case TYPES.E_USER_WARNING:
  96. case TYPES.E_WARNING:
  97. case TYPES.E_CORE_WARNING:
  98. case TYPES.E_COMPILE_WARNING:
  99. this.echo('Warning: ' + error_msg);
  100. break;
  101. case TYPES.E_USER_NOTICE:
  102. case TYPES.E_NOTICE:
  103. this.echo('Notice: ' + error_msg);
  104. break;
  105. case TYPES.E_DEPRECATED:
  106. case TYPES.E_USER_DEPRECATED:
  107. this.echo('Deprecated: ' + error_msg);
  108. break;
  109. default:
  110. throw 'Unrecognized error type';
  111. }
  112. }
  113. return true;
  114. }