fix(): Handle invalid URL format error with proper response message

This commit is contained in:
Anthony Al Lazkani 2025-01-03 00:09:24 +02:00
parent 2b2545d348
commit dbfe8c2dc9
3 changed files with 90 additions and 44 deletions

View file

@ -1,11 +1,11 @@
{ {
"name": "frontend", "name": "shortenit",
"version": "0.1.0", "version": "0.1.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "frontend", "name": "shortenit",
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"@testing-library/jest-dom": "^5.17.0", "@testing-library/jest-dom": "^5.17.0",

View file

@ -35,23 +35,66 @@ export default function URLShortener() {
const api_url = `${config.url}${api_endpoint}`; const api_url = `${config.url}${api_endpoint}`;
// Send the POST request to the backend // Send the POST request to the backend
await axios const response = await axios.post(api_url , {url: url})
.post(api_url , { if (response.data.url) {
url: url
})
.then((response) => {
if (response) {
const shortUrl: string = response.data.url; const shortUrl: string = response.data.url;
setShortenedUrl(shortUrl); setShortenedUrl(shortUrl);
setShowInput(true); setShowInput(true);
} toast.success("URL Shortened Successfully", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "dark",
className: "custom-toast",
}); });
} else {
toast.error("Something went wrong while shortening the URL", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "dark",
className: "custom-toast",
});
}
setTimeout(() => { setTimeout(() => {
setShowInput(false); setShowInput(false);
}, 900000); }, 900000);
} catch (error) { } catch (error : any) {
console.log(error); if (error.response) {
const errorMessage : string = error.response.data.msg || "An error occurred. Please try again.";
toast.error(errorMessage, {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "dark",
className: "custom-toast",
});
} else {
toast.error("Network error. Please check your connection and try again.", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
theme: "dark",
className: "custom-toast",
});
}
} }
} }
@ -76,6 +119,7 @@ export default function URLShortener() {
} }
} }
return ( return (
<>
<div className="URLShortener-component"> <div className="URLShortener-component">
<div className="left-side"> <div className="left-side">
<h1>Paste the URL</h1> <h1>Paste the URL</h1>
@ -108,7 +152,9 @@ export default function URLShortener() {
</p> </p>
</div> </div>
</div> </div>
<ToastContainer />
</div> </div>
<ToastContainer />
</>
); );
} }

View file

@ -80,7 +80,7 @@ class SiteHandler:
data = self.shortenit_load_format(data) data = self.shortenit_load_format(data)
except Exception as e: except Exception as e:
self.logger.error(e) self.logger.error(e)
return {}, 400 return {"msg" : "Invalid URL format"}, 400
self.logger.error(e) self.logger.error(e)
abort(400) abort(400)
try: try: