mirror of
https://github.com/SinTan1729/chhoto-url
synced 2025-04-28 20:06:53 -05:00
chg: Match using rusqlite errors instead of strings
This commit is contained in:
parent
c23fcdc9bd
commit
4eb88f3beb
1 changed files with 4 additions and 2 deletions
|
@ -6,7 +6,7 @@ use actix_web::HttpRequest;
|
||||||
use nanoid::nanoid;
|
use nanoid::nanoid;
|
||||||
use rand::seq::IndexedRandom;
|
use rand::seq::IndexedRandom;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rusqlite::Connection;
|
use rusqlite::{ffi::SQLITE_CONSTRAINT_UNIQUE, Connection};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::env;
|
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) {
|
match database::add_link(chunks.shortlink.clone(), chunks.longlink, db) {
|
||||||
Ok(_) => (true, chunks.shortlink),
|
Ok(_) => (true, chunks.shortlink),
|
||||||
Err(error) => {
|
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!"))
|
(false, String::from("Short URL is already in use!"))
|
||||||
} else {
|
} else {
|
||||||
// This should be super rare
|
// This should be super rare
|
||||||
|
|
Loading…
Reference in a new issue