use crate::rocket_framework as rocket;
use crate::{Error, ErrorKind};
use rocket::http::Status;
#[derive(rocket::Responder)]
#[response(content_type = "html")]
pub(crate) struct OAuthError((Status, String));
impl From<Error> for OAuthError {
fn from(error: Error) -> Self {
let status = match error.kind() {
ErrorKind::Callback => Status::BadRequest,
ErrorKind::Network | ErrorKind::Provider => Status::BadGateway,
ErrorKind::Unauthenticated => Status::Unauthorized,
ErrorKind::Configuration | ErrorKind::Session => Status::InternalServerError,
};
let message = match error.kind() {
ErrorKind::Callback => "OAuth callback validation failed",
ErrorKind::Network | ErrorKind::Provider => "OAuth provider request failed",
ErrorKind::Configuration | ErrorKind::Session => "Internal OAuth error",
ErrorKind::Unauthenticated => "Authentication required",
};
Self((
status,
format!(
r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="color-scheme" content="light dark">
<title>Internal Server Error</title>
</head>
<body align="center">
<div role="main" align="center">
<h1>{}: {}</h1>
<p>{}</p>
<hr />
</div>
<div role="contentinfo" align="center">
<small>Rocket</small>
</div>
</body>
</html>
"#,
status.code,
status.reason_lossy(),
message
),
))
}
}