From: Ian Jackson Date: Sun, 12 Jul 2020 15:07:57 +0000 (+0100) Subject: error handling X-Git-Tag: otter-0.2.0~1353 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=6cc55c491648319f61ec23bbe24bc74dcfec5920;p=otter.git error handling --- diff --git a/src/api.rs b/src/api.rs index 4bdc001c..282f33e4 100644 --- a/src/api.rs +++ b/src/api.rs @@ -81,6 +81,7 @@ fn api_piece_op(form : Json>) })() { Err(err) => { let err : GameError = err; + if let GameError::InternalErrorSVG(svg) = err { Err(svg)? } eprintln!("API {:?} => {:?}", &form, &err); }, Ok((update, logents)) => { @@ -157,7 +158,7 @@ impl ApiPieceOp for ApiPieceGrab { let logent = LogEntry { html : format!("{} grasped {}", &htmlescape::encode_minimal(&pl.nick), - pc.describe_html(&lens.log_pri(piece, pc))), + pc.describe_html(&lens.log_pri(piece, pc))?), }; (update, vec![logent]) @@ -189,7 +190,7 @@ impl ApiPieceOp for ApiPieceUngrab { let logent = LogEntry { html : format!("{} released {}", &htmlescape::encode_minimal(&pl.nick), - pc.describe_html(&lens.log_pri(piece, pc))), + pc.describe_html(&lens.log_pri(piece, pc))?), }; (update, vec![logent]) diff --git a/src/error.rs b/src/error.rs index 06280505..0f048895 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,6 +9,7 @@ pub enum GameError { Conflict, PieceGone, PieceHeld, + InternalErrorSVG(#[from] SVGProcessingError), } #[derive(Error,Debug)] diff --git a/src/gamestate.rs b/src/gamestate.rs index a1c39e6e..40294eb9 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -69,12 +69,16 @@ type SR = Result<(),SE>; pub trait Piece : Send + Debug { fn svg_piece(&self, f: &mut String, pri: &PieceRenderInstructions) -> SR; + #[throws(SE)] fn outline_path(&self, pri : &PieceRenderInstructions) -> String; + #[throws(SE)] fn surround_path(&self, pri : &PieceRenderInstructions) -> String; fn svg_x_defs(&self, pri : &PieceRenderInstructions) -> String; + #[throws(SE)] fn thresh_dragraise(&self, pri : &PieceRenderInstructions) -> Option; - fn describe_html(&self, face : Option) -> String; + + fn describe_html(&self, face : Option) -> Result; } #[derive(Debug,Copy,Clone)] @@ -126,8 +130,8 @@ impl PieceState { pub fn make_defs(&self, pri : &PieceRenderInstructions) -> String { let pr = self; let mut defs = String::new(); - let dragraise = match pr.p.thresh_dragraise(pri) { - Some(n) if n < 0 => panic!(), + let dragraise = match pr.p.thresh_dragraise(pri)? { + Some(n) if n < 0 => Err(SE::NegativeDragraise)?, Some(n) => n, None => -1, }; @@ -138,7 +142,7 @@ impl PieceState { write!(defs, r##""##)?; write!(defs, r##""##, - pri.id, pr.p.surround_path(&pri))?; + pri.id, pr.p.surround_path(&pri)?)?; write!(defs, "{}", pr.p.svg_x_defs(&pri))?; defs } @@ -155,7 +159,7 @@ impl PieceState { } } - pub fn describe_html(&self, pri : &PieceRenderInstructions) -> String { + pub fn describe_html(&self, pri : &PieceRenderInstructions) -> Result { self.p.describe_html(Some(pri.face)) } } diff --git a/src/pieces.rs b/src/pieces.rs index 339c9560..a595b567 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -24,6 +24,7 @@ pub enum SVGProcessingError { UnknownOperator, BadNumber, WriteFail, + NegativeDragraise, } display_as_debug!{SVGProcessingError} @@ -87,12 +88,15 @@ impl Piece for SimpleShape { write!(f, r##""##, self.colours[pri.face], self.path)?; } + #[throws(SE)] fn outline_path(&self, _pri : &PieceRenderInstructions) -> String { self.path.clone() } + #[throws(SE)] fn surround_path(&self, _pri : &PieceRenderInstructions) -> String { self.scaled_path.clone() } + #[throws(SE)] fn thresh_dragraise(&self, _pri : &PieceRenderInstructions) -> Option { Some(self.approx_dia / 2) @@ -100,6 +104,7 @@ impl Piece for SimpleShape { fn svg_x_defs(&self, _pri : &PieceRenderInstructions) -> String { "".to_owned() } + #[throws(SE)] fn describe_html(&self, face : Option) -> String { if let Some(face) = face { format!("a {} {}", self.colours[face], self.desc)