PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/js/error.js

https://github.com/julienbarrier/jappix
JavaScript | 139 lines | 73 code | 28 blank | 38 comment | 18 complexity | f4592aad4911085100fffed8239e7620 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*
  2. Jappix - An open social platform
  3. These are the error functions for Jappix
  4. -------------------------------------------------
  5. License: AGPL
  6. Author: Vanaryon
  7. Last revision: 02/04/11
  8. */
  9. // Shows the given error output
  10. function showError(condition, reason, type) {
  11. // Enough data to output the error
  12. if(condition || reason) {
  13. // Initialize the error text
  14. var eText = '';
  15. // Any error condition
  16. if(condition)
  17. eText += condition;
  18. // Any error type
  19. if(type && eText)
  20. eText += ' (' + type + ')';
  21. // Any error reason
  22. if(reason) {
  23. if(eText)
  24. eText += ' - ';
  25. eText += reason;
  26. }
  27. // We reveal the error
  28. openThisError(1);
  29. // Create the error text
  30. $('#board .one-board.error[data-id=1] span').text(eText);
  31. }
  32. // Not enough data to output the error: output a generic board
  33. else
  34. openThisError(2);
  35. }
  36. // Handles the error from a packet and return true if any error
  37. function handleError(packet) {
  38. /* REF: http://xmpp.org/extensions/xep-0086.html */
  39. // Initialize
  40. var type, code, reason, condition;
  41. var node = $(packet);
  42. // First level error (connection error)
  43. if(node.is('error')) {
  44. // Get the value
  45. code = node.attr('code');
  46. // Specific error reason
  47. switch(code) {
  48. case '401':
  49. reason = _e("Authorization failed");
  50. break;
  51. case '409':
  52. reason = _e("Registration failed, please choose a different username");
  53. break;
  54. case '503':
  55. reason = _e("Service unavailable");
  56. break;
  57. case '500':
  58. reason = _e("Internal server error, try later");
  59. break;
  60. default:
  61. reason = node.find('text').text();
  62. break;
  63. }
  64. // Remove the general wait item (security)
  65. removeGeneralWait();
  66. // Show reconnect pane
  67. if(CURRENT_SESSION && CONNECTED) {
  68. // Anonymous?
  69. if(isAnonymous())
  70. createReconnect('anonymous');
  71. else
  72. createReconnect('normal');
  73. }
  74. // Show the homepage (security)
  75. else if(!CURRENT_SESSION || !CONNECTED) {
  76. $('#home').show();
  77. pageTitle('home');
  78. }
  79. // Still connected? (security)
  80. if(isConnected())
  81. con.disconnect();
  82. logThis('First level error received.', 1);
  83. }
  84. // Second level error (another error)
  85. else if(node.find('error').size()) {
  86. type = node.find('error').attr('type');
  87. reason = node.find('error text').text();
  88. condition = packet.getElementsByTagName('error').item(0).childNodes.item(0).nodeName.replace(/-/g, ' ');
  89. logThis('Second level error received.', 1);
  90. }
  91. // No error
  92. else
  93. return false;
  94. // Show the error board
  95. showError(condition, reason, type);
  96. // Return there's an error
  97. return true;
  98. }
  99. // Handles the error reply of a packet
  100. function handleErrorReply(packet) {
  101. return handleError(packet.getNode());
  102. }
  103. // Handles the error reply for a message
  104. function handleMessageError(packet) {
  105. if(!handleErrorReply(packet))
  106. handleMessage(packet);
  107. }