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::pkg::{Package, Repository};
|
||||||
|
|
||||||
use pacco::config::Config;
|
use pacco::config::Config;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
pub mod push;
|
pub mod push;
|
||||||
pub mod ui;
|
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.tar.gz.sig")
|
||||||
|| pkg_name.ends_with("db.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 pacco::pkg::{Package, Repository, arch::Architecture, find_package_by_name};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
|
use crate::routes::{no_such_pkg_error, no_such_repo_error};
|
||||||
|
|
||||||
use super::take_out;
|
use super::take_out;
|
||||||
|
|
||||||
/// Package API Endpoint
|
/// Package API Endpoint
|
||||||
#[get("/json/pkg/<repo>/<pkg_name>?<ver>&<arch>")]
|
#[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 {
|
pub async fn pkg_json(
|
||||||
let repository = Repository::new(repo).unwrap();
|
repo: &str,
|
||||||
let mut pkg = repository.get_pkg_by_name(pkg_name).unwrap();
|
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 {
|
if let Some(ver) = ver {
|
||||||
let (version, rel) = Package::version(ver);
|
let (version, rel) = Package::version(ver);
|
||||||
pkg = pkg.get_version(&version);
|
pkg = pkg.get_version(&version);
|
||||||
pkg.rel = rel;
|
pkg.rel = rel;
|
||||||
|
if !pkg.exists() {
|
||||||
|
return Err(no_such_pkg_error());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(arch) = arch {
|
if let Some(arch) = arch {
|
||||||
let arch = Architecture::parse(arch).unwrap();
|
let arch = Architecture::parse(arch).unwrap();
|
||||||
pkg = pkg.switch_arch(arch);
|
pkg = pkg.switch_arch(arch);
|
||||||
|
if !pkg.exists() {
|
||||||
|
return Err(no_such_pkg_error());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let versions = pkg.versions();
|
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 arch = pkg.arch();
|
||||||
let pkginfo = pkg.pkginfo();
|
let pkginfo = pkg.pkginfo();
|
||||||
|
|
||||||
json!({
|
Ok(json!({
|
||||||
"repo": repo,
|
"repo": repo,
|
||||||
"pkg": pkg_name,
|
"pkg": pkg_name,
|
||||||
"versions": versions,
|
"versions": versions,
|
||||||
"version": version,
|
"version": version,
|
||||||
"arch": arch.iter().map(|x| x.to_string()).collect::<Vec<_>>(),
|
"arch": arch.iter().map(|x| x.to_string()).collect::<Vec<_>>(),
|
||||||
"info": pkginfo
|
"info": pkginfo
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Package overview UI
|
/// Package overview UI
|
||||||
|
|
|
@ -8,6 +8,7 @@ use rocket::{State, get};
|
||||||
use pacco::pkg::{Repository, arch::Architecture};
|
use pacco::pkg::{Repository, arch::Architecture};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
|
use crate::routes::no_such_repo_error;
|
||||||
use crate::routes::ui::arch_card;
|
use crate::routes::ui::arch_card;
|
||||||
use pacco::config::Config;
|
use pacco::config::Config;
|
||||||
|
|
||||||
|
@ -18,18 +19,18 @@ pub async fn repo_arch_json(
|
||||||
ctx: RequestContext,
|
ctx: RequestContext,
|
||||||
arch: &str,
|
arch: &str,
|
||||||
config: &State<Config>,
|
config: &State<Config>,
|
||||||
) -> serde_json::Value {
|
) -> Result<serde_json::Value, serde_json::Value> {
|
||||||
let arch = Architecture::parse(arch).unwrap_or(Architecture::any);
|
let arch = Architecture::parse(arch).unwrap_or(Architecture::any);
|
||||||
let repo_name = repo;
|
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());
|
let packages = repo.list_pkg_arch(arch.clone());
|
||||||
|
|
||||||
json!({
|
Ok(json!({
|
||||||
"repo": repo_name,
|
"repo": repo_name,
|
||||||
"arch": arch.to_string(),
|
"arch": arch.to_string(),
|
||||||
"packages": packages
|
"packages": packages
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Repository Overview UI
|
/// Repository Overview UI
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue