Common.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Errors not related to the Telegram API itself
  3. */
  4. const struct = require('python-struct')
  5. /**
  6. * Occurs when a read operation was cancelled.
  7. */
  8. class ReadCancelledError extends Error {
  9. constructor() {
  10. super('The read operation was cancelled.')
  11. }
  12. }
  13. /**
  14. * Occurs when a type is not found, for example,
  15. * when trying to read a TLObject with an invalid constructor code.
  16. */
  17. class TypeNotFoundError extends Error {
  18. constructor(invalidConstructorId, remaining) {
  19. super(`Could not find a matching Constructor ID for the TLObject that was supposed to be
  20. read with ID ${invalidConstructorId}. Most likely, a TLObject was trying to be read when
  21. it should not be read. Remaining bytes: ${remaining}`)
  22. this.invalidConstructorId = invalidConstructorId
  23. this.remaining = remaining
  24. }
  25. }
  26. /**
  27. * Occurs when using the TCP full mode and the checksum of a received
  28. * packet doesn't match the expected checksum.
  29. */
  30. class InvalidChecksumError extends Error {
  31. constructor(checksum, validChecksum) {
  32. super(`Invalid checksum (${checksum} when ${validChecksum} was expected). This packet should be skipped.`)
  33. this.checksum = checksum
  34. this.validChecksum = validChecksum
  35. }
  36. }
  37. /**
  38. * Occurs when the buffer is invalid, and may contain an HTTP error code.
  39. * For instance, 404 means "forgotten/broken authorization key", while
  40. */
  41. class InvalidBufferError extends Error {
  42. constructor(payload) {
  43. console.log("paerlkaelm rkae",payload)
  44. const code = -(struct.unpack('<i', payload)[0])
  45. if (payload.length === 4) {
  46. super(`Invalid response buffer (HTTP code ${code})`)
  47. } else {
  48. super(`Invalid response buffer (too short ${payload})`)
  49. this.code = null
  50. }
  51. this.code = code
  52. this.payload = payload
  53. }
  54. }
  55. /**
  56. * Generic security error, mostly used when generating a new AuthKey.
  57. */
  58. class SecurityError extends Error {
  59. constructor(...args) {
  60. if (!args.length) {
  61. args = ['A security check failed.']
  62. }
  63. super(...args)
  64. }
  65. }
  66. /**
  67. * Occurs when there's a hash mismatch between the decrypted CDN file
  68. * and its expected hash.
  69. */
  70. class CdnFileTamperedError extends SecurityError {
  71. constructor() {
  72. super('The CDN file has been altered and its download cancelled.')
  73. }
  74. }
  75. /**
  76. * Occurs when another exclusive conversation is opened in the same chat.
  77. */
  78. class AlreadyInConversationError extends Error {
  79. constructor() {
  80. super('Cannot open exclusive conversation in a chat that already has one open conversation')
  81. }
  82. }
  83. /**
  84. * Occurs when handling a badMessageNotification
  85. */
  86. class BadMessageError extends Error {
  87. static ErrorMessages = {
  88. 16:
  89. 'msg_id too low (most likely, client time is wrong it would be worthwhile to ' +
  90. 'synchronize it using msg_id notifications and re-send the original message ' +
  91. 'with the “correct” msg_id or wrap it in a container with a new msg_id if the ' +
  92. 'original message had waited too long on the client to be transmitted).',
  93. 17:
  94. 'msg_id too high (similar to the previous case, the client time has to be ' +
  95. 'synchronized, and the message re-sent with the correct msg_id).',
  96. 18:
  97. 'Incorrect two lower order msg_id bits (the server expects client message msg_id ' +
  98. 'to be divisible by 4).',
  99. 19: 'Container msg_id is the same as msg_id of a previously received message ' + '(this must never happen).',
  100. 20:
  101. 'Message too old, and it cannot be verified whether the server has received a ' +
  102. 'message with this msg_id or not.',
  103. 32:
  104. 'msg_seqno too low (the server has already received a message with a lower ' +
  105. 'msg_id but with either a higher or an equal and odd seqno).',
  106. 33:
  107. 'msg_seqno too high (similarly, there is a message with a higher msg_id but with ' +
  108. 'either a lower or an equal and odd seqno).',
  109. 34: 'An even msg_seqno expected (irrelevant message), but odd received.',
  110. 35: 'Odd msg_seqno expected (relevant message), but even received.',
  111. 48:
  112. 'Incorrect server salt (in this case, the bad_server_salt response is received with ' +
  113. 'the correct salt, and the message is to be re-sent with it).',
  114. 64: 'Invalid container.',
  115. }
  116. constructor(code) {
  117. super(BadMessageError.ErrorMessages[code] || `Unknown error code (this should not happen): ${code}.`)
  118. this.code = code
  119. }
  120. }
  121. // TODO : Support multi errors.
  122. module.exports = {
  123. ReadCancelledError,
  124. TypeNotFoundError,
  125. InvalidChecksumError,
  126. InvalidBufferError,
  127. SecurityError,
  128. CdnFileTamperedError,
  129. AlreadyInConversationError,
  130. BadMessageError,
  131. }