gramjs icon indicating copy to clipboard operation
gramjs copied to clipboard

RPC Error handling

Open salacoste opened this issue 3 years ago • 15 comments

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.

salacoste avatar May 30 '22 00:05 salacoste

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.

painor avatar May 30 '22 07:05 painor

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.

salacoste avatar May 30 '22 08:05 salacoste

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:.. }) );

painor avatar May 30 '22 12:05 painor

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?

salacoste avatar May 30 '22 15:05 salacoste

I'm not sure why you would want that,

you can probably just do if (error instanceof RPCError) maybe?

painor avatar May 30 '22 17:05 painor

image 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: image

How is it possible to get err.code and err.errorMessage in catch handler?

salacoste avatar Jun 01 '22 00:06 salacoste

try e.errorMessage

painor avatar Jun 01 '22 00:06 painor

image I consolled Object.getOwnPropertyNames to show all possible methods/properties: it has only stack and message regarding screenshot below image

e.errorMessage image

image

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.

salacoste avatar Jun 01 '22 00:06 salacoste

for example here image image

it works as expected image

salacoste avatar Jun 01 '22 00:06 salacoste

image

I consolled Object.getOwnPropertyNames to show all possible methods/properties: it has only stack and message regarding screenshot below image

e.errorMessage image

image

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

painor avatar Jun 01 '22 09:06 painor

image I consolled Object.getOwnPropertyNames to show all possible methods/properties: it has only stack and message regarding screenshot below image e.errorMessage image image 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. image

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 image but it just did not moved up to the Error instance.

U can replicate on your own if it's needed

salacoste avatar Jun 01 '22 12:06 salacoste

can you send me the full code you tried to replicate it ? I'll test it and see what's the issue.

painor avatar Jun 01 '22 12:06 painor

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
        }

salacoste avatar Jun 01 '22 22:06 salacoste

@painor did you hve a chance to take a look on that?

salacoste avatar Jun 03 '22 21:06 salacoste

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

painor avatar Jun 04 '22 15:06 painor