From a2d5343a87c05ac5640abba5c50712e2a10a7958 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Tue, 4 Jun 2024 14:51:28 -0500 Subject: [PATCH] Report persisted schema validation failures (#4358) * Report persisted schema validation failures * Make it safer --- src/state/persisted/store.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/state/persisted/store.ts b/src/state/persisted/store.ts index bb7fbed8..421bdccd 100644 --- a/src/state/persisted/store.ts +++ b/src/state/persisted/store.ts @@ -1,7 +1,7 @@ import AsyncStorage from '@react-native-async-storage/async-storage' -import {Schema, schema} from '#/state/persisted/schema' import {logger} from '#/logger' +import {Schema, schema} from '#/state/persisted/schema' const BSKY_STORAGE = 'BSKY_STORAGE' @@ -13,8 +13,19 @@ export async function write(value: Schema) { export async function read(): Promise { const rawData = await AsyncStorage.getItem(BSKY_STORAGE) const objData = rawData ? JSON.parse(rawData) : undefined - if (schema.safeParse(objData).success) { + const parsed = schema.safeParse(objData) + if (parsed.success) { return objData + } else { + const errors = + parsed.error?.errors?.map(e => ({ + code: e.code, + // @ts-ignore exists on some types + expected: e?.expected, + path: e.path, + })) || [] + logger.error(`persisted store: data failed validation on read`, {errors}) + return undefined } }