Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
feat: add error catchers
Browse files Browse the repository at this point in the history
Signed-off-by: ZTL-UwU <[email protected]>
  • Loading branch information
ZTL-UwU committed Dec 24, 2023
1 parent 8ac86ff commit 0dd505c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ cargo run

| 名称 | 类型 | 内容 |
| :----------- | :----- | :--------------------------------------------------------------------- |
| `code` | i32 | 响应代码 |
| `msg` | String | 返回信息 |
| `similarity` | [] | 表示前 5 个相似度最高的论文,每项第一个值表示相似度,第二个值为论文 id |


```json
{
"code": 200,
"msg": "加入成功",
"similarity": [
[
Expand Down
44 changes: 33 additions & 11 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate rocket;

use rocket::fairing::{self, AdHoc};
use rocket::http::Status;
use rocket::serde::json::Json;
use rocket::{Build, Rocket};

Expand All @@ -22,24 +23,44 @@ async fn check(
data: Json<ReqData>,
state: &rocket::State<SharedData>,
conn: Connection<'_, Db>,
) -> Json<ResData> {
) -> (Status, Json<ResData>) {
let req: ReqData = data.into_inner();
let db = conn.into_inner();

match process::similarity(&req, req.write, db, &state.jieba, &state.stop_words).await {
Ok(r) => Json(ResData {
code: 200,
msg: format!("{}成功", if req.write { "加入" } else { "查询" }),
similarity: r,
}),
Err(e) => Json(ResData {
code: 500,
msg: e.to_string(),
similarity: [].to_vec(),
}),
Ok(r) => (
Status::Ok,
Json(ResData {
msg: format!("{}成功", if req.write { "加入" } else { "查询" }),
similarity: Some(r),
}),
),
Err(e) => (
Status::InternalServerError,
Json(ResData {
msg: e.to_string(),
similarity: None,
}),
),
}
}

#[rocket::catch(404)]
fn not_found(req: &rocket::Request) -> Json<ResData> {
Json(ResData {
msg: format!("'{}'不存在", req.uri()),
similarity: None,
})
}

#[rocket::catch(400)]
fn bad_req() -> Json<ResData> {
Json(ResData {
msg: "调用有误".to_string(),
similarity: None,
})
}

async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
let conn = &Db::fetch(&rocket).unwrap().conn;
let _ = migration::Migrator::up(conn, None).await;
Expand All @@ -52,6 +73,7 @@ async fn start() -> Result<(), rocket::Error> {
.attach(Db::init())
.attach(AdHoc::try_on_ignite("Migrations", run_migrations))
.mount("/", rocket::routes![check])
.register("/", rocket::catchers![not_found, bad_req])
.manage(SharedData {
jieba: jieba_rs::Jieba::new(),
stop_words: data::stop_words(),
Expand Down
3 changes: 1 addition & 2 deletions entity/src/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ pub struct ReqData {

#[derive(Serialize, Debug)]
pub struct ResData {
pub code: i16,
pub msg: String,
pub similarity: Vec<(f64, String)>,
pub similarity: Option<Vec<(f64, String)>>,
}

/// Data shared through services as a Rocket state.
Expand Down

0 comments on commit 0dd505c

Please sign in to comment.