errors.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * vim: ts=4:sw=4:expandtab
  3. */
  4. ;(function() {
  5. 'use strict';
  6. var registeredFunctions = {};
  7. var Type = {
  8. ENCRYPT_MESSAGE: 1,
  9. INIT_SESSION: 2,
  10. TRANSMIT_MESSAGE: 3,
  11. REBUILD_MESSAGE: 4,
  12. RETRY_SEND_MESSAGE_PROTO: 5
  13. };
  14. window.textsecure = window.textsecure || {};
  15. window.textsecure.replay = {
  16. Type: Type,
  17. registerFunction: function(func, functionCode) {
  18. registeredFunctions[functionCode] = func;
  19. }
  20. };
  21. function ReplayableError(options) {
  22. options = options || {};
  23. this.name = options.name || 'ReplayableError';
  24. this.functionCode = options.functionCode;
  25. this.args = options.args;
  26. }
  27. ReplayableError.prototype = new Error();
  28. ReplayableError.prototype.constructor = ReplayableError;
  29. ReplayableError.prototype.replay = function() {
  30. return registeredFunctions[this.functionCode].apply(window, this.args);
  31. };
  32. function IncomingIdentityKeyError(number, message, key) {
  33. ReplayableError.call(this, {
  34. functionCode : Type.INIT_SESSION,
  35. args : [number, message]
  36. });
  37. this.number = number.split('.')[0];
  38. this.name = 'IncomingIdentityKeyError';
  39. this.message = "The identity of " + this.number + " has changed.";
  40. this.identityKey = key;
  41. }
  42. IncomingIdentityKeyError.prototype = new ReplayableError();
  43. IncomingIdentityKeyError.prototype.constructor = IncomingIdentityKeyError;
  44. function OutgoingIdentityKeyError(number, message, timestamp, identityKey) {
  45. ReplayableError.call(this, {
  46. functionCode : Type.ENCRYPT_MESSAGE,
  47. args : [number, message, timestamp]
  48. });
  49. this.number = number.split('.')[0];
  50. this.name = 'OutgoingIdentityKeyError';
  51. this.message = "The identity of " + this.number + " has changed.";
  52. this.identityKey = identityKey;
  53. }
  54. OutgoingIdentityKeyError.prototype = new ReplayableError();
  55. OutgoingIdentityKeyError.prototype.constructor = OutgoingIdentityKeyError;
  56. function OutgoingMessageError(number, message, timestamp, httpError) {
  57. ReplayableError.call(this, {
  58. functionCode : Type.ENCRYPT_MESSAGE,
  59. args : [number, message, timestamp]
  60. });
  61. this.name = 'OutgoingMessageError';
  62. if (httpError) {
  63. this.code = httpError.code;
  64. this.message = httpError.message;
  65. this.stack = httpError.stack;
  66. }
  67. }
  68. OutgoingMessageError.prototype = new ReplayableError();
  69. OutgoingMessageError.prototype.constructor = OutgoingMessageError;
  70. function SendMessageNetworkError(number, jsonData, httpError, timestamp) {
  71. ReplayableError.call(this, {
  72. functionCode : Type.TRANSMIT_MESSAGE,
  73. args : [number, jsonData, timestamp]
  74. });
  75. this.name = 'SendMessageNetworkError';
  76. this.number = number;
  77. this.code = httpError.code;
  78. this.message = httpError.message;
  79. this.stack = httpError.stack;
  80. }
  81. SendMessageNetworkError.prototype = new ReplayableError();
  82. SendMessageNetworkError.prototype.constructor = SendMessageNetworkError;
  83. function SignedPreKeyRotationError(numbers, message, timestamp) {
  84. ReplayableError.call(this, {
  85. functionCode : Type.RETRY_SEND_MESSAGE_PROTO,
  86. args : [numbers, message, timestamp]
  87. });
  88. this.name = 'SignedPreKeyRotationError';
  89. this.message = "Too many signed prekey rotation failures";
  90. }
  91. SignedPreKeyRotationError.prototype = new ReplayableError();
  92. SignedPreKeyRotationError.prototype.constructor = SignedPreKeyRotationError;
  93. function MessageError(message, httpError) {
  94. ReplayableError.call(this, {
  95. functionCode : Type.REBUILD_MESSAGE,
  96. args : [message]
  97. });
  98. this.name = 'MessageError';
  99. this.code = httpError.code;
  100. this.message = httpError.message;
  101. this.stack = httpError.stack;
  102. }
  103. MessageError.prototype = new ReplayableError();
  104. MessageError.prototype.constructor = MessageError;
  105. function UnregisteredUserError(number, httpError) {
  106. this.name = 'UnregisteredUserError';
  107. this.number = number;
  108. this.code = httpError.code;
  109. this.message = httpError.message;
  110. this.stack = httpError.stack;
  111. }
  112. UnregisteredUserError.prototype = new Error();
  113. UnregisteredUserError.prototype.constructor = UnregisteredUserError;
  114. window.textsecure.UnregisteredUserError = UnregisteredUserError;
  115. window.textsecure.SendMessageNetworkError = SendMessageNetworkError;
  116. window.textsecure.IncomingIdentityKeyError = IncomingIdentityKeyError;
  117. window.textsecure.OutgoingIdentityKeyError = OutgoingIdentityKeyError;
  118. window.textsecure.ReplayableError = ReplayableError;
  119. window.textsecure.OutgoingMessageError = OutgoingMessageError;
  120. window.textsecure.MessageError = MessageError;
  121. window.textsecure.SignedPreKeyRotationError = SignedPreKeyRotationError;
  122. })();