Skip to content

Commit bfbbadb

Browse files
committed
fix(predict): use parse util to catch errors
1 parent 2cf7c45 commit bfbbadb

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

app/components/UI/Predict/controllers/PredictController.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import {
101101
POLYMARKET_PROVIDER_ID,
102102
} from '../providers/polymarket/constants';
103103
import {
104+
DEFAULT_FEE_COLLECTION_FLAG,
104105
DEFAULT_LIVE_SPORTS_FLAG,
105106
DEFAULT_MARKET_HIGHLIGHTS_FLAG,
106107
} from '../constants/flags';
@@ -115,8 +116,7 @@ import {
115116
validatedVersionGatedFeatureFlag,
116117
} from '../../../../util/remoteFeatureFlag';
117118
import { unwrapRemoteFeatureFlag } from '../utils/flags';
118-
import { create } from '@metamask/superstruct';
119-
import { PredictFeeCollectionSchema } from '../schemas';
119+
import { parse, PredictFeeCollectionSchema } from '../schemas';
120120

121121
/**
122122
* State shape for PredictController
@@ -439,11 +439,12 @@ export class PredictController extends BaseController<
439439
? rawMarketHighlightsFlag
440440
: DEFAULT_MARKET_HIGHLIGHTS_FLAG;
441441

442-
const feeCollection = create(
442+
const feeCollection = parse(
443443
unwrapRemoteFeatureFlag<PredictFeatureFlags['feeCollection']>(
444444
flags.predictFeeCollection,
445445
),
446446
PredictFeeCollectionSchema,
447+
DEFAULT_FEE_COLLECTION_FLAG,
447448
);
448449

449450
const fakOrdersEnabled =

app/components/UI/Predict/schemas/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { define } from '@metamask/superstruct';
2+
import { Hex } from '@metamask/utils';
23

3-
export const HexSchema = define<`0x${string}`>('Hex', (value) => {
4+
export const HexSchema = define<Hex>('HexSchema', (value) => {
45
if (typeof value !== 'string') {
56
return false;
67
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
export { parse } from './utils';
2+
13
export { HexSchema } from './common';
24
export { PredictFeeCollectionSchema } from './flags';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { create, Struct, StructError } from '@metamask/superstruct';
2+
3+
export function parse<T>(
4+
value: unknown,
5+
schema: Struct<T, unknown>,
6+
defaultValue?: T,
7+
) {
8+
try {
9+
return create(value, schema);
10+
} catch (error) {
11+
if (defaultValue !== undefined) {
12+
return defaultValue;
13+
}
14+
if (error instanceof StructError || error instanceof Error) {
15+
throw new Error(`Invalid value: ${error.message}`);
16+
} else {
17+
throw new Error(`Invalid value: ${error}`);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)