Skip to content

Commit

Permalink
Fix error pages returning a 200 status code
Browse files Browse the repository at this point in the history
Closes #31.
  • Loading branch information
Insprill committed May 5, 2024
1 parent 85ad4b3 commit 9721bda
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
16 changes: 9 additions & 7 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use actix_web::{
HttpResponse, Result,
};
use askama::Template;
use awc::error::HeaderValue;
use awc::{error::HeaderValue};
use log::error;

use crate::{
settings::{settings_from_req, Settings},
templates::template,
templates::{template_with_res},
};

pub fn render_500<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
Expand All @@ -20,8 +20,9 @@ pub fn render_500<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>>
error!("{}", str);
}

let new_response = template(
let new_response = template_with_res(
res.request(),
HttpResponse::InternalServerError(),
InternalErrorTemplate {
settings: settings_from_req(res.request()),
err,
Expand All @@ -31,8 +32,9 @@ pub fn render_500<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>>
}

pub fn render_404<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
let new_response = template(
let new_response = template_with_res(
res.request(),
HttpResponse::NotFound(),
NotFoundTemplate {
settings: settings_from_req(res.request()),
},
Expand All @@ -41,8 +43,9 @@ pub fn render_404<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>>
}

pub fn render_400<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
let new_response = template(
let new_response = template_with_res(
res.request(),
HttpResponse::BadRequest(),
BadRequestTemplate {
settings: settings_from_req(res.request()),
err: get_err_str(&res),
Expand All @@ -53,9 +56,8 @@ pub fn render_400<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>>

fn create<B>(
res: ServiceResponse<B>,
new_response: HttpResponse,
mut new_response: HttpResponse,
) -> Result<ErrorHandlerResponse<B>> {
let mut new_response = new_response;
new_response
.headers_mut()
.append(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
Expand Down
14 changes: 10 additions & 4 deletions src/templates.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use actix_web::{HttpRequest, HttpResponse};
use actix_web::{HttpRequest, HttpResponse, HttpResponseBuilder};
use askama::Template;

use crate::settings::SETTINGS_KEY;

pub fn template(req: &HttpRequest, t: impl Template) -> HttpResponse {
let mut res = HttpResponse::Ok();
let res = res // rust moment
.append_header(("Content-Type", "text/html; charset=utf-8"))
template_with_res(req, HttpResponse::Ok(), t)
}

pub fn template_with_res(
req: &HttpRequest,
mut res: HttpResponseBuilder,
t: impl Template,
) -> HttpResponse {
res.append_header(("Content-Type", "text/html; charset=utf-8"))
// Caching Setup
// Since Cloudflare ignores Vary headers, we can't publically cache all pages since only
// the last-cached theme would be shown to users. Instead, we privately cache all pages in the
Expand Down

0 comments on commit 9721bda

Please sign in to comment.