@samitouri / QOS-React-2 / commits / 5b8e94c6d8

[ez][wip] Make playground more resilient to crashes

--- <img width="1031" alt="image" src="https://github.com/facebook/react-forget/assets/34200447/8e475472-45c3-4ef0-aa4b-d187e72b999c"> Behold! No instacrash on `useMemo()`

Mofei Zhang committed Nov 21, 2023 at 09:30 UTC 5b8e94c6d84342a906612d95315f239f9caea8c1
2 files changed +21 -11
compiler/apps/playground/components/Editor/EditorImpl.tsx
+12 -2
@@ -229,11 +229,21 @@ function compile(source: string): CompilerOutput {
229 }
230 }
231 }
232 - } catch (err: any) {
232 + } catch (err) {
233 // error might be an invariant violation or other runtime error
234 // (i.e. object shape that is not CompilerError)
235 - if (err.details !== null) {
235 + if (err instanceof CompilerError && err.details.length > 0) {
236 error.details.push(...err.details);
237 + } else {
238 + // Handle unexpected failures by logging (to get a stack trace)
239 + // and reporting
240 + console.error(err);
241 + error.details.push(new CompilerErrorDetail({
242 + severity: ErrorSeverity.Invariant,
243 + reason: `Unexpected failure when transforming input! ${err}`,
244 + loc: null,
245 + suggestions: null
246 + }));
247 }
248 }
249 if (error.hasErrors()) {
compiler/apps/playground/lib/stores/store.ts
+9 -9
@@ -33,13 +33,13 @@ export function saveStore(store: Store) {
33 * Check if @param raw is a valid Store by if
34 * - it has a `source` property and is a string
35 */
36 -function getValidStore(raw: any): Store | null {
37 - const isValidStore = "source" in raw && typeof raw["source"] === "string";
38 - if (isValidStore) {
39 - return raw;
40 - } else {
41 - return null;
42 - }
36 +function isValidStore(raw: unknown): raw is Store {
37 + return (
38 + raw != null &&
39 + typeof raw == "object" &&
40 + "source" in raw &&
41 + typeof raw["source"] === "string"
42 + );
43 }
44
45 /**
@@ -56,7 +56,7 @@ export function initStoreFromUrlOrLocalStorage(): Store {
56 if (!encodedSource) return defaultStore;
57
58 const raw = JSON.parse(codec.atou(encodedSource));
59 - const store = getValidStore(raw);
60 - invariant(store != null, "Invalid Store");
59 +
60 + invariant(isValidStore(raw), "Invalid Store");
61 return raw;
62 }