Skip to content

Commit

Permalink
feat: improve logging for extractor
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 5, 2024
1 parent 8cba41f commit 9f45c94
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
25 changes: 18 additions & 7 deletions apps/backend/src/api/hex_repo.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,30 @@ 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
}

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")
Expand All @@ -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) {
Expand Down
20 changes: 16 additions & 4 deletions apps/backend/src/backend/gleam/generate/types.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 9f45c94

Please sign in to comment.