Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: use BrokerError only in broker.rs
Lars Wirzenius committed 1 year ago
commit d4cbcc81ed917d5244d23767195b55d33d93a4a4
parent 9f5db36e44c30983daee27b0ed3cc18dbd712f7e
6 files changed +70 -82
modified src/bin/cib.rs
@@ -12,10 +12,9 @@ use log::{debug, error, info};

use radicle_ci_broker::{
    adapter::Adapter,
-
    broker::Broker,
+
    broker::{Broker, BrokerError},
    config::{Config, ConfigError},
    db::{Db, DbError},
-
    error::BrokerError,
    queueadd::{AdderError, QueueAdderBuilder},
    queueproc::{QueueError, QueueProcessorBuilder},
};
modified src/bin/cibtool.rs
@@ -17,8 +17,8 @@ use radicle::{
use radicle_git_ext::Oid;

use radicle_ci_broker::{
+
    broker::BrokerError,
    db::{Db, DbError, QueueId},
-
    error::BrokerError,
    event::BrokerEvent,
};

@@ -34,6 +34,7 @@ fn main() {
    }
}

+
#[allow(clippy::result_large_err)]
fn fallible_main() -> Result<(), CibToolError> {
    pretty_env_logger::init();

@@ -53,6 +54,7 @@ struct Args {
}

impl Args {
+
    #[allow(clippy::result_large_err)]
    fn run(&self) -> Result<(), CibToolError> {
        match &self.cmd {
            Cmd::Counter(x) => x.run(self)?,
@@ -62,6 +64,7 @@ impl Args {
        Ok(())
    }

+
    #[allow(clippy::result_large_err)]
    fn open_db(&self) -> Result<Db, CibToolError> {
        Ok(Db::new(&self.db)?)
    }
@@ -81,6 +84,7 @@ struct Counter {
}

impl Counter {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        match &self.cmd {
            CounterCmd::Show(x) => x.run(args)?,
@@ -100,6 +104,7 @@ enum CounterCmd {
struct ShowCounter {}

impl ShowCounter {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;
        let counter = db.get_counter()?;
@@ -115,6 +120,7 @@ struct CountCounter {
}

impl CountCounter {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;
        Self::inc(&db, self.goal)?;
@@ -165,6 +171,7 @@ struct Event {
}

impl Event {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        match &self.cmd {
            EventCmd::Add(x) => x.run(args)?,
@@ -195,6 +202,7 @@ struct ListEvents {
}

impl ListEvents {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;
        for id in db.queued_events()? {
@@ -216,6 +224,7 @@ impl ListEvents {
struct CountEvents {}

impl CountEvents {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;
        println!("{}", db.queued_events()?.len());
@@ -245,6 +254,7 @@ struct AddEvent {
}

impl AddEvent {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let rid = if let Ok(rid) = RepoId::from_urn(&self.repo) {
            rid
@@ -284,11 +294,13 @@ impl AddEvent {
        Ok(())
    }

+
    #[allow(clippy::result_large_err)]
    fn lookup_nid(&self) -> Result<NodeId, CibToolError> {
        let profile = Profile::load().map_err(CibToolError::Profile)?;
        Ok(*profile.id())
    }

+
    #[allow(clippy::result_large_err)]
    fn lookup_rid(&self, wanted: &str) -> Result<RepoId, CibToolError> {
        let profile = Profile::load().map_err(CibToolError::Profile)?;
        let storage =
@@ -317,6 +329,7 @@ impl AddEvent {
        }
    }

+
    #[allow(clippy::result_large_err)]
    fn lookup_commit(&self, rid: RepoId, gitref: &str) -> Result<Oid, CibToolError> {
        let profile = Profile::load().map_err(CibToolError::Profile)?;
        let storage =
@@ -349,6 +362,7 @@ struct ShowEvent {
}

impl ShowEvent {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;

@@ -390,6 +404,7 @@ struct RemoveEvent {
}

impl RemoveEvent {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;

@@ -415,6 +430,7 @@ struct Shutdown {
}

impl Shutdown {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;
        let id = db.push_queued_event(BrokerEvent::Shutdown)?;
@@ -434,6 +450,7 @@ struct Run {
}

impl Run {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        match &self.cmd {
            RunCmd::List(x) => x.run(args)?,
@@ -451,6 +468,7 @@ enum RunCmd {
struct ListRuns {}

impl ListRuns {
+
    #[allow(clippy::result_large_err)]
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let db = args.open_db()?;

@@ -464,6 +482,7 @@ impl ListRuns {
}

#[derive(Debug, thiserror::Error)]
+
#[allow(clippy::large_enum_variant)]
enum CibToolError {
    #[error("failed to look up node profile")]
    Profile(#[source] radicle::profile::Error),
modified src/broker.rs
@@ -3,7 +3,11 @@
//! This is type and module of its own to facilitate automated
//! testing.

-
use std::{collections::HashMap, error::Error, path::Path};
+
use std::{
+
    collections::HashMap,
+
    error::Error,
+
    path::{Path, PathBuf},
+
};

use log::{debug, error, info};
use time::{macros::format_description, OffsetDateTime};
@@ -12,8 +16,7 @@ use radicle::prelude::RepoId;

use crate::{
    adapter::Adapter,
-
    db::Db,
-
    error::BrokerError,
+
    db::{Db, DbError},
    msg::{PatchEvent, PushEvent, Request},
    run::{Run, Whence},
};
@@ -134,6 +137,47 @@ fn now() -> String {
    OffsetDateTime::now_utc().format(fmt).expect("format time")
}

+
/// All possible errors from this module.
+
#[derive(Debug, thiserror::Error)]
+
#[allow(clippy::large_enum_variant)]
+
pub enum BrokerError {
+
    /// Error from an node event subscriber.
+
    #[error(transparent)]
+
    NodeEvent(#[from] crate::event::NodeEventError),
+

+
    /// Error from Radicle.
+
    #[error(transparent)]
+
    RadicleProfile(#[from] radicle::profile::Error),
+

+
    /// Error from spawning a sub-process.
+
    #[error("failed to spawn a CI adapter sub-process: {0}")]
+
    SpawnAdapter(PathBuf, #[source] std::io::Error),
+

+
    /// Default adapter is not in list of adapters.
+
    #[error("default adapter is not in list of adapters")]
+
    UnknownDefaultAdapter(String),
+

+
    /// No adapter set for repository and no default adapter set.
+
    #[error("could not determine what adapter to use for repository {0}")]
+
    NoAdapter(RepoId),
+

+
    /// Request is not a trigger message.
+
    #[error("tried to execute CI based on a message that is not a trigger one: {0:#?}")]
+
    NotTrigger(Request),
+

+
    /// Could not convert repository ID from string.
+
    #[error("failed to understand repository id {0:?}")]
+
    BadRepoId(String, #[source] radicle::identity::IdError),
+

+
    /// Patch event doesn't have any revisions.
+
    #[error("expected at least one revision in a patch event")]
+
    NoRevisions,
+

+
    /// Database error.
+
    #[error(transparent)]
+
    Db(#[from] DbError),
+
}
+

#[cfg(test)]
mod test {
    use std::path::Path;
deleted src/error.rs
@@ -1,74 +0,0 @@
-
//! Possible errors returned by the CI broker library.
-
//!
-
//! Each module has its own error type, this module collects them
-
//! together into a unified type, for callers who don't care to handle
-
//! module errors in specific ways.
-

-
use std::path::PathBuf;
-

-
use radicle::prelude::RepoId;
-

-
use crate::{
-
    adapter::AdapterError,
-
    config::ConfigError,
-
    db::DbError,
-
    msg::{MessageError, Request},
-
    pages::PageError,
-
};
-

-
/// All possible errors from the CI broker messages.
-
#[derive(Debug, thiserror::Error)]
-
#[allow(clippy::large_enum_variant)]
-
pub enum BrokerError {
-
    /// A configuration related error.
-
    #[error(transparent)]
-
    Config(#[from] ConfigError),
-

-
    /// A message related error.
-
    #[error(transparent)]
-
    Message(#[from] MessageError),
-

-
    /// An adapter related error.
-
    #[error(transparent)]
-
    Adapter(#[from] AdapterError),
-

-
    /// Error from an node event subscriber.
-
    #[error(transparent)]
-
    NodeEvent(#[from] crate::event::NodeEventError),
-

-
    /// Error from Radicle.
-
    #[error(transparent)]
-
    RadicleProfile(#[from] radicle::profile::Error),
-

-
    /// Error from spawning a sub-process.
-
    #[error("failed to spawn a CI adapter sub-process: {0}")]
-
    SpawnAdapter(PathBuf, #[source] std::io::Error),
-

-
    /// Default adapter is not in list of adapters.
-
    #[error("default adapter is not in list of adapters")]
-
    UnknownDefaultAdapter(String),
-

-
    /// No adapter set for repository and no default adapter set.
-
    #[error("could not determine what adapter to use for repository {0}")]
-
    NoAdapter(RepoId),
-

-
    /// Request is not a trigger message.
-
    #[error("tried to execute CI based on a message that is not a trigger one: {0:#?}")]
-
    NotTrigger(Request),
-

-
    /// Could not convert repository ID from string.
-
    #[error("failed to understand repository id {0:?}")]
-
    BadRepoId(String, #[source] radicle::identity::IdError),
-

-
    /// Status page error.
-
    #[error(transparent)]
-
    StatusPage(#[from] PageError),
-

-
    /// Database error.
-
    #[error(transparent)]
-
    Db(#[from] DbError),
-

-
    /// Patch event doesn't have any revisions.
-
    #[error("expected at least one revision in a patch event")]
-
    NoRevisions,
-
}
modified src/lib.rs
@@ -9,7 +9,6 @@ pub mod adapter;
pub mod broker;
pub mod config;
pub mod db;
-
pub mod error;
pub mod event;
pub mod msg;
pub mod pages;
modified src/queueproc.rs
@@ -12,8 +12,8 @@ use radicle::Profile;

use crate::{
    broker::Broker,
+
    broker::BrokerError,
    db::{Db, DbError, QueueId, QueuedEvent},
-
    error::BrokerError,
    event::BrokerEvent,
    msg::{MessageError, RequestBuilder},
};
@@ -130,6 +130,7 @@ impl QueueProcessor {
}

#[derive(Debug, thiserror::Error)]
+
#[allow(clippy::large_enum_variant)]
pub enum QueueError {
    #[error("failed to load node profile")]
    Profile(#[source] radicle::profile::Error),