From b5fb5d0d253d3ad7e824b6e63b7196b603df1c16 Mon Sep 17 00:00:00 2001 From: Hugi Thordarson Date: Sun, 20 Oct 2024 07:10:14 +0000 Subject: [PATCH] Experimental helpful inline HTML-display of exceptions for simple issues --- .../templating/NGTemplateParserProxy.java | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/ng-appserver/src/main/java/ng/appserver/templating/NGTemplateParserProxy.java b/ng-appserver/src/main/java/ng/appserver/templating/NGTemplateParserProxy.java index 0ad58848..1bd981de 100644 --- a/ng-appserver/src/main/java/ng/appserver/templating/NGTemplateParserProxy.java +++ b/ng-appserver/src/main/java/ng/appserver/templating/NGTemplateParserProxy.java @@ -8,11 +8,16 @@ import java.util.Map.Entry; import java.util.Objects; +import ng.appserver.NGActionResults; import ng.appserver.NGApplication; +import ng.appserver.NGApplication.NGElementNotFoundException; import ng.appserver.NGAssociation; import ng.appserver.NGAssociationFactory; import ng.appserver.NGComponentReference; +import ng.appserver.NGContext; import ng.appserver.NGElement; +import ng.appserver.NGRequest; +import ng.appserver.NGResponse; import ng.appserver.elements.NGDynamicGroup; import ng.appserver.elements.NGHTMLBareString; import ng.appserver.elements.NGHTMLCommentString; @@ -40,19 +45,46 @@ public NGElement parse() throws NGDeclarationFormatException, NGHTMLFormatExcept return toDynamicElement( pNode ); } - private static NGElement toDynamicElement( PNode node ) throws NGDeclarationFormatException { - NGElement element = switch( node ) { + private static NGElement toDynamicElement( final PNode node ) throws NGDeclarationFormatException { + return switch( node ) { case PBasicNode n -> toDynamicElement( n.tag(), n.declaration() ); case PGroupNode n -> new NGDynamicGroup( null, null, template( n.tag() ) ); case PHTMLNode n -> new NGHTMLBareString( n.value() ); case PCommentNode n -> new NGHTMLCommentString( n.value() ); }; - - return element; } - private static NGElement toDynamicElement( NGDynamicHTMLTag tag, NGDeclaration declaration ) throws NGDeclarationFormatException { - return NGApplication.dynamicElementWithName( declaration.type(), toAssociations( declaration ), template( tag ), Collections.emptyList() ); + private static NGElement toDynamicElement( final NGDynamicHTMLTag tag, final NGDeclaration declaration ) throws NGDeclarationFormatException { + try { + return NGApplication.dynamicElementWithName( declaration.type(), toAssociations( declaration ), template( tag ), Collections.emptyList() ); + } + catch( NGElementNotFoundException e ) { + // FIXME: + // Very experimental functionality at the moment and definitely does not belong with the parser part of the framework. + // But since it's definitely something we want to add ad some point, I'm keeping it for reference + // Hugi 2024-10-19 + return new NGElement() { + @Override + public void appendToResponse( NGResponse response, NGContext context ) { + String s = """ + + Can't find an element/component '%s'. Would you like to create it? + + """.formatted( context.componentActionURL(), declaration.type() ); + response.appendContentString( s ); + }; + + @Override + public NGActionResults invokeAction( NGRequest request, NGContext context ) { + + if( context.currentElementIsSender() ) { + System.out.println( "Let's create that component!" ); + } + + return NGElement.super.invokeAction( request, context ); + } + }; + } } private static Map toAssociations( NGDeclaration declaration ) {