Skip to content

Commit

Permalink
Improve getDapp method error handling (#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobo-k2 authored Jun 7, 2024
1 parent b497d25 commit 890c8a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum HttpCodes {
NotFound = 404,
}
19 changes: 16 additions & 3 deletions src/staking-v3/logic/repositories/DappStakingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { Guard } from 'src/v2/common';
import { ethers } from 'ethers';
import { AnyTuple, Codec } from '@polkadot/types/types';
import { u8aToNumber } from '@polkadot/util';
import { HttpCodes } from 'src/constants';

@injectable()
export class DappStakingRepository implements IDappStakingRepository {
Expand All @@ -65,13 +66,25 @@ export class DappStakingRepository implements IDappStakingRepository {
}

//* @inheritdoc
public async getDapp(network: string, dappAddress: string, forEdit = false): Promise<Dapp> {
public async getDapp(
network: string,
dappAddress: string,
forEdit = false
): Promise<Dapp | undefined> {
Guard.ThrowIfUndefined(network, 'network');
Guard.ThrowIfUndefined(dappAddress, 'dappAddress');
const url = `${TOKEN_API_URL}/v1/${network.toLowerCase()}/dapps-staking/dapps/${dappAddress}?forEdit=${forEdit}`;
const response = await axios.get<Dapp>(url);

return response.data;
try {
const response = await axios.get<Dapp>(url);
return response.data;
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === HttpCodes.NotFound) {
return undefined;
}

throw error;
}
}

//* @inheritdoc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface IDappStakingRepository {
* @param forEdit Flag to indicate if dapp data should be fetched with encoded images.
* @returns A promise that resolves to a dapp data.
*/
getDapp(network: string, dappAddress: string, forEdit?: boolean): Promise<Dapp>;
getDapp(network: string, dappAddress: string, forEdit?: boolean): Promise<Dapp | undefined>;

/**
* Gets protocol state for the given network.
Expand Down

0 comments on commit 890c8a5

Please sign in to comment.