From 9f45c94025ed68c9516776eb07684f884a0e364e Mon Sep 17 00:00:00 2001 From: Guillaume Hivert Date: Sun, 5 May 2024 19:48:50 +0200 Subject: [PATCH] feat: improve logging for extractor Signed-off-by: Guillaume Hivert --- apps/backend/src/api/hex_repo.gleam | 25 +++++++++++++------ .../src/backend/gleam/generate/types.gleam | 20 ++++++++++++--- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/apps/backend/src/api/hex_repo.gleam b/apps/backend/src/api/hex_repo.gleam index c02c825..9e1e0cb 100644 --- a/apps/backend/src/api/hex_repo.gleam +++ b/apps/backend/src/api/hex_repo.gleam @@ -37,13 +37,22 @@ fn create_archives_directory() { archives_path } -fn read_archive(archives_path: String, slug: String) { - let filepath = archives_path <> "/" <> slug - simplifile.read_bits(filepath) +fn read_archive(archives_path: String, name: String, version: String) { + let slug = package_slug(name, version) <> ".tar" + let filepath = archives_path <> "/" <> name <> "/" <> slug + use content <- result.map(simplifile.read_bits(filepath)) + wisp.log_info("Using filesystem for " <> slug) + content } -fn create_archive(archives_path: String, slug: String, archive: BitArray) { - let filepath = archives_path <> "/" <> slug +fn create_archive( + archives_path: String, + name: String, + version: String, + archive: BitArray, +) { + let slug = package_slug(name, version) <> ".tar" + let filepath = archives_path <> "/" <> name <> "/" <> slug let _ = simplifile.write_bits(filepath, archive) archive } @@ -51,7 +60,7 @@ fn create_archive(archives_path: String, slug: String, archive: BitArray) { fn get_tarball(name: String, version: String) { let slug = package_slug(name, version) <> ".tar" use archives_path <- result.try(create_archives_directory()) - use _ <- result.try_recover(read_archive(archives_path, slug)) + use _ <- result.try_recover(read_archive(archives_path, name, version)) wisp.log_info("Querying hex for " <> slug) request.new() |> request.set_host("repo.hex.pm") @@ -61,7 +70,9 @@ fn get_tarball(name: String, version: String) { |> request.set_scheme(http.Https) |> httpc.send_bits() |> result.map_error(error.FetchError) - |> result.map(fn(res) { create_archive(archives_path, slug, res.body) }) + |> result.map(fn(res) { + create_archive(archives_path, name, version, res.body) + }) } fn read_interface(filepath: String) { diff --git a/apps/backend/src/backend/gleam/generate/types.gleam b/apps/backend/src/backend/gleam/generate/types.gleam index c623108..12849ef 100644 --- a/apps/backend/src/backend/gleam/generate/types.gleam +++ b/apps/backend/src/backend/gleam/generate/types.gleam @@ -190,17 +190,29 @@ fn find_type_signature( }) { option.None -> { + let slug = package <> "/" <> module case ctx.package_interface.name == package { - False -> Error(error.UnknownError("No release found")) + // Not the same package, coming from an external package, should wait + // for it to be extracted. It's impossible to get a type hidden by the + // package, should it should work in the long run. + False -> Error(error.UnknownError("No release found for " <> slug)) True -> case dict.get(ctx.package_interface.modules, module) { - Error(_) -> Error(error.UnknownError("No module found")) + // Module is hidden, everything is correct, type is hidden. + Error(_) -> Ok(option.None) + // Module is not hidden, checking if type is hidden by itself. Ok(mod) -> case dict.get(mod.type_aliases, name) { - Ok(_) -> Error(error.UnknownError("No release found")) + // Type is not hidden, returning an error to restart the extraction. + Ok(_) -> + Error(error.UnknownError("No release found for " <> slug)) + // Type is hidden, should check if type defed. Error(_) -> case dict.get(mod.types, name) { - Ok(_) -> Error(error.UnknownError("No release found")) + // Type is not hidden, returning an error to restart the extraction. + Ok(_) -> + Error(error.UnknownError("No release found for " <> slug)) + // Type is hidden, returning None because it can't be extracted. Error(_) -> Ok(option.None) } }