From b8d3435660e6196b1aa9bbd458accdecaf46c674 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 13 Jul 2021 15:07:43 +0100 Subject: [PATCH] errors: Rename error types according to new scheme Signed-off-by: Ian Jackson --- daemon/api.rs | 39 +++++++++++++++++++-------------------- daemon/main.rs | 4 ++-- src/error.rs | 32 ++++++++++++++++---------------- src/global.rs | 8 ++++---- src/hidden.rs | 4 ++-- src/organise.rs | 6 +++--- src/prelude.rs | 4 ++-- src/updates.rs | 2 +- 8 files changed, 49 insertions(+), 50 deletions(-) diff --git a/daemon/api.rs b/daemon/api.rs index ac079812..8e4a597d 100644 --- a/daemon/api.rs +++ b/daemon/api.rs @@ -39,10 +39,10 @@ mod op { use super::*; pub trait Core: Debug { - #[throws(OnlineError)] + #[throws(Fatal)] fn check_held(&self, pc: &GPiece, player: PlayerId) { if pc.held != None && pc.held != Some(player) { - throw!(OnlineError::PieceHeld) + throw!(Fatal::PieceHeld) } } } @@ -67,11 +67,11 @@ mod op { #[derive(Error,Debug)] #[error("{0}")] -pub struct OnlineErrorResponse(#[from] OnlineError); +pub struct FatalErrorResponse(#[from] Fatal); -impl From<&OnlineErrorResponse> for rocket::http::Status { - fn from(oe: &OnlineErrorResponse) -> rocket::http::Status { - use OnlineError::*; +impl From<&FatalErrorResponse> for rocket::http::Status { + fn from(oe: &FatalErrorResponse) -> rocket::http::Status { + use Fatal::*; match oe.0 { ServerFailure(_) => Status::InternalServerError, NoClient | NoPlayer(_) | GameBeingDestroyed(_) @@ -86,7 +86,7 @@ impl From<&OnlineErrorResponse> for rocket::http::Status { } } -impl<'r> Responder<'r> for OnlineErrorResponse { +impl<'r> Responder<'r> for FatalErrorResponse { #[throws(Status)] fn respond_to(self, req: &Request) -> Response<'r> { let msg = format!("Online-layer error\n{:?}\n{}\n", self, self); @@ -123,7 +123,6 @@ fn api_piece_op(form: Json>) let piece = vpiece_decode(gs, player, gpl, form.piece); if_let!{ Some(piece) = piece; else return Ok(()) } let was_held = gs.pieces.get(piece).as_ref().map(|gpc| gpc.held); - use ApiPieceOpError::*; match (||{ let ipc = ipieces.get(piece).ok_or(POE::PieceGone)?; @@ -136,7 +135,7 @@ fn api_piece_op(form: Json>) debug!("client={:?} pc.lastclient={:?} pc.gen_before={:?} pc.gen={:?} q_gen={:?} u_gen={:?}", &client, &gpc.lastclient, &gpc.gen_before_lastclient, &gpc.gen, &q_gen, &u_gen); - if u_gen > q_gen { throw!(PieceOpError::Conflict) } + if u_gen > q_gen { throw!(Inapplicable::Conflict) } trace_dbg!("form.op", player, piece, &form.op, &gpc); form.op.check_held(gpc,player)?; let update = @@ -148,21 +147,21 @@ fn api_piece_op(form: Json>) })?; Ok::<_,ApiPieceOpError>(update) })() { - Err(ReportViaUpdate(poe)) => { + Err(APOE::Inapplicable(poe)) => { PrepareUpdatesBuffer::piece_report_error( &mut ig, poe, piece, vec![], POEPP::Unprocessed, client, form.cseq, )?; debug!("api_piece_op Err(RVU): {:?}", &form); }, - Err(PartiallyProcessed(poe, logents)) => { + Err(APOE::PartiallyProcessed(poe, logents)) => { PrepareUpdatesBuffer::piece_report_error( &mut ig, poe, piece, logents, POEPP::Partially, client, form.cseq, )?; debug!("api_piece_op Err(PP): {:?}", &form); }, - Err(ReportViaResponse(err)) => { + Err(APOE::Fatal(err)) => { warn!("api_piece_op ERROR {:?}: {:?}", &form, &err); Err(err)?; }, @@ -289,7 +288,7 @@ api_route!{ "grasped" )?; - if gpc.held.is_some() { throw!(OnlineError::PieceHeld) } + if gpc.held.is_some() { throw!(Fatal::PieceHeld) } gpc.held = Some(player); let update = PieceUpdateOp::ModifyQuiet(()); @@ -306,7 +305,7 @@ api_route!{ } impl op::Core as { - #[throws(OnlineError)] + #[throws(Fatal)] fn check_held(&self, _pc: &GPiece, _player: PlayerId) { } } @@ -388,7 +387,7 @@ api_route!{ then { let z = gs.max_z.z.clone_mut().increment().map_err( - |e| APOE::ReportViaResponse(IE::from(e).into()))?; + |e| APOE::Fatal(IE::from(e).into()))?; Some(ZLevel { z, zg: gs.gen }) } else { None } @@ -403,7 +402,7 @@ api_route!{ )?; let who_by = who_by.ok_or(POE::PieceGone)?; - if gpc.held != Some(player) { throw!(OnlineError::PieceHeld) } + if gpc.held != Some(player) { throw!(Fatal::PieceHeld) } gpc.held = None; let wrc = if let Some(zlevel) = new_z { @@ -433,7 +432,7 @@ api_route!{ to_recalculate, piece, vanilla, - ).map_err(|e| OnlineError::from(e))?; + ).map_err(|e| Fatal::from(e))?; update } @@ -465,12 +464,12 @@ api_route!{ struct ApiPieceMove(Pos); impl op::Core as { - #[throws(OnlineError)] + #[throws(Fatal)] fn check_held(&self, gpc: &GPiece, player: PlayerId) { // This will ensure that occultations are (in general) properly // updated, because the player will (have to) release the thing // again - if gpc.held != Some(player) { throw!(OnlineError::PieceHeld) } + if gpc.held != Some(player) { throw!(Fatal::PieceHeld) } if gpc.occult.is_active() { throw!(OE::Occultation) } if matches_doesnot!( gpc.moveable(), @@ -491,7 +490,7 @@ api_route!{ Err(pote) => { gpc.pos = pote.clamped; throw!(ApiPieceOpError::PartiallyProcessed( - PieceOpError::PosOffTable, + Inapplicable::PosOffTable, logents, )); } diff --git a/daemon/main.rs b/daemon/main.rs index 0b997e51..5c20e1af 100644 --- a/daemon/main.rs +++ b/daemon/main.rs @@ -28,10 +28,10 @@ pub use rocket_contrib::templates::Engines; pub use rocket_contrib::templates::Template; pub use crate::api::InstanceAccess; -pub use crate::api::{OnlineErrorResponse}; +pub use crate::api::{FatalErrorResponse}; pub use crate::cmdlistener::*; -pub type OER = OnlineErrorResponse; +pub type OER = FatalErrorResponse; // xxx rename this alias use rocket::fairing; use rocket::response::Content; diff --git a/src/error.rs b/src/error.rs index 15a3a071..22dfc84c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,7 +5,7 @@ use crate::prelude::*; #[derive(Error,Debug)] -pub enum OnlineError { +pub enum Fatal { // Includes _bogus_ client updates, see PROTOCOL.md #[error("Game in process of being destroyed")] GameBeingDestroyed(#[from] GameBeingDestroyed), #[error("client session not recognised (terminated by server?)")] @@ -17,17 +17,17 @@ pub enum OnlineError { #[error("JSON deserialisation error: {0}")] BadJSON(serde_json::Error), #[error("referenced piece is gone (maybe race)")] - PieceHeld, + PieceHeld, // xxx should be _inapplicable_ #[error("improper UI operation")] - PieceImmoveable, + PieceImmoveable, // xxx should be _inapplicable_ #[error("improper UI operation")] - BadOperation, + BadOperation, // xxx should be _inapplicable_ #[error("overlapping occultation")] - OverlappingOccultation, + OverlappingOccultation, // xxx should be _inapplicable_ #[error("piece is occulting, or occulted")] - Occultation, + Occultation, // xxx should be _inapplicable_ #[error("UI operation not valid in the curret piece state")] - BadPieceStateForOperation, + BadPieceStateForOperation, // xxx should be _inapplicable_ } #[derive(Error,Debug)] @@ -130,26 +130,26 @@ impl From for SpecError { #[derive(Error,Debug)] pub enum ApiPieceOpError { - ReportViaResponse(#[from] OnlineError), - ReportViaUpdate(#[from] PieceOpError), + Fatal(#[from] Fatal), + Inapplicable(#[from] Inapplicable), /// This error is always generated in the context of a piece /// operation by a particular client. It corresponds roughly to a /// PieceUpdateFromOp for other clients of (Unpredicable, /// PieceUpdateOp::Modify, ..). /// For this client it is that but also an error report. - PartiallyProcessed(PieceOpError, Vec), + PartiallyProcessed(Inapplicable, Vec), } display_as_debug!(ApiPieceOpError); impl From for ApiPieceOpError { fn from(x: PlayerNotFound) -> ApiPieceOpError { - ApiPieceOpError::ReportViaResponse(x.into()) + ApiPieceOpError::Fatal(x.into()) } } impl From for ApiPieceOpError { fn from(x: InternalError) -> ApiPieceOpError { - ApiPieceOpError::ReportViaResponse(x.into()) + ApiPieceOpError::Fatal(x.into()) } } @@ -159,7 +159,7 @@ pub enum ErrorSignaledViaUpdate { PlayerRemoved, // appears only in streams for applicable player TokenRevoked, // appears only in streams for applicable player PieceOpError { - error: PieceOpError, + error: Inapplicable, partially: PieceOpErrorPartiallyProcessed, state: POEPU, }, @@ -174,7 +174,7 @@ pub enum PieceOpErrorPartiallyProcessed { display_as_debug!{PieceOpErrorPartiallyProcessed} #[derive(Error,Debug,Serialize,Copy,Clone)] -pub enum PieceOpError { +pub enum Inapplicable { Conflict, PosOffTable, PieceGone, @@ -183,11 +183,11 @@ pub enum PieceOpError { OcculterAlreadyRotated, OrganisedPlacementOverfull, } -display_as_debug!{PieceOpError} +display_as_debug!{Inapplicable} pub type StartupError = anyhow::Error; -pub use OnlineError::{NoClient,NoPlayer}; +pub use Fatal::{NoClient,NoPlayer}; pub enum AggregatedIE { Ok, diff --git a/src/global.rs b/src/global.rs index 354ef4ae..76e95dbc 100644 --- a/src/global.rs +++ b/src/global.rs @@ -1296,7 +1296,7 @@ pub fn load_games(accounts: &mut AccountsGuard, pub type TokenTable = HashMap>; pub trait AccessId: Copy + Clone + 'static { - type Error: Into; + type Error: Into; const ERROR: Self::Error; fn global_tokens(_:PrivateCaller) -> &'static RwLock>; fn tokens_registry(ig: &mut Instance, _:PrivateCaller) @@ -1321,8 +1321,8 @@ impl AccessId for PlayerId { } } impl AccessId for ClientId { - type Error = OnlineError; - const ERROR: OnlineError = NoClient; + type Error = Fatal; + const ERROR: Fatal = NoClient; fn global_tokens(_: PrivateCaller) -> &'static RwLock> { &GLOBAL.clients } @@ -1416,7 +1416,7 @@ impl GPieces { impl ById for GPieces { type Id = PieceId; type Entry = GPiece; - type Error = PieceOpError; + type Error = Inapplicable; #[throws(POE)] fn byid(&self, piece: PieceId) -> &GPiece { self.get(piece).ok_or(POE::PieceGone)? diff --git a/src/hidden.rs b/src/hidden.rs index b62d31e0..3228fda0 100644 --- a/src/hidden.rs +++ b/src/hidden.rs @@ -428,7 +428,7 @@ impl GPiece { } pub fn occulter_check_unrotated(&self, _:ShowUnocculted) - -> Result { + -> Result { if self.angle.is_rotated() { Err(POE::OcculterAlreadyRotated) } else { Ok(OcculterRotationChecked(())) } } @@ -881,7 +881,7 @@ impl OccultationViewDef for OwnerOccultationView { } } } -#[throws(OnlineError)] +#[throws(Fatal)] pub fn create_occultation( gen: &mut UniqueGenGen, max_z: &mut ZLevel, diff --git a/src/organise.rs b/src/organise.rs index 59550e2e..30619340 100644 --- a/src/organise.rs +++ b/src/organise.rs @@ -106,7 +106,7 @@ type Primary<'pe> = IndexVec>; type ZLevels = IndexVec; type OrderTable = IndexVec; -#[throws(PieceOpError)] +#[throws(Inapplicable)] fn recover_order(region: &Rect, pieces: &Primary, zlevels: &ZLevels) -> OrderTable { @@ -290,11 +290,11 @@ pub fn ui_operation(a: &mut ApiPieceOpArgs<'_>, _: OcculterRotationChecked, for &pos in &layout { // Some sanity checks pos.clamped(gs.table_size).map_err( - |_| APOE::ReportViaUpdate(POE::PosOffTable))?; + |_| APOE::Inapplicable(POE::PosOffTable))?; match gs.occults.pos_occulter(&gs.occults, pos)? { None => {}, Some(occulter) if occulter == apiece => {}, - Some(_) => throw!(APOE::ReportViaUpdate(POE::Occultation)), + Some(_) => throw!(APOE::Inapplicable(POE::Occultation)), }; } diff --git a/src/prelude.rs b/src/prelude.rs index c420b4b1..afc9aada 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -196,8 +196,8 @@ pub type PUM = ProgressUpdateMode; pub type APOE = ApiPieceOpError; pub type ESVU = ErrorSignaledViaUpdate; pub type IE = InternalError; -pub type OE = OnlineError; -pub type POE = PieceOpError; +pub type OE = Fatal; // xxx get rid of this alyas when we've cleaned up Fatal +pub type POE = Inapplicable; // xxx rename this alias pub type POEPP = PieceOpErrorPartiallyProcessed; pub type SvgE = SVGProcessingError; pub type SpE = SpecError; diff --git a/src/updates.rs b/src/updates.rs index 53b69871..5a9a80ff 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -611,7 +611,7 @@ impl<'r> PrepareUpdatesBuffer<'r> { } pub fn piece_report_error(ig: &mut Instance, - error: PieceOpError, piece: PieceId, + error: Inapplicable, piece: PieceId, logents: Vec, partially: PieceOpErrorPartiallyProcessed, client: ClientId, cseq: ClientSequence) -- 2.30.2