Skip to content

Commit

Permalink
Update errorpage.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
maddsua committed Feb 14, 2024
1 parent 8d2d2ba commit c117508
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
51 changes: 28 additions & 23 deletions core/server/errorpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,34 @@ HTTP::Response Pages::renderErrorPage(HTTP::Status code, const std::string& mess

HTTP::Response Pages::renderErrorPage(HTTP::Status code, const std::string& message, ErrorResponseType type) {

std::string pagecontent;

if (type == ErrorResponseType::HTML) {

pagecontent = renderTemplate(Templates::servicePage, {
{ "svcpage_statuscode", std::to_string(code.code()) },
{ "svcpage_statustext", code.text() },
{ "svcpage_message_text", message }
});

} else {

JSON::Map responseObject = {
{ "ok", false },
{ "status", "failed" },
{ "context", code.text() },
{ "what", message }
};

pagecontent = JSON::stringify(responseObject);
HTTP::Response errorResponse { code };

switch (type) {

case ErrorResponseType::HTML: {
errorResponse.body = renderTemplate(Templates::servicePage, {
{ "svcpage_statuscode", std::to_string(code.code()) },
{ "svcpage_statustext", code.text() },
{ "svcpage_message_text", message }
});
errorResponse.headers.set("content-type", "text/html");
} break;

case ErrorResponseType::JSON: {
errorResponse.body = JSON::stringify(JSON::Map({
{ "ok", false },
{ "status", "failed" },
{ "context", code.text() },
{ "what", message }
}));
errorResponse.headers.set("content-type", "application/json");
} break;

default: {
errorResponse.body = "Backend error: " + message + "\r\nmaddsua/lambda\r\n";
errorResponse.headers.set("content-type", "text/plain");
} break;
}

return Lambda::HTTP::Response(code, {
{ "Content-Type", type == ErrorResponseType::HTML ? "text/html" : "application/json" }
}, pagecontent);
return errorResponse;
}
2 changes: 1 addition & 1 deletion core/server/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Lambda {
};

enum struct ErrorResponseType {
HTML, JSON
Plain, HTML, JSON
};

struct ServeOptions {
Expand Down

0 comments on commit c117508

Please sign in to comment.