Skip to content

Commit

Permalink
Add HttpError class, for the error handling implemention.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sepulven committed May 13, 2024
1 parent 0570089 commit 4e9b0c1
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 38 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
NAME = webserv
CXX = c++
CXXFLAGS = -Wall -Werror -Wextra -std=c++98 -g # -fsanitize=address
CXXFLAGS = #-Wall -Werror -Wextra -std=c++98 -g # -fsanitize=address

SRCS = main.cpp ServerContext.cpp WebServer.cpp \
/parser/Lexer.cpp /parser/Parser.cpp /parser/ParserUtils.cpp
SRCS = main.cpp ServerContext.cpp WebServer.cpp HttpError.cpp

PARSER = Parser.cpp ParserUtils.cpp Lexer.cpp

UTILS = __raw_data_utils.cpp __file_manager_utils.cpp __webserv_utils.cpp

CONN = Req.cpp Res.cpp ConnStream.cpp

SRC = $(addprefix srcs/,$(SRCS)) \
$(addprefix srcs/utils/,$(UTILS)) \
$(addprefix srcs/parser/,$(PARSER)) \
$(addprefix srcs/conn/,$(CONN))

INCLUDES = -I ./inc
Expand Down
14 changes: 1 addition & 13 deletions inc/ConnStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/* Classes */
#include <Req.hpp>
#include <Res.hpp>
#include <HttpError.hpp>
#include <ServerContext.hpp>

/*Utils*/
Expand All @@ -19,7 +20,6 @@
class Req;
class Res;


class ConnStream
{
protected:
Expand All @@ -41,16 +41,4 @@ class ConnStream

void set_time(void);
void clean_conn();

class Error : public std::exception
{
public:
const char *status;
const char *msg;
virtual const char *what() const throw();

const char *get_msg(void) const;
const char *get_status(void) const;
Error(const char *, const char *);
};
};
15 changes: 15 additions & 0 deletions inc/HttpError.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <exception>

class HttpError : public std::exception
{
public:
const char *status;
const char *msg;
virtual const char *what() const throw();

const char *get_msg(void) const;
const char *get_status(void) const;
HttpError(const char *, const char *);
};
1 change: 1 addition & 0 deletions inc/Req.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

/* Classes */
#include <ConnStream.hpp>
#include <HttpError.hpp>

/* Utils */
#include <__raw_data_utils.hpp>
Expand Down
1 change: 1 addition & 0 deletions inc/Res.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/* Classes */
#include <Req.hpp>
#include <HttpError.hpp>
#include <ConnStream.hpp>

enum RES_STATUS {
Expand Down
19 changes: 19 additions & 0 deletions srcs/HttpError.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <HttpError.hpp>

/*Exception class*/
HttpError::HttpError(const char *_status, const char *_msg) : status(_status), msg(_msg) {}

const char *HttpError::get_msg(void) const
{
return (msg);
}

const char *HttpError::get_status(void) const
{
return (status);
}

const char *HttpError::what() const throw()
{
return (msg);
}
19 changes: 0 additions & 19 deletions srcs/conn/ConnStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,3 @@ void ConnStream::set_time(void)
this->close_conn_time = this->last_action_time + 20000; // * The connection can last 20 seconds without events;
this->kill_cgi_time = this->last_action_time + 5000; // * The connection can take 5 seconds to run the CGI
}


/*Exception class*/
ConnStream::Error::Error(const char *_status, const char *_msg) : status(_status), msg(_msg) {}

const char *ConnStream::Error::get_msg(void) const
{
return (msg);
}

const char *ConnStream::Error::get_status(void) const
{
return (status);
}

const char *ConnStream::Error::what() const throw()
{
return (msg);
}
2 changes: 1 addition & 1 deletion srcs/conn/Req.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int Req::read(int fd)
&& raw_body.size() >= content_length)
return (1);
}
catch (const ConnStream::Error &e)
catch (const HttpError &e)
{
std::cout << e.what() << std::endl;
return (1);
Expand Down
6 changes: 4 additions & 2 deletions srcs/conn/Res.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ int Res::send(void)
Req *req = stream->req;
std::vector<std::string>::iterator it = req->cgi_path.begin();

this->status_code = "";
try
{
while (it != req->cgi_path.end())
Expand All @@ -61,10 +62,11 @@ int Res::send(void)
else if (req->method == "DELETE")
exec_delete();
if (this->status[status_code] == "")
throw ConnStream::Error("200", "Status code not found");
throw HttpError("200", "Status code not found");
}
catch (const ConnStream::Error &e)
catch (const HttpError &e)
{
std::cout << e.what() << std::endl;
}
return (build_http_response());
}
Expand Down

0 comments on commit 4e9b0c1

Please sign in to comment.