error handling
This commit is contained in:
parent
2544143809
commit
48476d5cf4
3 changed files with 38 additions and 9 deletions
|
@ -13,6 +13,7 @@ use pacco::pkg::arch::Architecture;
|
|||
use pacco::pkg::{Package, Repository};
|
||||
|
||||
use pacco::config::Config;
|
||||
use serde_json::json;
|
||||
|
||||
pub mod push;
|
||||
pub mod ui;
|
||||
|
@ -174,3 +175,15 @@ pub fn is_repo_db(pkg_name: &str) -> bool {
|
|||
|| pkg_name.ends_with("db.tar.gz.sig")
|
||||
|| pkg_name.ends_with("db.sig")
|
||||
}
|
||||
|
||||
pub fn no_such_repo_error() -> serde_json::Value {
|
||||
json!({
|
||||
"err": "No such repository"
|
||||
})
|
||||
}
|
||||
|
||||
pub fn no_such_pkg_error() -> serde_json::Value {
|
||||
json!({
|
||||
"err": "No such package"
|
||||
})
|
||||
}
|
||||
|
|
|
@ -11,23 +11,38 @@ use rocket::{State, get};
|
|||
use pacco::pkg::{Package, Repository, arch::Architecture, find_package_by_name};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::routes::{no_such_pkg_error, no_such_repo_error};
|
||||
|
||||
use super::take_out;
|
||||
|
||||
/// Package API Endpoint
|
||||
#[get("/json/pkg/<repo>/<pkg_name>?<ver>&<arch>")]
|
||||
pub async fn pkg_json(repo: &str, pkg_name: &str, ver: Option<&str>, arch: Option<&str>) -> serde_json::Value {
|
||||
let repository = Repository::new(repo).unwrap();
|
||||
let mut pkg = repository.get_pkg_by_name(pkg_name).unwrap();
|
||||
pub async fn pkg_json(
|
||||
repo: &str,
|
||||
pkg_name: &str,
|
||||
ver: Option<&str>,
|
||||
arch: Option<&str>,
|
||||
) -> Result<serde_json::Value, serde_json::Value> {
|
||||
let repository = Repository::new(repo).ok_or_else(no_such_repo_error)?;
|
||||
let mut pkg = repository
|
||||
.get_pkg_by_name(pkg_name)
|
||||
.ok_or_else(no_such_pkg_error)?;
|
||||
|
||||
if let Some(ver) = ver {
|
||||
let (version, rel) = Package::version(ver);
|
||||
pkg = pkg.get_version(&version);
|
||||
pkg.rel = rel;
|
||||
if !pkg.exists() {
|
||||
return Err(no_such_pkg_error());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(arch) = arch {
|
||||
let arch = Architecture::parse(arch).unwrap();
|
||||
pkg = pkg.switch_arch(arch);
|
||||
if !pkg.exists() {
|
||||
return Err(no_such_pkg_error());
|
||||
}
|
||||
}
|
||||
|
||||
let versions = pkg.versions();
|
||||
|
@ -35,14 +50,14 @@ pub async fn pkg_json(repo: &str, pkg_name: &str, ver: Option<&str>, arch: Optio
|
|||
let arch = pkg.arch();
|
||||
let pkginfo = pkg.pkginfo();
|
||||
|
||||
json!({
|
||||
Ok(json!({
|
||||
"repo": repo,
|
||||
"pkg": pkg_name,
|
||||
"versions": versions,
|
||||
"version": version,
|
||||
"arch": arch.iter().map(|x| x.to_string()).collect::<Vec<_>>(),
|
||||
"info": pkginfo
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
/// Package overview UI
|
||||
|
|
|
@ -8,6 +8,7 @@ use rocket::{State, get};
|
|||
use pacco::pkg::{Repository, arch::Architecture};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::routes::no_such_repo_error;
|
||||
use crate::routes::ui::arch_card;
|
||||
use pacco::config::Config;
|
||||
|
||||
|
@ -18,18 +19,18 @@ pub async fn repo_arch_json(
|
|||
ctx: RequestContext,
|
||||
arch: &str,
|
||||
config: &State<Config>,
|
||||
) -> serde_json::Value {
|
||||
) -> Result<serde_json::Value, serde_json::Value> {
|
||||
let arch = Architecture::parse(arch).unwrap_or(Architecture::any);
|
||||
let repo_name = repo;
|
||||
|
||||
let repo = Repository::new(repo_name).unwrap();
|
||||
let repo = Repository::new(repo_name).ok_or_else(no_such_repo_error)?;
|
||||
let packages = repo.list_pkg_arch(arch.clone());
|
||||
|
||||
json!({
|
||||
Ok(json!({
|
||||
"repo": repo_name,
|
||||
"arch": arch.to_string(),
|
||||
"packages": packages
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
/// Repository Overview UI
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue