Skip to content

Commit 7e1604c

Browse files
committed
Don't let external did resolution swallow every getRecord error
1 parent dd16c86 commit 7e1604c

File tree

1 file changed

+71
-65
lines changed

1 file changed

+71
-65
lines changed

pegasus/lib/api/repo/getRecord.ml

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,70 +11,76 @@ type response = {uri: string; cid: string; value: Mist.Lex.repo_record}
1111
let handler =
1212
Xrpc.handler (fun ctx ->
1313
let input = Xrpc.parse_query ctx.req query_of_yojson in
14-
try%lwt
15-
let%lwt input_did = Xrpc.resolve_repo_did ctx input.repo in
16-
let uri =
17-
Util.make_at_uri ~repo:input_did ~collection:input.collection
18-
~rkey:input.rkey ~fragment:None
19-
in
20-
let%lwt repo = Repository.load ~ensure_active:true input_did in
21-
let path = input.collection ^ "/" ^ input.rkey in
22-
match%lwt Repository.get_record repo path with
23-
| Some {cid; value; _}
24-
when input.cid = None || input.cid = Some (Cid.to_string cid) ->
25-
Dream.json @@ Yojson.Safe.to_string
26-
@@ response_to_yojson {uri; cid= Cid.to_string cid; value}
27-
| _ ->
28-
Errors.internal_error ~name:"RecordNotFound"
29-
~msg:("could not find record " ^ uri)
30-
()
31-
with _ -> (
32-
let%lwt input_did =
33-
if String.starts_with ~prefix:"did:" input.repo then
34-
Lwt.return input.repo
35-
else
36-
match%lwt Id_resolver.Handle.resolve input.repo with
37-
| Ok did ->
38-
Lwt.return did
14+
let%lwt input_did =
15+
Lwt_result.catch @@ fun () -> Xrpc.resolve_repo_did ctx input.repo
16+
in
17+
match input_did with
18+
| Ok input_did -> (
19+
let uri =
20+
Util.make_at_uri ~repo:input_did ~collection:input.collection
21+
~rkey:input.rkey ~fragment:None
22+
in
23+
let%lwt repo = Repository.load ~ensure_active:true input_did in
24+
let path = input.collection ^ "/" ^ input.rkey in
25+
match%lwt Repository.get_record repo path with
26+
| Some {cid; value; _}
27+
when input.cid = None || input.cid = Some (Cid.to_string cid) ->
28+
Dream.json @@ Yojson.Safe.to_string
29+
@@ response_to_yojson {uri; cid= Cid.to_string cid; value}
30+
| _ ->
31+
Errors.internal_error ~name:"RecordNotFound"
32+
~msg:("could not find record " ^ uri)
33+
() )
34+
| Error _ -> (
35+
let%lwt input_did =
36+
if String.starts_with ~prefix:"did:" input.repo then
37+
Lwt.return input.repo
38+
else
39+
match%lwt Id_resolver.Handle.resolve input.repo with
40+
| Ok did ->
41+
Lwt.return did
42+
| Error _ ->
43+
Errors.invalid_request "failed to resolve repo"
44+
in
45+
let%lwt pds =
46+
match%lwt Id_resolver.Did.resolve input_did with
47+
| Ok doc -> (
48+
Lwt.return
49+
@@
50+
match
51+
Id_resolver.Did.Document.get_service doc "#atproto_pds"
52+
with
53+
| Some service ->
54+
service
55+
| None ->
56+
Errors.invalid_request "failed to resolve repo pds" )
3957
| Error _ ->
40-
Errors.invalid_request "failed to resolve repo"
41-
in
42-
let%lwt pds =
43-
match%lwt Id_resolver.Did.resolve input_did with
44-
| Ok doc -> (
45-
Lwt.return
46-
@@
47-
match Id_resolver.Did.Document.get_service doc "#atproto_pds" with
48-
| Some service ->
49-
service
50-
| None ->
51-
Errors.invalid_request "failed to resolve repo pds" )
52-
| Error _ ->
53-
Errors.invalid_request "failed to resolve repo did document"
54-
in
55-
if pds = Env.host_endpoint then
56-
Errors.internal_error ~name:"RecordNotFound"
57-
~msg:("could not resolve user " ^ input.repo)
58-
() ;
59-
let get_uri = Uri.of_string pds in
60-
let get_uri =
61-
Uri.with_path get_uri "/xrpc/com.atproto.repo.getRecord"
62-
in
63-
let get_uri = Uri.with_query get_uri (Util.copy_query ctx.req) in
64-
let%lwt res, body =
65-
Util.http_get get_uri
66-
~headers:(Cohttp.Header.of_list [("Accept", "application/json")])
67-
in
68-
match res.status with
69-
| `OK ->
70-
let%lwt json = Cohttp_lwt.Body.to_string body in
71-
let%lwt () = Cohttp_lwt.Body.drain_body body in
72-
Dream.json json
73-
| _ ->
74-
let%lwt () = Cohttp_lwt.Body.drain_body body in
58+
Errors.invalid_request "failed to resolve repo did document"
59+
in
60+
if pds = Env.host_endpoint then
7561
Errors.internal_error ~name:"RecordNotFound"
76-
~msg:
77-
( "could not find record "
78-
^ Util.make_at_uri ~repo:input.repo ~collection:input.collection
79-
~rkey:input.rkey ~fragment:None )
80-
() ) )
62+
~msg:("could not resolve user " ^ input.repo)
63+
() ;
64+
let get_uri = Uri.of_string pds in
65+
let get_uri =
66+
Uri.with_path get_uri "/xrpc/com.atproto.repo.getRecord"
67+
in
68+
let get_uri = Uri.with_query get_uri (Util.copy_query ctx.req) in
69+
let%lwt res, body =
70+
Util.http_get get_uri
71+
~headers:(Cohttp.Header.of_list [("Accept", "application/json")])
72+
in
73+
match res.status with
74+
| `OK ->
75+
let%lwt json = Cohttp_lwt.Body.to_string body in
76+
let%lwt () = Cohttp_lwt.Body.drain_body body in
77+
Dream.json json
78+
| _ ->
79+
let%lwt () = Cohttp_lwt.Body.drain_body body in
80+
Errors.internal_error ~name:"RecordNotFound"
81+
~msg:
82+
( "could not find record "
83+
^ Util.make_at_uri ~repo:input.repo
84+
~collection:input.collection ~rkey:input.rkey
85+
~fragment:None )
86+
() ) )

0 commit comments

Comments
 (0)