Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:change http error code #63

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ mime_guess = "2.0.4"
rust-embed = { version = "8.0.0", features = ["axum-ex"] }
prometheus = "0.13.3"
lazy_static = "1.4.0"
thiserror = "1.0"
6 changes: 3 additions & 3 deletions src/forward/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use webrtc::sdp::{MediaDescription, SessionDescription};

use crate::forward::forward_internal::{get_peer_key, PeerForwardInternal};
use crate::{media, metrics};

use crate::HttpError;
mod forward_internal;
mod rtcp;
mod track_match;
Expand All @@ -38,11 +38,11 @@ impl PeerForward {
offer: RTCSessionDescription,
) -> Result<(RTCSessionDescription, String)> {
if self.internal.anchor_is_some().await {
return Err(anyhow::anyhow!("anchor is set"));
return Err(HttpError::NotFound(anyhow::anyhow!("anchor is set")).into());
}
let _ = self.anchor_lock.lock().await;
if self.internal.anchor_is_some().await {
return Err(anyhow::anyhow!("anchor is set"));
return Err(HttpError::NotFound(anyhow::anyhow!("anchor is set")).into());
}
let peer = self
.internal
Expand Down
43 changes: 37 additions & 6 deletions src/main.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use anyhow and thiserror together to define our own error

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use log::info;
use tower_http::services::{ServeDir, ServeFile};
use tower_http::validate_request::ValidateRequestHeaderLayer;
use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;

use thiserror::Error;
use config::IceServer;
use path::manager::Manager;
#[cfg(not(debug_assertions))]
Expand Down Expand Up @@ -160,7 +160,8 @@ async fn whip(
return Err(anyhow::anyhow!("Content-Type must be application/sdp").into());
}
let offer = RTCSessionDescription::offer(body)?;
let (answer, key) = state.paths.publish(id, offer).await?;
let (answer, key) = state.paths.publish(id, offer).await.map_err(|err|{
AppError::not_found(anyhow::anyhow!(err))})?;
Ok(Response::builder()
.status(StatusCode::CREATED)
.header("Content-Type", "application/sdp")
Expand All @@ -184,7 +185,8 @@ async fn whep(
return Err(anyhow::anyhow!("Content-Type must be application/sdp").into());
}
let offer = RTCSessionDescription::offer(body)?;
let (answer, key) = state.paths.subscribe(id, offer).await?;
let (answer, key) = state.paths.subscribe(id, offer).await.map_err(|err|{
AppError::not_found(anyhow::anyhow!(err))})?;
Ok(Response::builder()
.status(StatusCode::CREATED)
.header("Content-Type", "application/sdp")
Expand Down Expand Up @@ -276,11 +278,14 @@ fn string_encoder(s: &impl ToString) -> String {
s[1..s.len() - 1].to_string()
}

struct AppError(anyhow::Error);
pub struct AppError {
status_code: StatusCode,
error: anyhow::Error,
}

impl IntoResponse for AppError {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, self.0.to_string()).into_response()
(self.status_code, self.error.to_string()).into_response()
}
}

Expand All @@ -289,6 +294,32 @@ where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
Self {
status_code: StatusCode::INTERNAL_SERVER_ERROR,
error: err.into(),
}
}
}

impl AppError {
pub fn not_found<E>(err: E) -> Self
where
E: Into<anyhow::Error>,
{
Self {
status_code: StatusCode::NOT_FOUND,
error: err.into(),
}
}
}
#[derive(Debug, Error)]
enum HttpError {
#[error("404 not found")]
NotFound(anyhow::Error),
}

impl IntoResponse for HttpError {
fn into_response(self) -> Response {
(StatusCode::NOT_FOUND, self.to_string()).into_response()
}
}
4 changes: 2 additions & 2 deletions src/path/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use webrtc::{
};

use crate::forward::PeerForward;

use crate::HttpError;
#[derive(Clone)]
pub struct Manager {
ice_servers: Vec<RTCIceServer>,
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Manager {
if let Some(forward) = forward {
forward.add_subscribe(offer).await
} else {
Err(anyhow::anyhow!("resource not exists"))
Err(HttpError::NotFound(anyhow::anyhow!("resource not exists")).into())
}
}

Expand Down