Skip to content

attempt at error refactoring #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions services/src/contexts/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use snafu::Snafu;

/// High-level error, reported to the user.
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
#[snafu(context(suffix(false)))] // disables default `Snafu` suffix
pub enum ApplicationContextError {
CreateContext { source: InternalError },
SessionById { source: InternalError },
}

#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
#[snafu(context(suffix(false)))] // disables default `Snafu` suffix
pub enum SimpleApplicationContextError {
DefaultSession { source: InternalError },
UpdateDefaultSessionProject { source: InternalError },
UpdateDefaultSessionView { source: InternalError },
DefaultSessionContext { source: InternalError },
}

#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
#[snafu(context(suffix(false)))] // disables default `Snafu` suffix
pub enum SessionContextError {
Volumes,
}

/// Low-level internal error, not (directly) reported to the user.
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
#[snafu(context(suffix(false)))] // disables default `Snafu` suffix
pub enum InternalError {
CouldNotGetPostgresConfig,
CouldNotGetSchemaStatus {
source: tokio_postgres::Error,
},
CouldNotGetClearDatabaseOnStartConfig {
source: tokio_postgres::Error,
},
#[snafu(display(
"Database cannot be cleared on startup because it was initially started without that setting."
))]
ClearDatabaseOnStartupNotAllowed,
CouldNotRecreateDatabaseSchema {
source: tokio_postgres::Error,
},
CouldNotInitializeSchema {
source: tokio_postgres::Error,
},
CouldNotCreateDefaultSession {
source: tokio_postgres::Error,
},
CouldNotLoadDefaultSession {
source: tokio_postgres::Error,
},
InvalidSession,
Postgres {
source: tokio_postgres::Error,
},
Bb8 {
source: bb8_postgres::bb8::RunError<tokio_postgres::Error>,
},
// TODO: add source error from user db once it is refactored
CouldNotGetSessionById,
}
16 changes: 11 additions & 5 deletions services/src/contexts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::datasets::upload::Volume;
use crate::error::Result;
use crate::layers::listing::{DatasetLayerCollectionProvider, LayerCollectionProvider};
use crate::layers::storage::{LayerDb, LayerProviderDb};
use crate::tasks::{TaskContext, TaskManager};
Expand All @@ -16,6 +15,7 @@ use tokio::fs::File;
use tokio::io::AsyncWriteExt;
use tokio::sync::RwLock;

pub mod error;
mod postgres;
mod session;
mod simple_context;
Expand All @@ -34,10 +34,13 @@ use geoengine_operators::engine::{
use geoengine_operators::mock::MockDatasetDataSourceLoadingInfo;
use geoengine_operators::source::{GdalLoadingInfo, OgrSourceDataset};

pub use error::ApplicationContextError;
pub use postgres::{PostgresContext, PostgresDb, PostgresSessionContext};
pub use session::{MockableSession, Session, SessionId, SimpleSession};
pub use simple_context::SimpleApplicationContext;

use self::error::SessionContextError;

pub type Db<T> = Arc<RwLock<T>>;

/// The application context bundles shared resources.
Expand All @@ -51,7 +54,10 @@ pub trait ApplicationContext: 'static + Send + Sync + Clone {
fn session_context(&self, session: Self::Session) -> Self::SessionContext;

/// Load a session by its id
async fn session_by_id(&self, session_id: SessionId) -> Result<Self::Session>;
async fn session_by_id(
&self,
session_id: SessionId,
) -> Result<Self::Session, ApplicationContextError>;
}

/// The session context bundles resources that are specific to a session.
Expand All @@ -71,13 +77,13 @@ pub trait SessionContext: 'static + Send + Sync + Clone {
fn tasks(&self) -> Self::TaskManager;

/// Create a new query context for executing queries on processors
fn query_context(&self) -> Result<Self::QueryContext>;
fn query_context(&self) -> Result<Self::QueryContext, SessionContextError>;

/// Create a new execution context initializing operators
fn execution_context(&self) -> Result<Self::ExecutionContext>;
fn execution_context(&self) -> Result<Self::ExecutionContext, SessionContextError>;

/// Get the list of available data volumes
fn volumes(&self) -> Result<Vec<Volume>>;
fn volumes(&self) -> Result<Vec<Volume>, SessionContextError>;

/// Get the current session
fn session(&self) -> &Self::Session;
Expand Down
Loading