From 497319ec8707ab5b0b454c75e544a5651a34d776 Mon Sep 17 00:00:00 2001 From: michael Date: Sat, 16 May 2020 11:58:19 +0000 Subject: [PATCH] * Demo for exception hooks in sysutils --- demo/errorhandler/errordemo.lpi | 90 +++++++++++++++++++++++++++++++++ demo/errorhandler/errordemo.lpr | 66 ++++++++++++++++++++++++ demo/errorhandler/index.html | 20 ++++++++ 3 files changed, 176 insertions(+) create mode 100644 demo/errorhandler/errordemo.lpi create mode 100644 demo/errorhandler/errordemo.lpr create mode 100644 demo/errorhandler/index.html diff --git a/demo/errorhandler/errordemo.lpi b/demo/errorhandler/errordemo.lpi new file mode 100644 index 0000000..3936197 --- /dev/null +++ b/demo/errorhandler/errordemo.lpi @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <CustomData Count="4"> + <Item0 Name="MaintainHTML" Value="1"/> + <Item1 Name="PasJSHTMLFile" Value="project1.html"/> + <Item2 Name="PasJSPort" Value="0"/> + <Item3 Name="PasJSWebBrowserProject" Value="1"/> + </CustomData> + <BuildModes> + <Item Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + <UseFileFilters Value="True"/> + </PublishOptions> + <RunParams> + <FormatVersion Value="2"/> + </RunParams> + <Units> + <Unit> + <Filename Value="errordemo.lpr"/> + <IsPartOfProject Value="True"/> + </Unit> + <Unit> + <Filename Value="index.html"/> + <IsPartOfProject Value="True"/> + <CustomData Count="1"> + <Item0 Name="PasJSIsProjectHTMLFile" Value="1"/> + </CustomData> + </Unit> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <Target FileExt=".js"> + <Filename Value="errordemo"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <UnitOutputDirectory Value="js"/> + </SearchPaths> + <Parsing> + <SyntaxOptions> + <AllowLabel Value="False"/> + <CPPInline Value="False"/> + <UseAnsiStrings Value="False"/> + </SyntaxOptions> + </Parsing> + <CodeGeneration> + <TargetOS Value="browser"/> + </CodeGeneration> + <Linking> + <Debugging> + <GenerateDebugInfo Value="False"/> + <UseLineInfoUnit Value="False"/> + </Debugging> + </Linking> + <Other> + <CustomOptions Value="-Jeutf-8 -Jirtl.js -Jc -Jminclude"/> + <CompilerPath Value="$(pas2js)"/> + </Other> + </CompilerOptions> + <Debugging> + <Exceptions> + <Item> + <Name Value="EAbort"/> + </Item> + <Item> + <Name Value="ECodetoolError"/> + </Item> + <Item> + <Name Value="EFOpenError"/> + </Item> + </Exceptions> + </Debugging> +</CONFIG> diff --git a/demo/errorhandler/errordemo.lpr b/demo/errorhandler/errordemo.lpr new file mode 100644 index 0000000..8d44866 --- /dev/null +++ b/demo/errorhandler/errordemo.lpr @@ -0,0 +1,66 @@ +program errordemo; + +{$mode objfpc} + +uses + BrowserConsole, JS, Classes, SysUtils, Web; + +function DoRaise(aEvent : TJSMouseEvent) : boolean; + +begin + Result:=False; + raise exception.Create('A exception'); +end; + +function DoHook(aEvent : TJSMouseEvent) : boolean; + +begin + Result:=False; + HookUncaughtExceptions; +end; + +Procedure DoPascalException(O : TObject); + +begin + Writeln('O :',O.ClassName); + if O is Exception then + Writeln('Exception class message : ',Exception(O).Message); +end; + +Procedure DoJSException(O : TJSObject); +begin + writeln('Javascript exception: ',O.toString); + if O is TJSError then + Writeln('Error message : ',TJSError(O).Message); +end; + +Procedure DoRaiseJS; assembler; +asm + throw new Object(); +end; + +Procedure DoRaiseJSError; assembler; +asm + var e = new Error(); + e.message="My error message"; + throw e; +end; + +begin + // This will only work for the main program if you have set showUncaughtExceptions before rtl.run(); + TJSHtmlButtonElement(Document.getElementById('btnhook')).OnClick:=@DoHook; + // These will not be caught (yet) + TJSHtmlButtonElement(Document.getElementById('btn')).OnClick:=@DoRaise; + // Uncomment this to set default exception handlers + // HookUncaughtExceptions; + + // Uncomment these to set special exception handlers + // SetOnUnCaughtExceptionHandler(@DoPascalException); + // SetOnUnCaughtExceptionHandler(@DoJSException); + + // Various ways to raise an exception. + // DoRaiseJS; + // DoRaiseJSError; + + DoRaise(Nil); +end. diff --git a/demo/errorhandler/index.html b/demo/errorhandler/index.html new file mode 100644 index 0000000..f824e4c --- /dev/null +++ b/demo/errorhandler/index.html @@ -0,0 +1,20 @@ +<!doctype html> +<html lang="en"> +<head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>TestException + + + + + + + + +