Skip to content

Commit

Permalink
feat: Gracefully handle HTTP errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alainncls committed Apr 4, 2024
1 parent 5926f28 commit 0946d18
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/Consensys/lxp-snap"
},
"source": {
"shasum": "2Rb/XsEPUbLi9zfXyRyrpG6JzNOFW5pEVsBHtL7LjqI=",
"shasum": "JUxqhuky+NCVJkWCBgLsTy5QKc+p29BebqUrfuw4TvI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
9 changes: 8 additions & 1 deletion packages/snap/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const getData = async (url: string) => {
method: 'GET',
});

if (!response.ok) {
console.error(`Call to ${url} failed with status ${response.status}`);
throw new Error(`HTTP error! Status: ${response.status}`);
}

return response.json();
};

Expand Down Expand Up @@ -55,5 +60,7 @@ export const postAddressRegistration = async (
}

const body = await response.json();
return { status: 'error', message: body.message ?? 'Unknown error' };
const message = body.message ?? 'Unknown error';
console.error(`Failed to register address: ${message.toString()}`);

Check failure on line 64 in packages/snap/src/api.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint

Invalid type "any" of template literal expression
return { status: 'error', message };
};
25 changes: 20 additions & 5 deletions packages/snap/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ import { convertBalanceToDisplay, getState } from './utils';
* @returns The LXP balance for the address.
*/
async function getLxpBalanceFromLineascan(address: string) {
const rawBalance = await fetchBalanceFromLineascan(address);
return convertBalanceToDisplay(rawBalance);
let rawBalance;

try {
rawBalance = await fetchBalanceFromLineascan(address);
return convertBalanceToDisplay(rawBalance);
} catch (error) {
return 0;
}
}

/**
Expand Down Expand Up @@ -65,16 +71,25 @@ export async function getPohStatus(address: string) {
if (!address) {
return false;
}
const pohStatus = await fetchPohStatus(address);
return pohStatus.poh as boolean;

try {
const pohStatus = await fetchPohStatus(address);
return pohStatus.poh as boolean;
} catch (error) {
return false;
}
}

/**
* Get the current activations.
* @returns The current activations.
*/
export async function getCurrentActivations() {
return fetchLxpActivations();
try {
return fetchLxpActivations();
} catch (error) {
return [];
}
}

/**
Expand Down

0 comments on commit 0946d18

Please sign in to comment.