///////////////////////////////////////////////////////////////////////////// public static string HandleError( RazorRunnerException ex ) { // ****** string preMsg = string.Empty; switch( ex.RunContext ) { case RunHow.Run: preMsg = "While running a razor instance:"; break; case RunHow.RunOnly: preMsg = "While running razor on only the input text (no Nmp):"; break; case RunHow.RunBefore: preMsg = "While running razor on the input text before invoking Nmp:"; break; case RunHow.RunAfter: preMsg = "While running razor on the output produced by Nmp:"; break; } /// stack dump // ****** //ThreadContext.WriteMessage( preMsg ); //ThreadContext.WriteMessage( ex.Message + " Session output is the Razor source\n" ); ThreadContext.WriteMessage( "{0}\n{1} Session output is the Razor source", preMsg, ex.Message ); //ThreadContext.WriteMessage( ex.RazorError ); string [] errs = ex.RazorError.Split( new string [] { "\r\n" }, StringSplitOptions.None ); foreach( string err in errs ) { string text = err.Trim(); if( !string.IsNullOrEmpty( text ) ) { ThreadContext.WriteMessage( text ); } } // ****** return AddLineNumbers( ex.Code ); }
///////////////////////////////////////////////////////////////////////////// private string Runner( string text, bool returnSource, RazorRunnerException.RunHow runContext ) { // ****** //dynamic context = new ExpandoObject(); //context.Version = 2.0; // ****** var asmId = string.Empty; var result = string.Empty; var reader = new StringReader( text ); // ****** // as arguments to ctor pass the PrePostHandler() and a method to customize // the host and whatever else // // for the host we set the names for section, etc var razor = new RazorEngine<NmpRazorTemplateBase>( DefaultNamespaces, PrePostRenderHandler, CreateHostHandler ); razor.SetError(); try { asmId = razor.ParseAndCompileTemplate( assemblyPaths, reader ); } catch ( Exception ex ) { ThreadContext.MacroError( "error Parsing and Compiling Razor template: {0}", ex.Message ); } if( null == asmId ) { // // error // const string ErrMsg = "Razor failed to compile the generated source."; throw new RazorRunnerException( runContext, ErrMsg, razor.ErrorMessage, razor.LastGeneratedCode ); } // ****** try { result = razor.RenderTemplateFromAssembly( asmId, null ); } catch ( Exception ex ) { ThreadContext.MacroError( "error Rendering Razor Template from Assembly template: {0}", ex.Message ); } if( null == result ) { const string ErrMsg = "Razor failed to execute the template."; throw new RazorRunnerException( runContext, ErrMsg, razor.ErrorMessage, razor.LastGeneratedCode ); } // ****** if( RazorRunnerException.RunHow.Run == runContext ) { // // (#block `razor' // } // ****** return FileReader.FixText( returnSource ? RazorRunnerException.AddLineNumbers(razor.LastGeneratedCode) : result ); }