Skip to content

Commit

Permalink
Merge pull request webrtc-rs#3 from edmellum/refactor-error-handling
Browse files Browse the repository at this point in the history
Refactor error handling
  • Loading branch information
Rain Liu authored Jan 31, 2021
2 parents 9c8a55f + 7c8ad57 commit 6585e1e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ repository = "https://github.com/webrtc-rs/sdp"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
util = { package = "webrtc-rs-util", version = "0.1.0" }
url = "2.1.0"
rand = "0.8.0"
thiserror = "1.0.23"
33 changes: 33 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::{num::ParseIntError, string::FromUtf8Error};

use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("codec not found")]
CodecNotFound,
#[error("could not extract codec from rtcp-fb")]
RtcpFb,
#[error("could not extract codec from fmtp")]
FmtpParse,
#[error("could not extract codec from rtpmap")]
RtpmapParse,
#[error("payload type not found")]
PayloadTypeNotFound,
#[error("SyntaxError: {0}")]
ExtMapParse(String),
#[error("sdp: empty time_descriptions")]
SdpEmptyTimeDescription,
#[error("SdpInvalidSyntax: {0}")]
SdpInvalidSyntax(String),
#[error("SdpInvalidValue: {0}")]
SdpInvalidValue(String),
#[error("FromUtf8Error: {0}")]
Utf8Error(#[from] FromUtf8Error),
#[error("ParseIntError: {0}")]
ParseIntError(#[from] ParseIntError),
#[error("UrlParseError: {0}")]
UrlParseError(#[from] url::ParseError),
#[error("IoError: {0}")]
Io(#[from] std::io::Error),
}
15 changes: 9 additions & 6 deletions src/extmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::fmt;
use std::io;

use url::Url;
use util::Error;

use super::common_description::*;
use super::direction::*;
use super::error::Error;

#[cfg(test)]
mod extmap_test;
Expand Down Expand Up @@ -66,19 +66,19 @@ impl ExtMap {
reader.read_line(&mut line)?;
let parts: Vec<&str> = line.trim().splitn(2, ':').collect();
if parts.len() != 2 {
return Err(Error::new(format!("SyntaxError: {}", line)));
return Err(Error::ExtMapParse(line));
}

let fields: Vec<&str> = parts[1].split_whitespace().collect();
if fields.len() < 2 {
return Err(Error::new(format!("SyntaxError: {}", line)));
return Err(Error::ExtMapParse(line));
}

let valdir: Vec<&str> = fields[0].split('/').collect();
let value = valdir[0].parse::<isize>()?;
if value < 1 || value > 246 {
return Err(Error::new(format!(
"SyntaxError: {} -- extmap key must be in the range 1-256",
return Err(Error::ExtMapParse(format!(
"{} -- extmap key must be in the range 1-256",
valdir[0]
)));
}
Expand All @@ -87,7 +87,10 @@ impl ExtMap {
if valdir.len() == 2 {
direction = Direction::new(valdir[1]);
if direction == Direction::DirectionUnknown {
return Err(Error::new(format!("unknown direction from {}", valdir[1])));
return Err(Error::ExtMapParse(format!(
"unknown direction from {}",
valdir[1]
)));
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/extmap/extmap_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use super::*;
use crate::util::{ATTRIBUTE_KEY, END_LINE};

use util::Error;

use std::io::BufReader;
use std::iter::Iterator;

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

pub mod common_description;
pub mod direction;
pub mod error;
pub mod extmap;
pub mod media_description;
pub mod session_description;
Expand Down
Loading

0 comments on commit 6585e1e

Please sign in to comment.