From: Ian Jackson Date: Sun, 12 Jul 2020 14:49:46 +0000 (+0100) Subject: wip error handling X-Git-Tag: otter-0.2.0~1356 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=9b0fc39d677091b84b4c1ac43335f03f8d2349d9;p=otter.git wip error handling --- diff --git a/src/api.rs b/src/api.rs index 641ac026..4bdc001c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -97,11 +97,11 @@ fn api_piece_op(form : Json>) let pri_for_all = lens.svg_pri(piece,pc,Default::default()); - let update = update.map_new_state(|_|{ - let mut ns = pc.prep_piecestate(&pri_for_all); + let update = update.try_map_new_state(|_|{ + let mut ns = pc.prep_piecestate(&pri_for_all)?; lens.massage_prep_piecestate(&mut ns); - ns - }); + >::Ok(ns) + })?; let mut us = Vec::with_capacity(1 + logents.len()); diff --git a/src/error.rs b/src/error.rs index 7d443193..06280505 100644 --- a/src/error.rs +++ b/src/error.rs @@ -23,6 +23,8 @@ pub enum OnlineError { InvalidZCoord, #[error("JSON~ serialisation error: {0:?}")] JSONSerializeFailed(#[from] serde_json::error::Error), + #[error("SVG processing/generation error {0:?}")] + SVGProcessingFailed(#[from] SVGProcessingError), } pub use OnlineError::{NoClient,NoPlayer}; diff --git a/src/gamestate.rs b/src/gamestate.rs index de88436f..df151ccc 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -132,6 +132,7 @@ impl Display for ZCoord { // ---------- game state - rendering etc. ---------- impl PieceState { + #[throws(SE)] pub fn make_defs(&self, pri : &PieceRenderInstructions) -> String { let pr = self; let mut defs = String::new(); @@ -152,12 +153,13 @@ impl PieceState { defs } + #[throws(SE)] pub fn prep_piecestate(&self, pri : &PieceRenderInstructions) -> PreparedPieceState { PreparedPieceState { pos : self.pos, held : self.held, - svg : self.make_defs(pri), + svg : self.make_defs(pri)?, z : self.zlevel.z, zg : self.zlevel.zg, } diff --git a/src/http.rs b/src/http.rs index 9751e372..973b1516 100644 --- a/src/http.rs +++ b/src/http.rs @@ -14,7 +14,7 @@ impl<'r> Responder<'r> for OnlineError { use rocket::http::Status; use OnlineError::*; let status = match self { - GameCorrupted | JSONSerializeFailed(_) + GameCorrupted | JSONSerializeFailed(_) | SVGProcessingFailed(_) => Status::InternalServerError, NoClient | NoPlayer => Status::NotFound, InvalidZCoord => Status::BadRequest, diff --git a/src/imports.rs b/src/imports.rs index 77ac8108..35906b4c 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -17,6 +17,7 @@ pub use std::iter::repeat_with; pub use std::collections::VecDeque; pub use std::num::Wrapping; pub use std::cmp; +pub use std::error::Error; pub use thiserror::Error; pub use anyhow::{Context,anyhow}; diff --git a/src/session.rs b/src/session.rs index fa13f7ac..8c3a329e 100644 --- a/src/session.rs +++ b/src/session.rs @@ -91,7 +91,7 @@ fn session(form : Json) -> Result { id : make_pieceid_visible(gpid), face : pr.face, }; - let defs = pr.make_defs(&pri); + let defs = pr.make_defs(&pri)?; alldefs.push((pri.id, defs)); let for_info = SessionPieceLoadJson { diff --git a/src/updates.rs b/src/updates.rs index 6baabec5..4ae1dc99 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -122,16 +122,23 @@ impl PieceUpdateOp { SetZLevel(_) => None, } } - pub fn map_new_state NS2>(self, f:F) - -> PieceUpdateOp { + pub fn try_map_new_state Result> + (self, f:F) -> Result,E> + { use PieceUpdateOp::*; - match self { + Ok(match self { Delete() => Delete(), - Insert(ns) => Insert(f(ns)), - Modify(ns) => Modify(f(ns)), + Insert(ns) => Insert(f(ns)?), + Modify(ns) => Modify(f(ns)?), Move(pos) => Move(pos), SetZLevel(zl) => SetZLevel(zl), - } + }) + } + pub fn map_new_state NS2>(self, f:F) + -> PieceUpdateOp { + #[derive(Error,Debug)] + enum Never { } + self.try_map_new_state(|ns| >::Ok(f(ns))).unwrap() } pub fn new_z_generation(&self) -> Option { use PieceUpdateOp::*;