Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/api/exception-response-handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "exception-response-handler.h"

#include <drogon/drogon.h>

drogon::HttpResponsePtr ExceptionResponseHandler::handle(const std::string& status, const std::string& message, drogon::HttpStatusCode status_code)
{
Json::Value responseBody;
responseBody["status"] = status;
responseBody["message"] = message;

auto response = drogon::HttpResponse::newHttpJsonResponse(responseBody);
response->setStatusCode(status_code);

return response;
}

drogon::HttpResponsePtr ExceptionResponseHandler::handle(const std::exception& exception)
{
LOG_ERROR << "Exception occured: " << exception.what();

return ExceptionResponseHandler::handle("error", "Internal server error", drogon::k500InternalServerError);
}
21 changes: 21 additions & 0 deletions src/api/exception-response-handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <drogon/HttpResponse.h>

#include <exception>

class ExceptionResponseHandler
{
public:
/// @brief Creates a JSON HTTP error response with the specified status and message.
/// @param status Status value included in the JSON response body.
/// @param message Message included in the JSON response body.
/// @param status_code HTTP status code returned to the client.
/// @return JSON HTTP response.
[[nodiscard]] static drogon::HttpResponsePtr handle(const std::string& status, const std::string& message, drogon::HttpStatusCode status_code);

/// @brief Handles an unexpected exception and creates an internal server error response.
/// @param exception Exception caught while handling an HTTP request.
/// @return HTTP 500 JSON response.
[[nodiscard]] static drogon::HttpResponsePtr handle(const std::exception& exception);
};
29 changes: 9 additions & 20 deletions src/api/features/to-dos/to-dos-controller.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
#include "to-dos-controller.h"
#include "db_connection.h"
#include <exception-response-handler.h>

// TODO(https://github.com/TourmalineCore/to-dos-api-cpp/issues/25): Create http exception handler or generic class for handling that type of errors
HttpResponsePtr ToDosController::createInternalServerErrorResponse(const std::string& error) const
{
Json::Value jsonResponse;
jsonResponse["status"] = "Error";
jsonResponse["message"] = "Internal server error";
jsonResponse["error"] = error;

auto resp = HttpResponse::newHttpJsonResponse(jsonResponse);
resp->setStatusCode(k500InternalServerError);
return resp;
}

ToDosController::ToDosController()
{
Expand Down Expand Up @@ -48,9 +37,9 @@ void ToDosController::getToDos(const HttpRequestPtr& req, std::function<void(con
resp->setStatusCode(k200OK);
callback(resp);
}
catch (const std::exception& e)
catch (const std::exception& exception)
{
callback(createInternalServerErrorResponse(e.what()));
callback(ExceptionResponseHandler::handle(exception));
}
}

Expand Down Expand Up @@ -79,9 +68,9 @@ void ToDosController::addToDo(const HttpRequestPtr& req, std::function<void(cons
resp->setStatusCode(k201Created);
callback(resp);
}
catch (const std::exception& e)
catch (const std::exception& exception)
{
callback(createInternalServerErrorResponse(e.what()));
callback(ExceptionResponseHandler::handle(exception));
}
}

Expand Down Expand Up @@ -114,9 +103,9 @@ void ToDosController::completeToDos(const HttpRequestPtr& req, std::function<voi
resp->setStatusCode(k200OK);
callback(resp);
}
catch (const std::exception& e)
catch (const std::exception& exception)
{
callback(createInternalServerErrorResponse(e.what()));
callback(ExceptionResponseHandler::handle(exception));
}
}

Expand All @@ -134,8 +123,8 @@ void ToDosController::deleteToDo(const HttpRequestPtr& req, std::function<void(c
resp->setStatusCode(k200OK);
callback(resp);
}
catch (const std::exception& e)
catch (const std::exception& exception)
{
callback(createInternalServerErrorResponse(e.what()));
callback(ExceptionResponseHandler::handle(exception));
}
}
Loading