Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug json.exception.type_error.302 #4492

Closed
1 of 2 tasks
OyakXD opened this issue Nov 12, 2024 · 16 comments
Closed
1 of 2 tasks

Bug json.exception.type_error.302 #4492

OyakXD opened this issue Nov 12, 2024 · 16 comments

Comments

@OyakXD
Copy link

OyakXD commented Nov 12, 2024

Description

I'm creating a bot for discord and it's falling into this exception, for no reason at all, I've already added all kinds of checks, all kinds of try catches, but it breaks all the time when I call it, it even shows the correct token

Reproduction steps

When I turn on the bot it starts everything fine, then at the end it launches the message and breaks

Expected vs. actual results

I get results

Minimal code example

I don't know exactly where the bug is, but when I start this main, this happens

`#include <string>
#include "Bot.h"
#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>

using namespace std;

int main() {
    ifstream token_file("../token.json");

    if (!token_file.is_open()) {
        cerr << "Erro: Não foi possivel abrir o arquivo token.json" << endl;
        return 1;
    }

    nlohmann::json config;

    try {
        token_file >> config;
        cout << "Conteúdo do JSON carregado: " << config.dump() << endl;

        if (config.contains("token") && !config["token"].is_null() && config["token"].is_string()) {
            string token = config["token"];

            cout << "Iniciando o bot com o token: " << token << endl;
            Bot DiscordBot(token);
            DiscordBot.start();
        } else {
            cerr << "Erro: token inválido." << endl;
            return 1;
        }
    } catch (const nlohmann::json::exception& e) {
        cerr << "Erro ao processar o JSON: " << e.what() << endl;
        return 1;
    } catch (const std::exception& e) {
        cerr << "Erro inesperado: " << e.what() << endl;
        return 1;
    }

    cout << "Programa finalizado com sucesso." << endl;
    return 0;
}` 

My code atuality

here is the token formatting in the json file

`
"token":  "MY_TOKEN"
`

Error messages

terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_2::detail::type_error'
  what():  [json.exception.type_error.302] type must be array, but is null
Abortado (imagem do núcleo gravada)

Compiler and operating system

I'm using gcc in linux, but atuality i am using cmake with build

Library version

master

Validation

@gregmarr
Copy link
Contributor

gregmarr commented Nov 12, 2024

Is that the full json file? That's not a valid file.
What is your full output? This is not showing that it hits even Conteúdo do JSON carregado: .

@OyakXD
Copy link
Author

OyakXD commented Nov 12, 2024

the complete Json it returns the bot token normally, the bot starts up to a certain point, I will give you a reference for this:

This is the terminal log:

Conteúdo do JSON carregado: {"token":"MTMwMTYyNDA4MzE5NjI4MDg3Mw.GchdbE.mPQ-y7CshnlSz-ZD6IYmctJXWR4D8RfYeUCHCI"}
Iniciando o bot com o token: MTMwMTYyNDA4MzE5NjI4MDg3Mw.GchdbE.mPQ-y7CshnlSz-ZD6IYmctJXWR4D8RfYeUCHCI
[2024-11-12 15:36:33] DEBUG: Cluster: 979 of 1000 session starts remaining
[2024-11-12 15:36:33] INFO: Auto Shard: Bot requires 1 shard
[2024-11-12 15:36:33] DEBUG: Starting with 1 shards...
[2024-11-12 15:36:34] DEBUG: Connecting new session...
[2024-11-12 15:36:34] INFO: Shard id 0 (1/1) ready!
[2024-11-12 15:36:34] DEBUG: Resume URL for session 707a14a57009b8eb9b44e6dd825e7879 is wss://gateway-us-east1-c.discord.gg (host: gateway-us-east1-c.discord.gg)
Bot está pronto, registrando comandos
terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_2::detail::type_error'
what(): [json.exception.type_error.302] type must be array, but is null
Abortado (imagem do núcleo gravada)

