You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* global __filename, Strophe */
  2. /**
  3. * Strophe logger implementation. Logs from level WARN and above.
  4. */
  5. import {getLogger} from "jitsi-meet-logger";
  6. const logger = getLogger(__filename);
  7. import GlobalOnErrorHandler from "../util/GlobalOnErrorHandler";
  8. /**
  9. * This is the last HTTP error status captured from Strophe debug logs.
  10. * The purpose of storing it is to distinguish between the network and
  11. * infrastructure reason for connection being dropped (see connectionHandler in
  12. * xmpp.js). The value will be cleared (-1) if the subsequent request succeeds
  13. * which means that the failure could be transient.
  14. *
  15. * FIXME in the latest Strophe (not released on npm) there is API to handle
  16. * particular HTTP errors, but there is no way to learn if the subsequent
  17. * request succeeded in order to tell if the error was one time incident or if
  18. * it was the reason for dropping the connection by Strophe (the connection is
  19. * dropped after 5 subsequent failures). Ideally Strophe should provide more
  20. * details about the reason on why the connection stopped.
  21. *
  22. * @type {number}
  23. */
  24. let lastErrorStatus = -1;
  25. /**
  26. * A regular expression used to catch Strophe's log message indicating that the
  27. * last BOSH request was successful. When there is such message seen the
  28. * {@link lastErrorStatus} will be set back to '-1'.
  29. * @type {RegExp}
  30. */
  31. const resetLastErrorStatusRegExpr = /request id \d+.\d+ got 200/;
  32. /**
  33. * A regular expression used to capture the current value of the BOSH request
  34. * error status (HTTP error code or '0' or something else).
  35. * @type {RegExp}
  36. */
  37. const lastErrorStatusRegExpr
  38. = /request errored, status: (\d+), number of errors: \d+/;
  39. export default function () {
  40. Strophe.log = function (level, msg) {
  41. // Our global handler reports uncaught errors to the stats which may
  42. // interpret those as partial call failure.
  43. // Strophe log entry about secondary request timeout does not mean that
  44. // it's a final failure(the request will be restarted), so we lower it's
  45. // level here to a warning.
  46. logger.trace("Strophe", level, msg);
  47. if (typeof msg === 'string' &&
  48. msg.indexOf("Request ") !== -1 &&
  49. msg.indexOf("timed out (secondary), restarting") !== -1) {
  50. level = Strophe.LogLevel.WARN;
  51. }
  52. /* eslint-disable no-case-declarations */
  53. switch (level) {
  54. case Strophe.LogLevel.DEBUG:
  55. // The log message which reports successful status is logged
  56. // on Strophe's DEBUG level
  57. if (lastErrorStatus !== -1 &&
  58. resetLastErrorStatusRegExpr.test(msg)) {
  59. logger.debug("Reset lastErrorStatus");
  60. lastErrorStatus = -1;
  61. }
  62. break;
  63. case Strophe.LogLevel.WARN:
  64. logger.warn("Strophe: " + msg);
  65. const errStatusCapture = lastErrorStatusRegExpr.exec(msg);
  66. if (errStatusCapture && errStatusCapture.length === 2) {
  67. lastErrorStatus = parseInt(errStatusCapture[1]);
  68. logger.debug(
  69. "lastErrorStatus set to: " + lastErrorStatus);
  70. }
  71. break;
  72. case Strophe.LogLevel.ERROR:
  73. case Strophe.LogLevel.FATAL:
  74. msg = "Strophe: " + msg;
  75. GlobalOnErrorHandler.callErrorHandler(new Error(msg));
  76. logger.error(msg);
  77. break;
  78. }
  79. /* eslint-enable no-case-declarations */
  80. };
  81. /**
  82. * Returns error status (HTTP error code) of the last BOSH request.
  83. *
  84. * @return {number} HTTP error code, '0' for unknown or "god knows what"
  85. * (this is a hack).
  86. */
  87. Strophe.getLastErrorStatus = function () {
  88. return lastErrorStatus;
  89. };
  90. Strophe.getStatusString = function (status) {
  91. switch (status) {
  92. case Strophe.Status.ERROR:
  93. return "ERROR";
  94. case Strophe.Status.CONNECTING:
  95. return "CONNECTING";
  96. case Strophe.Status.CONNFAIL:
  97. return "CONNFAIL";
  98. case Strophe.Status.AUTHENTICATING:
  99. return "AUTHENTICATING";
  100. case Strophe.Status.AUTHFAIL:
  101. return "AUTHFAIL";
  102. case Strophe.Status.CONNECTED:
  103. return "CONNECTED";
  104. case Strophe.Status.DISCONNECTED:
  105. return "DISCONNECTED";
  106. case Strophe.Status.DISCONNECTING:
  107. return "DISCONNECTING";
  108. case Strophe.Status.ATTACHED:
  109. return "ATTACHED";
  110. default:
  111. return "unknown";
  112. }
  113. };
  114. }