freechat/json-api/db/src/common.rs
shockrah ef73b39f4f + Adding db-lib::common::try_error_passthrough
This is more heavily used in Message::send as a way of discerning "real" server errors
from those that are caused  by user input. The name itself might not be super fitting either

* Moving code for inserting textual messages into its own function
This splits up the logic a bit for Message::send but this segment of logic is also
much simpler than that of file upload

* Flattening Message::send overall
Keeping this function as flat as can be is crucial as it is one of the heavier+most
important funcs in the whole JSON API codebase

Files are now also generated correctly and asynchronously
2021-04-21 16:08:23 -07:00

46 lines
1.0 KiB
Rust

use crate::Response;
use mysql_async::Error;
#[macro_export]
macro_rules! no_conn {
($spec:literal) => {
format!("[ CON Error ] : {}", $spec)
}
}
#[macro_export]
macro_rules! sql_err {
($spec:literal) => {
format!("[ SQL Error ] : {}", $spec)
};
// Using this mostly to pull in sql err types from lib to outside world for logging
($exp:expr) => {
format!("[ SQL Error ] : {}", $exp)
}
}
#[macro_export]
macro_rules! sql_err_log {
($spec:expr) => {
println!($spec);
}
}
pub fn try_error_passthrough<T>(err: Error) -> Result<Response<T>, Error> {
// Some user input _will_ cause sql to complain about things like foreign key
// constraints
// In order to translate 500's into more precise 400's we use this function
// (sparingly)
match &err {
Error::Server(se) => {
if se.code == 1452 {
return Ok(Response::RestrictedInput(format!("Invalid key value given")))
}
return Err(err);
},
_ => Err(err)
}
}