mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-27 06:46:01 +00:00
fix(api): answer 200 when analytics hold no record for a package
Analytics live in memory and restart empty. A package with no record yet returned 404, which every browser logs as a failed request on the Downloads tab. The endpoint now returns a null package.
This commit is contained in:
@@ -97,13 +97,9 @@ func (a *App) handleAnalyticsPackageStats(c fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
stats, exists := a.analyticsEngine.GetPackageStats(registry, name)
|
// Analytics live in memory and restart empty, so a package with no record yet is a normal
|
||||||
if !exists {
|
// answer, not a missing resource: a 404 here shows up as a failed request in every client.
|
||||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
stats, _ := a.analyticsEngine.GetPackageStats(registry, name)
|
||||||
"error": "package not found in analytics",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
"package": stats,
|
"package": stats,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -265,7 +265,9 @@ func TestPackageAnalyticsAcceptsNamesWithSlashes(t *testing.T) {
|
|||||||
t.Errorf("analytics for %q = %s, want one download", name, body)
|
t.Errorf("analytics for %q = %s, want one download", name, body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if code, _, _ := doRequest(t, a, "/api/analytics/package/npm/@types/missing", nil); code != http.StatusNotFound {
|
// A package with no record yet is a normal answer; a 404 would show as a failed request in the browser.
|
||||||
t.Errorf("unknown package = %d, want 404", code)
|
out, body := getJSON(t, a, "/api/analytics/package/npm/@types/missing")
|
||||||
|
if pkg, present := out["package"]; !present || pkg != nil {
|
||||||
|
t.Errorf("unknown package = %s, want 200 with a null package", body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user