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

chg: Match using rusqlite errors instead of strings

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

View file

@ -6,7 +6,7 @@ use actix_web::HttpRequest;
use nanoid::nanoid;
use rand::seq::IndexedRandom;
use regex::Regex;
use rusqlite::Connection;
use rusqlite::{ffi::SQLITE_CONSTRAINT_UNIQUE, Connection};
use serde::{Deserialize, Serialize};
use std::env;
@ -131,7 +131,9 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
match database::add_link(chunks.shortlink.clone(), chunks.longlink, db) {
Ok(_) => (true, chunks.shortlink),
Err(error) => {
if error.to_string() == "UNIQUE constraint failed: urls.short_url" {
if error.sqlite_error().map(|err| err.extended_code)
== Some(SQLITE_CONSTRAINT_UNIQUE)
{
(false, String::from("Short URL is already in use!"))
} else {
// This should be super rare