RPC Error handling
Hello,
I'm trying to sanitize channelId from user input in the method via try/catch approach, how can I grab RPCError object from the method
await telegram_client.invoke( new Api.channels.GetFullChannel({ channel: channel_id }) );
as I have the Error message in stdout
RPCError: 400: CHANNEL_INVALID (caused by channels.GetChannels)
but catch handler gives me the following:
Error: Could not find the input entity for {"channelId":"xxxx","className":"PeerChannel"}. Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
I'm not sure what you mean by "sanitize" The be able to get a channel you need both channel id and access hash if the library didn't see the access hash fo the specific channel it can't use any requests.
Well, let's say i got the input from user where I can't be sure that the channel ID is correct one.
I'll use try/catch wrapper together with
telegram_client.invoke( new Api.channels.GetFullChannel({ channel: channel_id }) );
it works fine (if the channel ID is correct), but how really handle the case when channel ID is wrong, because I did not get how to grab this error from stdout of node to catch block:
RPCError: 400: CHANNEL_INVALID (caused by channels.GetChannels)
I would like to get the cod of RPC Error and desc: CHANNEL_INVALID to understand the reason of error.
But in the block catch(err), the err.message is
Error: Could not find the input entity for {"channelId":"xxxx","className":"PeerChannel"}. Please read https://docs.telethon.dev/en/latest/concepts/entities.html to find out more details.
you won't get CHANNEL_INVALID beacuse the ID might be valid but you don't have permission to see it as you need the acceshash for that.
If you want to always get CHANNEL_INVALID you have to manually put the access hash.
telegram_client.invoke( new Api.channels.GetFullChannel({ channel: channel_id, acessHash:.. }) );
you won't get CHANNEL_INVALID beacuse the ID might be valid but you don't have permission to see it as you need the acceshash for that.
If you want to always get CHANNEL_INVALID you have to manually put the access hash.
telegram_client.invoke( new Api.channels.GetFullChannel({ channel: channel_id, acessHash:.. }) );
I'll check it later today, hope that helps in that matter.
I have a question about: how to carry out handling of RPCErrors, I mean in the documents there is mentioning that you can extend the RPC error class with custom one. Can you share the code example how should it be done?
I'm not sure why you would want that,
you can probably just do if (error instanceof RPCError) maybe?
That's what i try to achieve,
I would like to get in error object code of error to arrange the right handling approach, but based on code above I got the error code only in the console, in error object of catch(){} I have only message and stack options:
How is it possible to get err.code and err.errorMessage in catch handler?
try e.errorMessage
I consolled Object.getOwnPropertyNames to show all possible methods/properties: it has only stack and message regarding screenshot below
e.errorMessage

It's pretty much strange, because for some gram's methods I do get the "right"/expected error message with error code, for some not.
for example here

it works as expected

![]()
I consolled Object.getOwnPropertyNames to show all possible methods/properties: it has only stack and message regarding screenshot below
e.errorMessage
![]()
It's pretty much strange, because for some gram's methods I do get the "right"/expected error message with error code, for some not.
when the error is about "missing" entity then the library throws the error not telegram, and that will not have an errorMessage field.
When the error is about something with the API itself telegram will return the error and the library will put the API result in errorMessage
I consolled Object.getOwnPropertyNames to show all possible methods/properties: it has only stack and message regarding screenshot below
e.errorMessage
![]()
It's pretty much strange, because for some gram's methods I do get the "right"/expected error message with error code, for some not.
when the error is about "missing" entity then the library throws the error not telegram, and that will not have an errorMessage field.
When the error is about something with the API itself telegram will return the error and the library will put the API result in errorMessage
At both cases we had errorMessage in the console and code, but only in one it was dropped through to the Error object.

For the first case (which issue has), it was not missing value, I just amended one number in the channelId to make it wrong, but type, length and the rest for the input was correct. The lib could not preliminary classify the input as "wrong".
It seems as TG answer
but it just did not moved up to the Error instance.
U can replicate on your own if it's needed
can you send me the full code you tried to replicate it ? I'll test it and see what's the issue.
can you send me the full code you tried to replicate it? I'll test it and see what's the issue.
Case with issue:
try {
const a = await client.invoke(
new Api.channels.GetParticipants({
channel: id, <-- put any wrong BigInt number for the channel id
filter: new Api.ChannelParticipantsSearch({ q: '' }),
offset: 0,
limit: 200,
hash: 0,
}))
return a
}
catch (e) {
console.log('eeee', 'code', e, e.stack, e.message, Object.getOwnPropertyNames(e), e.errorMessage)
throw e
}
Case 2 (works as expected):
try {
result = await telegram_client.invoke(
new Api.channels.GetParticipant({
channel: -xxxx, <-- put correct channel ID
participant: "xxxx", <-- put userID which is absent on the channel
})
);
}
catch (e) {
console.log(err:e, e.message, e.code, 'code')
throw e
}
@painor did you hve a chance to take a look on that?
hey, yes, sadly I was not able to reproduce the issue, in my case it shows Error: Could not find the input entity for {"userId:XXX"} for both cases.
The library cannot send the request if it hasn't seen the ID before. and if it didn't see the ID it will throw that error.
The check happens here https://github.com/gram-js/gramjs/blob/master/gramjs/tl/api.js#L149