Skip to content

Commit

Permalink
better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhartha-gadgil committed Oct 1, 2024
1 parent 2ba60fa commit 4261c7c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions LeanSearchClient/LoogleSyntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def getLoogleQueryJson (s : String) (num_results : Nat := 6) :
let q := apiUrl ++ s!"?q={s'}"
let s ← IO.Process.output {cmd := "curl", args := #["-X", "GET", "--user-agent", ← useragent, q]}
match Json.parse s.stdout with
| Except.error e =>
IO.throwServerError s!"Could not parse JSON from {s.stdout}; error: {e}"
| Except.error _ =>
IO.throwServerError s!"Could not contact Loogle server"
| Except.ok js =>
let result? := js.getObjValAs? Json "hits" |>.toOption
let result? := result?.filter fun js => js != Json.null
Expand Down
10 changes: 6 additions & 4 deletions LeanSearchClient/Syntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ def getLeanSearchQueryJson (s : String) (num_results : Nat := 6) : CoreM <| Arra
let s' := System.Uri.escapeUri s
let q := apiUrl ++ s!"?query={s'}&num_results={num_results}"
let s ← IO.Process.output {cmd := "curl", args := #["-X", "GET", "--user-agent", ← useragent, q]}
let js := Json.parse s.stdout |>.toOption |>.get!
return js.getArr? |>.toOption |>.get!
let js ← match Json.parse s.stdout |>.toOption with
| some js => pure js
| none => IO.throwServerError s!"Could not contact LeanSearch server"
return js.getArr? |>.toOption |>.getD #[]

def getMoogleQueryJson (s : String) (num_results : Nat := 6) : CoreM <| Array Json := do
let apiUrl := "https://www.moogle.ai/api/search"
let data := Json.arr
#[Json.mkObj [("isFind", false), ("contents", s)]]
let s ← IO.Process.output {cmd := "curl", args := #[apiUrl, "-H", "content-type: application/json", "--user-agent", ← useragent, "--data", data.pretty]}
match Json.parse s.stdout with
| Except.error e =>
IO.throwServerError s!"Could not parse JSON from {s.stdout}; error: {e}"
| Except.error _ =>
throwError m!"Could not contact Moogle server"
| Except.ok js =>
let result? := js.getObjValAs? Json "data"
match result? with
Expand Down

0 comments on commit 4261c7c

Please sign in to comment.