mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-28 21:07:59 +03:00
feat: move custom exceptions to domain_error
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
// #include "parse.hpp"
|
||||
|
||||
#define THREAD_NUM 100
|
||||
#define MAX_EVENTS
|
||||
@@ -43,26 +44,26 @@ void Server::readConfig(char *filename)
|
||||
root = parse(filename);
|
||||
|
||||
}
|
||||
catch (config::Tokenizer::NoMoreTokens &e)
|
||||
catch (std::domain_error &e)
|
||||
{
|
||||
std::cerr << RED << "FATAL: ";
|
||||
std::cerr << e.what() << RESET << std::endl;
|
||||
// root->clear();
|
||||
// delete root;
|
||||
// clean_parsed(root);
|
||||
config::clean_parsed(parser.root);
|
||||
// delete parser.root;
|
||||
exit(-1);
|
||||
}
|
||||
catch (config::Tokenizer::InvalidToken &e)
|
||||
{
|
||||
std::cerr << RED << "FATAL: ";
|
||||
std::cerr << e.what() << RESET << std::endl;
|
||||
config::clean_parsed(parser.root);
|
||||
// root->clear();
|
||||
delete root;
|
||||
exit(-1);
|
||||
// exit(-1);
|
||||
return;
|
||||
|
||||
}
|
||||
// catch (config::Tokenizer::InvalidToken &e)
|
||||
// {
|
||||
// std::cerr << RED << "FATAL: ";
|
||||
// std::cerr << e.what() << RESET << std::endl;
|
||||
// config::clean_parsed(parser.root);
|
||||
// // root->clear();
|
||||
// // delete parser.root;
|
||||
// exit(-1);
|
||||
// }
|
||||
|
||||
/* TOMLMap *map; */
|
||||
TOMLMap::iterator it1;
|
||||
@@ -82,6 +83,8 @@ void Server::readConfig(char *filename)
|
||||
config::clean_parsed(parser.root);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Server::sendData(Client &client, int fd)
|
||||
{
|
||||
/* std::string tmp = client.getStrToSend(); */
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "TOMLNode.hpp"
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace config
|
||||
{
|
||||
@@ -14,8 +15,6 @@ namespace config
|
||||
{
|
||||
private:
|
||||
std::fstream file;
|
||||
std::auto_ptr<TOMLMap> root;
|
||||
// TOMLMap *root; //root of TOML tree
|
||||
/* toml_node *current; //node currently being parsed */
|
||||
Tokenizer tokenizer;
|
||||
|
||||
@@ -26,10 +25,12 @@ namespace config
|
||||
|
||||
public:
|
||||
TOMLParser(char *filename);
|
||||
TOMLMap *parse(void);
|
||||
void parse(void);
|
||||
|
||||
// toml_node *parseMap(void);
|
||||
std::auto_ptr<toml_node> parseMap(void);
|
||||
TOMLMap *root; //root of TOML tree
|
||||
|
||||
toml_node *parseMap(void);
|
||||
// TOMLMap *parseMap(void);
|
||||
void processMap(void);
|
||||
|
||||
void processMapArray(void);
|
||||
@@ -44,48 +45,48 @@ namespace config
|
||||
|
||||
toml_node *parseNil(void);
|
||||
|
||||
class ExpectedToken: public std::exception
|
||||
class ExpectedToken: public std::domain_error
|
||||
{
|
||||
protected:
|
||||
std::string *msg;
|
||||
std::string msg;
|
||||
|
||||
public:
|
||||
ExpectedToken(const std::string &missing, const std::string context)
|
||||
ExpectedToken(const std::string &missing, const std::string context): std::domain_error("ExpectedToken")
|
||||
{
|
||||
msg = new std::string("Config file: ");
|
||||
*msg += ("Expected " + missing + " at " + context);
|
||||
msg = std::string("Config file: ");
|
||||
msg += ("Expected " + missing + " at " + context);
|
||||
}
|
||||
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return (msg->c_str());
|
||||
return (msg.c_str());
|
||||
}
|
||||
|
||||
virtual ~ExpectedToken() throw()
|
||||
{
|
||||
delete msg;
|
||||
// delete msg;
|
||||
}
|
||||
};
|
||||
class UnexpectedToken: public std::exception
|
||||
class UnexpectedToken: public std::domain_error
|
||||
{
|
||||
protected:
|
||||
std::string *msg;
|
||||
std::string msg;
|
||||
|
||||
public:
|
||||
UnexpectedToken(const std::string &unexpected, const std::string context)
|
||||
UnexpectedToken(const std::string &unexpected, const std::string context): std::domain_error("ExpectedToken")
|
||||
{
|
||||
msg = new std::string("Config file: ");
|
||||
*msg += ("Unexpected " + unexpected + " " + context);
|
||||
msg = std::string("Config file: ");
|
||||
msg += ("Unexpected " + unexpected + " " + context);
|
||||
}
|
||||
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return (msg->c_str());
|
||||
return (msg.c_str());
|
||||
}
|
||||
|
||||
virtual ~UnexpectedToken() throw()
|
||||
{
|
||||
delete msg;
|
||||
// delete msg;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -61,34 +61,58 @@ namespace config
|
||||
void rollBackToken();
|
||||
void set_last(e_token type);
|
||||
|
||||
class NoMoreTokens: public std::exception
|
||||
class NoMoreTokens: public std::domain_error
|
||||
{
|
||||
public:
|
||||
NoMoreTokens(void): std::domain_error("NoMoreTokens")
|
||||
{}
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return ("Config may be incomplete, expected more tokens (check EOL)");
|
||||
}
|
||||
};
|
||||
class InvalidToken: public std::exception
|
||||
class InvalidToken: public std::domain_error
|
||||
{
|
||||
protected:
|
||||
std::string *msg;
|
||||
std::string msg;
|
||||
|
||||
public:
|
||||
InvalidToken(const std::string &token)
|
||||
InvalidToken(const std::string &token): std::domain_error("InvalidToken")
|
||||
{
|
||||
|
||||
msg = new std::string("Invalid token: '" + token + "'");
|
||||
msg = std::string("Invalid token: '" + token + "'");
|
||||
}
|
||||
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return (msg->c_str());
|
||||
return (msg.c_str());
|
||||
}
|
||||
|
||||
virtual ~InvalidToken() throw()
|
||||
{
|
||||
delete msg;
|
||||
// delete msg;
|
||||
}
|
||||
};
|
||||
class SyntaxErrNear: public std::domain_error
|
||||
{
|
||||
protected:
|
||||
std::string msg;
|
||||
|
||||
public:
|
||||
SyntaxErrNear(const std::string &near): std::domain_error("InvalidToken")
|
||||
{
|
||||
|
||||
msg = std::string("Syntax error near: " + near);
|
||||
}
|
||||
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return (msg.c_str());
|
||||
}
|
||||
|
||||
virtual ~SyntaxErrNear() throw()
|
||||
{
|
||||
// delete msg;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user