As said, it turns on, but it aborts the operation due to this error

@gregmarr
Copy link
Contributor

Your exception is not in this code that you've shared here. This code executes completely. You can see the Iniciando o bot com o token: output, which is after the last use of nlohmann::json in this code.

It must be some other usage in the Bot code. You should look for the string Bot está pronto, registrando comandos in your code and see what it does after that.

@OyakXD
Copy link
Author

OyakXD commented Nov 12, 2024

But I only use the library in this part of the code, but I can tell you where it says the bot is starting, here it is:

`
Bot::Bot(const string& token) : bot(token){

bot.on_slashcommand([this](const slashcommand_t& event) {
handle_command(event);
});
}

void Bot::start(){

bot.on_ready([this](const ready_t& event) {
if(run_once()){
spdlog::info("Bot Iniciado com sucesso!");
register_commands();
}
});
bot.start(st_wait);
}

void Bot::register_commands(){
// Registra um novo comando e adiciona ao mapeamento
command_map["hello"] = make_unique();
command_map["ping"] = make_unique();
for(const auto& [name, command] : command_map){
slashcommand new_command(command->get_name(), command->get_description(), bot.me.id);
add_command(bot, new_command);
}

}

void Bot::handle_command(const slashcommand_t& event){
/*
TODO
Método para lidar com os comandos
*/
auto command_name = event.command.get_command_name();

if(command_map.find(command_name) != command_map.end()){
command_map[command_name]->execute(event);
} else {
event.reply("Comando Desconhecido.");
}
}`

he recorded the commands

@gregmarr
Copy link
Contributor

As far as I can see, it is not possible for this code to throw that error. This error (code 302, mentioning array) is only thrown by from_json functions for arrays and tuples, and you're not using any of them in that code. Are you sure that nothing else in your code is using the library other than the code in main?

Could you try removing the library from main and hardcoding the token to see if it still happens?

@OyakXD
Copy link
Author

OyakXD commented Nov 12, 2024

Yes, I have already removed the library and it still happens, could it be a memory error? I also had the same thought about this error, I'm not using it anywhere else in the code except in main.

@gregmarr
Copy link
Contributor

Are you using any third party libraries that might include it?

@OyakXD
Copy link
Author

OyakXD commented Nov 12, 2024

No, I'm just using it at the moment. To actually pull just the token

@gregmarr
Copy link
Contributor

I can't see what would be causing this then. You'll probably have to run it under a debugger and set it to stop when an exception is thrown.
@nlohmann Any other ideas?

@OyakXD
Copy link
Author

OyakXD commented Nov 12, 2024

I tried to put exceptions so the bot doesn't shut down, but it still doesn't fall into the block, it just aborts the program. If you want to analyze the code, it's in my github repository pinned

@nlohmann
Copy link
Owner

Try with diagnostics enabled to maybe get a path to the mistyped element. In any way, please share a minimal example so we don't need to guess which other code might trigger the warning.

@gregmarr
Copy link
Contributor

gregmarr commented Nov 12, 2024

You're using dpp which also uses this library.
https://github.com/brainboxdotcc/DPP/tree/master/include/dpp/nlohmann

@OyakXD
Copy link
Author

OyakXD commented Nov 12, 2024

So the problem is there? Isn't it from json? or is the problem my code? What exactly does this mean?

@OyakXD
Copy link
Author

OyakXD commented Nov 12, 2024

I just tested it on my friend's pc and it worked on his

@gregmarr
Copy link
Contributor

The exception is from this library, but it's from something that the dpp code is doing, not your code. You may be doing something wrong in how you're using dpp, or there may be a bug in dpp. In any case, you probably need to get support from them, as there's nothing we can do with the information that we have here.

@OyakXD OyakXD closed this as completed Nov 12, 2024
@nlohmann
Copy link
Owner

@OyakXD Just to make sure: did you revoke the token you shared in the example above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants