summaryrefslogtreecommitdiff
path: root/src/handlers/ships.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/handlers/ships.rs')
-rw-r--r--src/handlers/ships.rs19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/handlers/ships.rs b/src/handlers/ships.rs
index ddb81a6..75f5dc5 100644
--- a/src/handlers/ships.rs
+++ b/src/handlers/ships.rs
@@ -1,10 +1,11 @@
use std::sync::Arc;
use axum::{Json, extract::Path, Extension};
+use hyper::StatusCode;
use solarlib::ship::{Ship, DbShip, Sha256};
-use sqlx::{query_as, query};
+use sqlx::{query_as, query, Error as SqlxError};
-use crate::{errors::{JsonResult, StringResult}, State};
+use crate::{errors::{JsonResult, StringResult, ServiceError}, State};
pub async fn list(state: Extension<Arc<State>>) -> JsonResult<Json<Vec<Ship>>> {
@@ -38,7 +39,19 @@ pub async fn delete(Path(shasum): Path<Sha256>, state: Extension<Arc<State>>) ->
pub async fn get(Path(shasum): Path<Sha256>, state: Extension<Arc<State>>) -> JsonResult<Json<DbShip>> {
let mut conn = state.conn.acquire().await?;
- let db_ship = query_as!(DbShip, "SELECT * FROM ships WHERE shasum=$1", shasum.to_string()).fetch_one(&mut conn).await?;
+ let db_ship = match query_as!(DbShip, "SELECT * FROM ships WHERE shasum=$1", shasum.to_string()).fetch_one(&mut conn).await {
+ Ok(d) => d,
+ Err(e) => {
+ match e {
+ SqlxError::RowNotFound => {
+ return Err(ServiceError::NotFound)
+ },
+ _ => {
+ return Err(e.into());
+ }
+ }
+ },
+ };
Ok(Json(db_ship))
}