1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2025-04-28 11:56:52 -05:00

fix: Better response on error when shortlink is not provided

This commit is contained in:
Sayantan Santra 2025-04-23 15:45:41 -05:00
parent 4eb88f3beb
commit 084334cd11
Signed by: SinTan1729
GPG key ID: 0538DD402EA50898

View file

@ -123,9 +123,12 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
len = 4; len = 4;
} }
if chunks.shortlink.is_empty() { let shortlink_provided = if chunks.shortlink.is_empty() {
chunks.shortlink = gen_link(style, len); chunks.shortlink = gen_link(style, len);
} false
} else {
true
};
if validate_link(chunks.shortlink.as_str()) { if validate_link(chunks.shortlink.as_str()) {
match database::add_link(chunks.shortlink.clone(), chunks.longlink, db) { match database::add_link(chunks.shortlink.clone(), chunks.longlink, db) {
@ -133,6 +136,7 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
Err(error) => { Err(error) => {
if error.sqlite_error().map(|err| err.extended_code) if error.sqlite_error().map(|err| err.extended_code)
== Some(SQLITE_CONSTRAINT_UNIQUE) == Some(SQLITE_CONSTRAINT_UNIQUE)
&& shortlink_provided
{ {
(false, String::from("Short URL is already in use!")) (false, String::from("Short URL is already in use!"))
} else { } else {