You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to isolate failing nodes and replace them with custom error nodes?
I know error handling in Slate is non-existent - specifically without this, but there must be options...
What I've tried so far, looking at this, is a plugin that wraps every editor method with a try/catch, attempts to find a path in arguments, and inserts an error node at that path.
constfindPathInArgs=(args: unknown[]): Path|undefined=>{// some code deepsearching for any Path.isPath in args};consttryCatchCallback=(editor: PlateEditor,callback: TypeSafeFunction)=>(...args: unknown[])=>{try{returncallback(...args);}catch{constpath=findPathInArgs(args);if(path&&path.length>0){insertErrorNode(editor,path);focusEditor(editor,Path.next(path));}else{// does not workthrowerror;// WiP: crash the editor if we can't silent-fail (with deselect) or find the path}}};constwithErrorReporting=(editor: PlateEditor)=>{Object.entries(editor).forEach(([key,value])=>{if(typeofvalue==="function"){editor[key]=tryCatchCallback(editor,value);// update to only wrap transforms?}});returneditor;};exportconstErrorBoundaryPlugin=createPlatePlugin({key: KEY,node: {type: KEY,component: ErrorViewNode,isElement: true,isVoid: true,},extendEditor: ({ editor })=>{// we cannot definitively say which methods may cause errors so we wrap all of themreturnwithErrorReporting(editor);},});
I'm simulating the crash with editor.normalizeNode(path);, basically passing incorrect argument to normalizeNode.
It works in some cases, but it's very flimsy. To top it off, we actually want to try and render the original element on page refresh, instead of saving the error element... Could there be a better solution to this?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Is it possible to isolate failing nodes and replace them with custom error nodes?
I know error handling in Slate is non-existent - specifically without this, but there must be options...
What I've tried so far, looking at this, is a plugin that wraps every editor method with a try/catch, attempts to find a path in arguments, and inserts an error node at that path.
I'm simulating the crash with
editor.normalizeNode(path);
, basically passing incorrect argument to normalizeNode.It works in some cases, but it's very flimsy. To top it off, we actually want to try and render the original element on page refresh, instead of saving the error element... Could there be a better solution to this?
Beta Was this translation helpful? Give feedback.
All reactions