return descriptive errors when saving bots

This commit is contained in:
Ilion Beyst 2022-03-06 16:53:02 +01:00
parent bd5dd17eb9
commit 148178a344

View file

@ -1,11 +1,13 @@
use axum::body;
use axum::extract::{Multipart, Path}; use axum::extract::{Multipart, Path};
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json; use axum::Json;
use diesel::OptionalExtension; use diesel::OptionalExtension;
use rand::distributions::Alphanumeric; use rand::distributions::Alphanumeric;
use rand::Rng; use rand::Rng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, value::Value as JsonValue}; use serde_json::{self, json, value::Value as JsonValue};
use std::io::Cursor; use std::io::Cursor;
use std::path::PathBuf; use std::path::PathBuf;
@ -20,16 +22,38 @@ pub struct SaveBotParams {
pub bot_name: String, pub bot_name: String,
pub code: String, pub code: String,
} }
pub enum SaveBotError {
BotNameTaken,
}
impl IntoResponse for SaveBotError {
fn into_response(self) -> Response {
let (status, value) = match self {
SaveBotError::BotNameTaken => {
(StatusCode::FORBIDDEN, json!({ "error": "BotNameTaken" }))
}
};
let encoded = serde_json::to_vec(&value).expect("could not encode response value");
Response::builder()
.status(status)
.body(body::boxed(body::Full::from(encoded)))
.expect("could not build response")
}
}
pub async fn save_bot( pub async fn save_bot(
Json(params): Json<SaveBotParams>, Json(params): Json<SaveBotParams>,
conn: DatabaseConnection, conn: DatabaseConnection,
) -> Result<Json<Bot>, StatusCode> { ) -> Result<Json<Bot>, SaveBotError> {
// TODO: authorization // TODO: authorization
let res = bots::find_bot_by_name(&params.bot_name, &conn) let res = bots::find_bot_by_name(&params.bot_name, &conn)
.optional() .optional()
.expect("could not run query"); .expect("could not run query");
let bot = match res { let bot = match res {
Some(_bot) => return Err(StatusCode::FORBIDDEN), Some(_bot) => return Err(SaveBotError::BotNameTaken),
None => { None => {
let new_bot = bots::NewBot { let new_bot = bots::NewBot {
owner_id: None, owner_id: None,