fix: handle shortening submission using try-catch in background-scripts

This commit is contained in:
abhijithvijayan
2020-02-07 10:40:35 +05:30
parent 87b1522258
commit 3f8c1e1048
2 changed files with 29 additions and 18 deletions

View File

@@ -17,19 +17,33 @@ export type ShortenUrlBodyProperties = {
domain: string;
};
function shortenUrl(params: ShortenUrlBodyProperties): AxiosPromise<any> {
// ToDo: get apikey from local storage
return api({
method: 'POST',
timeout: constants.SHORTEN_URL_TIMEOUT,
url: `/api/v2/links`,
headers: {
'X-API-Key': 'replace-with-api-key',
},
data: {
...params,
},
});
async function shortenUrl(params: ShortenUrlBodyProperties): AxiosPromise<any> {
try {
// ToDo: get apikey from local storage
const { data } = await api({
method: 'POST',
timeout: constants.SHORTEN_URL_TIMEOUT,
url: `/api/v2/links`,
headers: {
'X-API-Key': 'replace-with-api-key',
},
data: {
...params,
},
});
console.log(data);
} catch (err) {
if (err.response) {
if (err.response.status === 401) {
// 'Error: Invalid API Key'
}
}
if (err.code === 'ECONNABORTED') {
// timed-out
}
}
}
function getUserSettings(apikey: string): AxiosPromise<any> {

View File

@@ -105,11 +105,8 @@ const PopupForm = withFormik<PopupFormProperties, FormValuesProperties>({
domain, // ToDo: validate this
};
try {
await messageUtil.send(SHORTEN_URL, data);
} catch (err) {
console.log(err);
}
const response = await messageUtil.send(SHORTEN_URL, data);
console.log(response);
setSubmitting(false);
},