mattmalec / Pterodactyl4J

Java wrapper for the API of Pterodactyl
Apache License 2.0
113 stars 31 forks source link

[BUG] Client get request throws HttpException instead of LoginException #73

Open Raft08 opened 9 months ago

Raft08 commented 9 months ago

Hello, I'm trying to make some check ups before running my code.

I do this code to get some information and to check if the panel is acccessible and I have a correct API token.

    private void checkoutAccount() {
        try {
            Account account = this.client.retrieveAccount()
                    .timeout(this.configuration.getTimeout(), TimeUnit.MILLISECONDS)
                    .execute();

            if (!account.isRootAdmin())
                Bootstrap.crash("Pterodactyl account is missing permissions. Administration permissions are required by AtlasNetwork.");

            LOGGER.info("Connected as {}!", account.getUserName());
        } catch (LoginException | HttpException e) {
            Bootstrap.crash("Could not open connection with '" +
                    this.configuration.getUrl() + ": Invalid Authentication Token!");
        }
    }

But when I'm unauthenticated. it throws HttpException instead of LoginException is this expected behaviour?

The javadoc says "Throws: LoginException – If the API key is incorrect"

mattmalec commented 9 months ago

Is the API key valid, but missing certain permissions?

Raft08 commented 9 months ago

Nope I gave a wrong api key just for testing

I gave 'some-token' as api key just to test that code

mattmalec commented 9 months ago

How do you know it's throwing a HttpException? If we get a 403 from the panel, we throw a LoginException (see here: https://github.com/mattmalec/Pterodactyl4J/blob/develop/src/main/java/com/mattmalec/pterodactyl4j/requests/Request.java#L73-L76)

Raft08 commented 9 months ago

With the code up there if I don't catch the HttpException it simply makes my application crash, I'm forced to handle the HttpException as a login issue if I want the application to gracefully crash.

image

It's not an OkHttp HttpException but your library's HttpException (com.mattmalec.pterodactyl4j.exceptions.HttpException)

Raft08 commented 9 months ago

It seems that the panel if the API key is wrong doesn't return 403 code but an 401 code

mattmalec commented 9 months ago

Definitely looks like a bug then - want to throw up a PR? Sounds like we can just have a 401 case and fallthrough to the 403 logic

Raft08 commented 9 months ago

I could do that but won't it be better to simply report this issue to pterodactyl directly? At least to know if it is expected behaviour from the panel to return 401 instead of 403

Raft08 commented 9 months ago

I'm going to make a PR for this bug and another bug that I found.