From f9f1d6026700566b3aadb035f1762ed92ef7707f Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Thu, 21 Nov 2019 06:28:24 -0800 Subject: [PATCH] Fix some DEV errors showing up as `DEV: [{}]` Summary: Collections of errors were not reported correctly in the UI. Also, reporting errors could crash flipper if the error is not serializable (for example due to being cyclic). Reviewed By: jknoxville Differential Revision: D18595257 fbshipit-source-id: 689b6dbfe5d7daa6d84bd703dba9daa6be2e6b82 --- src/utils/errors.tsx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/utils/errors.tsx b/src/utils/errors.tsx index 59240082c..de1cfd29a 100644 --- a/src/utils/errors.tsx +++ b/src/utils/errors.tsx @@ -13,12 +13,21 @@ export class CancelledPromiseError extends Error { this.name = 'CancelledPromiseError'; } } -export function getStringFromErrorLike(e: any) { - if (typeof e == 'string') { + +export function getStringFromErrorLike(e: any): string { + if (Array.isArray(e)) { + return e.map(getStringFromErrorLike).join(' '); + } else if (typeof e == 'string') { return e; } else if (e instanceof Error) { - return e.message; + return e.message || e.toString(); } else { - return JSON.stringify(e); + try { + return JSON.stringify(e); + } catch (e) { + // Stringify might fail on arbitrary structures + // Last resort: toString it. + return '' + e; + } } }