Skip to content
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

chore(airtable_fdw): refactor error reporting #148

Merged
merged 8 commits into from
Sep 14, 2023
Merged
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
27 changes: 14 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions supabase-wrappers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pg_test = []

[dependencies]
pgrx = {version = "=0.9.8", default-features = false }
thiserror = "1.0.48"
tokio = { version = "1.24", features = ["rt"] }
uuid = { version = "1.2.2" }
supabase-wrappers-macros = { version = "0.1", path = "../supabase-wrappers-macros" }
Expand Down
25 changes: 22 additions & 3 deletions supabase-wrappers/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!

use crate::interface::{Cell, Column, Row};
use pgrx::pg_sys::panic::ErrorReport;
use pgrx::prelude::PgBuiltInOids;
use pgrx::spi::Spi;
use pgrx::IntoDatum;
Expand All @@ -10,6 +11,7 @@ use std::collections::HashMap;
use std::ffi::CStr;
use std::num::NonZeroUsize;
use std::ptr;
use thiserror::Error;
use tokio::runtime::{Builder, Runtime};
use uuid::Uuid;

Expand Down Expand Up @@ -106,6 +108,19 @@ pub fn report_error(code: PgSqlErrorCode, msg: &str) {
ereport!(PgLogLevel::ERROR, code, msg, "Wrappers");
}

#[derive(Error, Debug)]
pub enum CreateRuntimeError {
#[error("failed to create async runtime: {0}")]
FailedToCreateAsyncRuntime(#[from] std::io::Error),
}

impl From<CreateRuntimeError> for ErrorReport {
fn from(value: CreateRuntimeError) -> Self {
let error_message = format!("{value}");
ErrorReport::new(PgSqlErrorCode::ERRCODE_FDW_ERROR, error_message, "")
}
}

/// Create a Tokio async runtime
///
/// Use this runtime to run async code in `block` mode. Run blocked code is
Expand All @@ -115,6 +130,8 @@ pub fn report_error(code: PgSqlErrorCode, msg: &str) {
/// For example,
///
/// ```rust,no_run
/// # use supabase_wrappers::utils::CreateRuntimeError;
/// # fn main() -> Result<(), CreateRuntimeError> {
/// # use supabase_wrappers::prelude::create_async_runtime;
/// # struct Client {
/// # }
Expand All @@ -123,17 +140,19 @@ pub fn report_error(code: PgSqlErrorCode, msg: &str) {
/// # }
/// # let client = Client {};
/// # let sql = "";
/// let rt = create_async_runtime();
/// let rt = create_async_runtime()?;
///
/// // client.query() is an async function returning a Result
/// match rt.block_on(client.query(&sql)) {
/// Ok(result) => { }
/// Err(err) => { }
/// }
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn create_async_runtime() -> Runtime {
Builder::new_current_thread().enable_all().build().unwrap()
pub fn create_async_runtime() -> Result<Runtime, CreateRuntimeError> {
Ok(Builder::new_current_thread().enable_all().build()?)
}

/// Get required option value from the `options` map
Expand Down
10 changes: 6 additions & 4 deletions wrappers/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions wrappers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ pg15 = ["pgrx/pg15", "pgrx-tests/pg15", "supabase-wrappers/pg15" ]
pg_test = []

helloworld_fdw = []
bigquery_fdw = ["gcp-bigquery-client", "serde_json", "serde", "wiremock", "futures", "yup-oauth2"]
clickhouse_fdw = ["clickhouse-rs", "chrono", "chrono-tz", "regex"]
stripe_fdw = ["reqwest", "reqwest-middleware", "reqwest-retry", "serde_json"]
firebase_fdw = ["reqwest", "reqwest-middleware", "reqwest-retry", "serde_json", "yup-oauth2", "regex"]
bigquery_fdw = ["gcp-bigquery-client", "serde_json", "serde", "wiremock", "futures", "yup-oauth2", "thiserror"]
clickhouse_fdw = ["clickhouse-rs", "chrono", "chrono-tz", "regex", "thiserror"]
stripe_fdw = ["reqwest", "reqwest-middleware", "reqwest-retry", "serde_json", "thiserror"]
firebase_fdw = ["reqwest", "reqwest-middleware", "reqwest-retry", "serde_json", "yup-oauth2", "regex", "thiserror"]
s3_fdw = [
"reqwest", "reqwest-middleware", "reqwest-retry", "aws-config", "aws-sdk-s3",
"tokio", "tokio-util", "csv", "async-compression", "serde_json",
"http", "parquet", "futures", "arrow-array", "chrono"
]
airtable_fdw = ["reqwest", "reqwest-middleware", "reqwest-retry", "serde_json", "serde", "url"]
logflare_fdw = ["reqwest", "reqwest-middleware", "reqwest-retry", "serde_json"]
airtable_fdw = ["reqwest", "reqwest-middleware", "reqwest-retry", "serde_json", "serde", "url", "thiserror"]
logflare_fdw = ["reqwest", "reqwest-middleware", "reqwest-retry", "serde_json", "thiserror"]

# Does not include helloworld_fdw because of its general uselessness
all_fdws = ["airtable_fdw", "bigquery_fdw", "clickhouse_fdw", "stripe_fdw", "firebase_fdw", "s3_fdw", "logflare_fdw"]
Expand Down Expand Up @@ -72,6 +72,8 @@ http = { version = "0.2", optional = true }
parquet = { version = "41.0.0", features = ["async"], optional = true }
arrow-array = { version = "41.0.0", optional = true }

thiserror = { version = "1.0.48", optional = true }

[dev-dependencies]
pgrx-tests = "=0.9.8"

Expand Down
Loading
Loading