Skip to content

Commit 9588695

Browse files
committed
handle error if use wrong env vars
1 parent 8246b5e commit 9588695

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
},
4040
"dependencies": {
4141
"@iconify/svelte": "^3.0.1",
42+
"axios": "^1.2.0",
4243
"clsx": "^1.2.1",
4344
"crypto-js": "^4.1.1"
4445
}

pnpm-lock.yaml

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/ShortURL.svelte

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { getShortLink } from '../utils/shortLink'
44
import Button from './Button.svelte'
55
import CopyToClipboard from './CopyToClipboard.svelte'
6+
import { AxiosError } from 'axios'
67
78
interface $$Props {
89
campaign: Campaign
@@ -58,12 +59,19 @@
5859
}
5960
6061
async function generateShortUrl(): Promise<void> {
61-
const fullUrl = generateFullUrl(campaign)
62-
if (!fullUrl) {
63-
return
62+
try {
63+
const fullUrl = generateFullUrl(campaign)
64+
if (!fullUrl) {
65+
return
66+
}
67+
const shortLink = await getShortLink(fullUrl)
68+
shortUrl = shortLink
69+
} catch (err) {
70+
if (err instanceof AxiosError) {
71+
console.error(err.response?.data.error)
72+
alert(err.response?.data.error)
73+
}
6474
}
65-
const shortLink = await getShortLink(fullUrl)
66-
shortUrl = shortLink
6775
}
6876
6977
$: fullUrl = generateFullUrl(campaign)

src/utils/shortLink.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
import axios from 'axios'
2+
13
export async function getShortLink(url: string): Promise<string> {
2-
const data = {
4+
const body = {
35
domain: import.meta.env.VITE_SHORT_IO_DOMAIN,
46
originalURL: url,
57
}
68

7-
const response = await fetch('https://api.short.io/links/public', {
8-
method: 'post',
9+
const { data } = await axios.post('https://api.short.io/links/public', body, {
910
headers: {
1011
accept: 'application/json',
1112
'Content-Type': 'application/json',
1213
authorization: import.meta.env.VITE_SHORT_IO_PUBLIC_KEY,
1314
},
14-
body: JSON.stringify(data),
1515
})
1616

17-
const json = await response.json()
18-
return json['shortURL']
17+
return data['shortURL']
1918
}

0 commit comments

Comments
 (0)