File size: 1,337 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use anyhow::{Context, Result};
use turbo_rcstr::RcStr;
use turbo_tasks::Vc;
use turbo_tasks_fs::embed_file;

#[turbo_tasks::function]
pub(super) async fn error_html(
    status_code: u16,
    title: RcStr,
    details: RcStr,
) -> Result<Vc<RcStr>> {
    let html = create_html(status_code, title, details).await?;

    Ok(Vc::cell(html.into()))
}

#[turbo_tasks::function]
pub(super) async fn error_html_body(
    status_code: u16,
    title: RcStr,
    details: RcStr,
) -> Result<Vc<RcStr>> {
    let html = create_html(status_code, title, details).await?;

    let (_, body) = html.split_once("<body>").context("no body in html")?;
    let (body, _) = body.split_once("</body>").context("no body in html")?;

    Ok(Vc::cell(body.into()))
}

async fn create_html(status_code: u16, title: RcStr, details: RcStr) -> Result<String> {
    let file_content = embed_file!("src/render/error.html").await?;
    let file = file_content
        .as_content()
        .context("embedded file not found (this should not happen)")?;

    let html = file
        .content()
        .to_str()
        .context("couldn't convert embedded html to string")?;

    let html = html.replace("${TITLE}", &title);
    let html = html.replace("${STATUS_CODE}", &status_code.to_string());
    let html = html.replace("${DETAILS}", &details);

    Ok(html)
}