From 2cd76edf9d418edce85ad624d99e2eb1d4bf267d Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 19 Dec 2022 21:50:14 -0800 Subject: pds: for non-existant DID as an error, not panic --- adenosine-pds/src/bsky.rs | 15 +++++++++++---- adenosine-pds/src/lib.rs | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/adenosine-pds/src/bsky.rs b/adenosine-pds/src/bsky.rs index ed4fe10..d2ae827 100644 --- a/adenosine-pds/src/bsky.rs +++ b/adenosine-pds/src/bsky.rs @@ -60,11 +60,15 @@ pub fn bsky_mutate_db(db: &mut AtpDatabase, did: &Did, mutations: Vec) Ok(()) } +// TODO: should probably return Result>? pub fn bsky_get_profile(srv: &mut AtpService, did: &Did) -> Result { // first get the profile record let mut profile_cid: Option = None; - let commit_cid = &srv.repo.lookup_commit(did)?.unwrap(); - let last_commit = srv.repo.get_commit(commit_cid)?; + let commit_cid = match srv.repo.lookup_commit(did)? { + Some(cid) => cid, + None => Err(anyhow!("repository not found: {}", did))?, + }; + let last_commit = srv.repo.get_commit(&commit_cid)?; let full_map = srv.repo.mst_to_map(&last_commit.mst_cid)?; let prefix = "/app.bsky.actor.profile/"; for (mst_key, cid) in full_map.iter() { @@ -115,8 +119,11 @@ pub fn bsky_get_profile(srv: &mut AtpService, did: &Did) -> Result { pub fn bsky_update_profile(srv: &mut AtpService, did: &Did, profile: ProfileRecord) -> Result<()> { // get the profile record let mut profile_tid: Option = None; - let commit_cid = &srv.repo.lookup_commit(did)?.unwrap(); - let last_commit = srv.repo.get_commit(commit_cid)?; + let commit_cid = match srv.repo.lookup_commit(did)? { + Some(cid) => cid, + None => Err(anyhow!("repository not found: {}", did))?, + }; + let last_commit = srv.repo.get_commit(&commit_cid)?; let full_map = srv.repo.mst_to_map(&last_commit.mst_cid)?; let prefix = "/app.bsky.actor.profile/"; for (mst_key, _cid) in full_map.iter() { diff --git a/adenosine-pds/src/lib.rs b/adenosine-pds/src/lib.rs index 9721652..f855e59 100644 --- a/adenosine-pds/src/lib.rs +++ b/adenosine-pds/src/lib.rs @@ -444,6 +444,7 @@ fn xrpc_get_handler( // TODO did or handle let did = Did::from_str(&xrpc_required_param(request, "actor")?)?; let mut srv = srv.lock().unwrap(); + // TODO: if profile doesn't exist, return a 404 Ok(json!(bsky_get_profile(&mut srv, &did)?)) } "app.bsky.actor.search" => { -- cgit v1.2.3