From 4eb84b0ca4bbbee4df55514018d5199b571bfcb7 Mon Sep 17 00:00:00 2001 From: Matthew Thomas Date: Fri, 24 Mar 2023 16:41:44 +0000 Subject: [PATCH] fix(encoders): add error handling to en/decodeURI --- .../toolbox/src/components/Encoders/UrlEncode.tsx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/plugins/toolbox/src/components/Encoders/UrlEncode.tsx b/plugins/toolbox/src/components/Encoders/UrlEncode.tsx index 5034096..dd848ba 100644 --- a/plugins/toolbox/src/components/Encoders/UrlEncode.tsx +++ b/plugins/toolbox/src/components/Encoders/UrlEncode.tsx @@ -7,11 +7,16 @@ export const UrlEncode = () => { const [mode, setMode] = React.useState('Encode'); useEffect(() => { - if (mode === 'Encode') { - setOutput(encodeURI(input)); - } else { - setOutput(decodeURI(input)); + let url = ''; + let errorMessage = ''; + try { + url = mode === 'Encode' ? encodeURI(input) : decodeURI(input); + } catch (error) { + errorMessage = `couldn't ${ + mode === 'Encode' ? 'encode' : 'decode' + } URL...`; } + setOutput(url || errorMessage); }, [input, mode]); return (