Skip to content

Commit

Permalink
Merge pull request #166 from supabase/chore/firebase-fdw-error
Browse files Browse the repository at this point in the history
chore(firebase_fdw): remove a few unwrap calls
  • Loading branch information
imor authored Oct 4, 2023
2 parents ab54506 + 519d761 commit 0c4e317
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 10 additions & 6 deletions wrappers/src/fdw/firebase_fdw/firebase_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,14 @@ impl FirebaseFdw {
_ => {
// match for firestore documents
// ref: https://firebase.google.com/docs/firestore/reference/rest/v1beta1/projects.databases.documents/listDocuments
let re = Regex::new(r"^firestore/(?P<collection>[^/]+)").unwrap();
let re = Regex::new(r"^firestore/(?P<collection>[^/]+)").expect("regex is valid");
if let Some(caps) = re.captures(obj) {
let base_url =
require_option_or("base_url", options, Self::DEFAULT_FIRESTORE_BASE_URL);
let collection = caps.name("collection").unwrap().as_str();
let collection = caps
.name("collection")
.expect("`collection` capture group always exists in a match")
.as_str();
let mut ret = format!(
"{}/{}/databases/(default)/documents/{}?pageSize={}",
base_url,
Expand Down Expand Up @@ -241,13 +244,13 @@ impl ForeignDataWrapper<FirebaseFdwError> for FirebaseFdw {
// create client
let mut headers = header::HeaderMap::new();
let value = format!("Bearer {}", token);
let mut auth_value = header::HeaderValue::from_str(&value).unwrap();
let mut auth_value = header::HeaderValue::from_str(&value)
.map_err(|_| FirebaseFdwError::InvalidApiKeyHeader)?;
auth_value.set_sensitive(true);
headers.insert(header::AUTHORIZATION, auth_value);
let client = reqwest::Client::builder()
.default_headers(headers)
.build()
.unwrap();
.build()?;
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
let client = ClientBuilder::new(client)
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
Expand All @@ -270,7 +273,8 @@ impl ForeignDataWrapper<FirebaseFdwError> for FirebaseFdw {
let obj = require_option("object", options)?;
let row_cnt_limit = options
.get("limit")
.map(|n| n.parse::<usize>().unwrap())
.map(|n| n.parse::<usize>())
.transpose()?
.unwrap_or(Self::DEFAULT_ROWS_LIMIT);

self.scan_result = Vec::new();
Expand Down
12 changes: 11 additions & 1 deletion wrappers/src/fdw/firebase_fdw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod tests;

use pgrx::pg_sys::panic::ErrorReport;
use pgrx::prelude::PgSqlErrorCode;
use std::num::ParseIntError;
use thiserror::Error;

use supabase_wrappers::prelude::{CreateRuntimeError, OptionsError};
Expand Down Expand Up @@ -37,8 +38,17 @@ enum FirebaseFdwError {
#[error("{0}")]
OptionsError(#[from] OptionsError),

#[error("invalid api_key header")]
InvalidApiKeyHeader,

#[error("request failed: {0}")]
RequestError(#[from] reqwest_middleware::Error),
RequestError(#[from] reqwest::Error),

#[error("request middleware failed: {0}")]
RequestMiddlewareError(#[from] reqwest_middleware::Error),

#[error("`limit` option must be an integer: {0}")]
LimitOptionParseError(#[from] ParseIntError),

#[error("parse JSON response failed: {0}")]
JsonParseError(#[from] serde_json::Error),
Expand Down

0 comments on commit 0c4e317

Please sign in to comment.