refactor(client): normalized MtCuteTypeAssertionError errors

This commit is contained in:
teidesu 2021-05-12 18:16:50 +03:00
parent 9be7b0d6c9
commit 30253c83c7
18 changed files with 42 additions and 28 deletions

View file

@ -21,7 +21,7 @@ export async function acceptTos(
if (!res) if (!res)
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'acceptTos (@ help.acceptTermsOfService)', 'help.acceptTermsOfService',
'true', 'true',
'false' 'false'
) )

View file

@ -23,7 +23,7 @@ export async function createChannel(
if (!(res._ === 'updates' || res._ === 'updatesCombined')) { if (!(res._ === 'updates' || res._ === 'updatesCombined')) {
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'createChannel (@ channels.createChannel)', 'channels.createChannel',
'updates | updatesCombined', 'updates | updatesCombined',
res._ res._
) )

View file

@ -36,7 +36,7 @@ export async function createGroup(
if (!(res._ === 'updates' || res._ === 'updatesCombined')) { if (!(res._ === 'updates' || res._ === 'updatesCombined')) {
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'createChannel (@ channels.createChannel)', 'messages.createChat',
'updates | updatesCombined', 'updates | updatesCombined',
res._ res._
) )

View file

@ -22,7 +22,7 @@ export async function createSupergroup(
if (!(res._ === 'updates' || res._ === 'updatesCombined')) { if (!(res._ === 'updates' || res._ === 'updatesCombined')) {
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'createChannel (@ channels.createChannel)', 'channels.createChannel',
'updates | updatesCombined', 'updates | updatesCombined',
res._ res._
) )

View file

@ -31,7 +31,7 @@ export async function joinChat(
}) })
if (!(res._ === 'updates' || res._ === 'updatesCombined')) { if (!(res._ === 'updates' || res._ === 'updatesCombined')) {
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'joinChat, (@ messages.importChatInvite)', 'messages.importChatInvite',
'updates | updatesCombined', 'updates | updatesCombined',
res._ res._
) )
@ -52,7 +52,7 @@ export async function joinChat(
}) })
if (!(res._ === 'updates' || res._ === 'updatesCombined')) { if (!(res._ === 'updates' || res._ === 'updatesCombined')) {
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'joinChat, (@ channels.joinChannel)', 'channels.joinChannel',
'updates | updatesCombined', 'updates | updatesCombined',
res._ res._
) )

View file

@ -53,7 +53,7 @@ export async function setChatDefaultPermissions(
if (!(res._ === 'updates' || res._ === 'updatesCombined')) { if (!(res._ === 'updates' || res._ === 'updatesCombined')) {
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'joinChat, (@ channels.joinChannel)', 'messages.editChatDefaultBannedRights',
'updates | updatesCombined', 'updates | updatesCombined',
res._ res._
) )

View file

@ -178,7 +178,7 @@ export async function* getDialogs(
): Dialog[] => { ): Dialog[] => {
if (res._ === 'messages.dialogsNotModified') if (res._ === 'messages.dialogsNotModified')
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'getDialogs', 'messages.getPeerDialogs',
'!messages.dialogsNotModified', '!messages.dialogsNotModified',
'messages.dialogsNotModified' 'messages.dialogsNotModified'
) )

View file

@ -41,7 +41,7 @@ export async function closePoll(
if (!(res._ === 'updates' || res._ === 'updatesCombined')) if (!(res._ === 'updates' || res._ === 'updatesCombined'))
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'_findMessageInUpdate', 'messages.editMessage',
'updates | updatesCombined', 'updates | updatesCombined',
res._ res._
) )
@ -49,10 +49,10 @@ export async function closePoll(
this._handleUpdate(res, true) this._handleUpdate(res, true)
const upd = res.updates[0] const upd = res.updates[0]
assertTypeIs('closePoll (@ messages.editMessage)', upd, 'updateMessagePoll') assertTypeIs('messages.editMessage (@ .updates[0])', upd, 'updateMessagePoll')
if (!upd.poll) { if (!upd.poll) {
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'closePoll (@ messages.editMessage)', 'messages.editMessage (@ .updates[0].poll)',
'poll', 'poll',
'undefined' 'undefined'
) )

View file

@ -42,7 +42,7 @@ export function _findMessageInUpdate(
} }
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'_findMessageInUpdate (@ -> updates[*])', '_findInUpdate (@ .updates[*])',
'updateNewMessage | updateNewChannelMessage | updateNewScheduledMessage', 'updateNewMessage | updateNewChannelMessage | updateNewScheduledMessage',
'none' 'none'
) )

View file

@ -232,7 +232,7 @@ export async function forwardMessages(
if (!(res._ === 'updates' || res._ === 'updatesCombined')) if (!(res._ === 'updates' || res._ === 'updatesCombined'))
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'forwardMessages', 'messages.forwardMessages',
'updates | updatesCombined', 'updates | updatesCombined',
res._ res._
) )

View file

