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)
throw new MtCuteTypeAssertionError(
'acceptTos (@ help.acceptTermsOfService)',
'help.acceptTermsOfService',
'true',
'false'
)

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,7 +1,15 @@
import { TelegramClient } from '../../client'
import { InputPeerLike, MtCuteArgumentError, MtCuteTypeAssertionError, Poll } from '../../types'
import {
InputPeerLike,
MtCuteArgumentError,
MtCuteTypeAssertionError,
Poll,
} from '../../types'
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'
/**
@ -31,7 +39,9 @@ export async function sendVote(
if (options.some((it) => typeof it === 'number')) {
const msg = await this.getMessages(peer, message)
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
options = options.map((opt) => {
@ -46,12 +56,12 @@ export async function sendVote(
_: 'messages.sendVote',
peer,
msgId: message,
options: options as Buffer[]
options: options as Buffer[],
})
if (!(res._ === 'updates' || res._ === 'updatesCombined'))
throw new MtCuteTypeAssertionError(
'_findMessageInUpdate',
'messages.sendVote',
'updates | updatesCombined',
res._
)
@ -59,9 +69,13 @@ export async function sendVote(
this._handleUpdate(res, true)
const upd = res.updates[0]
assertTypeIs('sendVote (@ messages.sendVote)', upd, 'updateMessagePoll')
assertTypeIs('messages.sendVote (@ .updates[0])', upd, 'updateMessagePoll')
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)

View file

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

View file

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

View file

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

View file

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