@ -75,7 +75,7 @@ export async function getHistory(
if (res._ === 'messages.messagesNotModified') if (res._ === 'messages.messagesNotModified')
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'getHistory', 'messages.getHistory',
'!messages.messagesNotModified', '!messages.messagesNotModified',
res._ res._
) )

View file

@ -72,7 +72,7 @@ export async function* searchGlobal(
if (res._ === 'messages.messagesNotModified') if (res._ === 'messages.messagesNotModified')
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'searchMessages', 'messages.searchGlobal',
'!messages.messagesNotModified', '!messages.messagesNotModified',
res._ res._
) )

View file

@ -97,7 +97,7 @@ export async function* searchMessages(
if (res._ === 'messages.messagesNotModified') if (res._ === 'messages.messagesNotModified')
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'searchMessages', 'messages.search',
'!messages.messagesNotModified', '!messages.messagesNotModified',
res._ res._
) )

View file

@ -1,7 +1,15 @@
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteArgumentError, MtCuteTypeAssertionError, Poll } from '../../types' import {
InputPeerLike,
MtCuteArgumentError,
MtCuteTypeAssertionError,
Poll,
} from '../../types'
import { MaybeArray } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { createUsersChatsIndex, normalizeToInputPeer } from '../../utils/peer-utils' import {
createUsersChatsIndex,
normalizeToInputPeer,
} from '../../utils/peer-utils'
import { assertTypeIs } from '../../utils/type-assertion' import { assertTypeIs } from '../../utils/type-assertion'
/** /**
@ -31,7 +39,9 @@ export async function sendVote(
if (options.some((it) => typeof it === 'number')) { if (options.some((it) => typeof it === 'number')) {
const msg = await this.getMessages(peer, message) const msg = await this.getMessages(peer, message)
if (!(msg.media instanceof Poll)) if (!(msg.media instanceof Poll))
throw new MtCuteArgumentError('This message does not contain a poll') throw new MtCuteArgumentError(
'This message does not contain a poll'
)
poll = msg.media poll = msg.media
options = options.map((opt) => { options = options.map((opt) => {
@ -46,12 +56,12 @@ export async function sendVote(
_: 'messages.sendVote', _: 'messages.sendVote',
peer, peer,
msgId: message, msgId: message,
options: options as Buffer[] options: options as Buffer[],
}) })
if (!(res._ === 'updates' || res._ === 'updatesCombined')) if (!(res._ === 'updates' || res._ === 'updatesCombined'))
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'_findMessageInUpdate', 'messages.sendVote',
'updates | updatesCombined', 'updates | updatesCombined',
res._ res._
) )
@ -59,9 +69,13 @@ export async function sendVote(
this._handleUpdate(res, true) this._handleUpdate(res, true)
const upd = res.updates[0] const upd = res.updates[0]
assertTypeIs('sendVote (@ messages.sendVote)', upd, 'updateMessagePoll') assertTypeIs('messages.sendVote (@ .updates[0])', upd, 'updateMessagePoll')
if (!upd.poll) { if (!upd.poll) {
throw new MtCuteTypeAssertionError('sendVote (@ messages.sendVote)', 'poll', 'undefined') throw new MtCuteTypeAssertionError(
'messages.sendVote (@ .updates[0].poll)',
'poll',
'undefined'
)
} }
const { users } = createUsersChatsIndex(res) const { users } = createUsersChatsIndex(res)

View file

@ -64,7 +64,7 @@ export class Thumbnail extends FileLocation {
) { ) {
if (sz._ === 'photoSizeEmpty' || sz._ === 'photoCachedSize') if (sz._ === 'photoSizeEmpty' || sz._ === 'photoCachedSize')
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'Thumbnail#constructor (sz)', 'sz',
'not (photoSizeEmpty | photoCachedSize)', 'not (photoSizeEmpty | photoCachedSize)',
sz._ sz._
) )

View file

@ -216,7 +216,7 @@ export class Message {
this._sender = new User(this.client, this._users[from.userId]) this._sender = new User(this.client, this._users[from.userId])
} else } else
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'Message#sender (@ raw.fromId)', 'raw.fromId',
'peerUser | peerChannel', 'peerUser | peerChannel',
from._ from._
) )
@ -287,7 +287,7 @@ export class Message {
break break
default: default:
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'Message#forward (@ raw.fwdFrom.fromId)', 'raw.fwdFrom.fromId',
'peerUser | peerChannel', 'peerUser | peerChannel',
fwd.fromId._ fwd.fromId._
) )

View file

@ -147,7 +147,7 @@ export class StickerSet {
) )
if (!(sticker instanceof Sticker)) { if (!(sticker instanceof Sticker)) {
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'StickerSet#stickers (full.documents)', 'full.documents',
'Sticker', 'Sticker',
sticker.mimeType sticker.mimeType
) )

View file

@ -66,7 +66,7 @@ export class Chat {
) )
) )
throw new MtCuteTypeAssertionError( throw new MtCuteTypeAssertionError(
'Chat#constructor (@ peer)', 'peer',
'user | chat | channel', 'user | chat | channel',
peer._ peer._
) )