Pokud ve Claris FileMakeru při spuštění skriptu dojde k nějaké chybě, lze zjistit chybu pomocí funkce Get(LastError)
. Při vlastním ošetření chyb ale bohužel nemůžeme jednoduše vypsat textový popis chyby. FileMaker vrací číselné kódy FileMaker Pro error codes.
Lze však napsat vlastní funkci nazvanou např. getErrorDescription(errorNumber)
, která vrátí textový popis chyby. Pokud by kód nebyl nalezen, tak vracím číslo chyby.
Vlastní funkce getErrorDescription(errorNumber)
Kód funkce by byl např. tento:
// http://www.filemaker.com/help/14/fmp/en/html/error_codes.html#1030072 Case( errorNumber = -1; "Unknown error"; errorNumber = 0; "No error"; errorNumber = 1; "User canceled action"; errorNumber = 2; "Memory error"; errorNumber = 3; "Command is unavailable (for example, wrong operating system or mode)"; errorNumber = 4; "Command is unknown"; // ... // další kódy... // ... errorNumber = 1632; "Certificate cannot be authenticated by a supported certificate authority"; errorNumber = 1633; "Certificate is valid but still causes an error (for example, the certificate has expired)"; errorNumber)
Zde je přípaně obdobně napsaná funkce pro starší verzi Filemakeru 12.
Použití vlastní funkce getErrorDescription(errorNumber)
Funkci getErrorDescription(errorNumber)
lze pak použít ve Script Workspace editoru např. následujícím způsobem:
Set Error Capture [On] # doplníme nějaký kód, který chceme ošetřit např. Save Records As Excel [Restore; No dialog; "soubor.xlsx"; Records being browsed] Set Variable [$lastError; Value: Get(LastError)] If [$lastError] Set Variable [$errorDescription; Value: getErrorDescription ($lastError)] Show Custom Dialog ["Error"; "Error: " & $errorDescription & "."] Else # nějaký náš kód, který se vykoná v případě úspěchu End If Set Error Capture [Off]
Nebo ještě jednodušeji:
Set Error Capture [On] # doplníme nějaký kód, který chceme ošetřit např. Save Records As Excel [Restore; No dialog; "soubor.xlsx"; Records being browsed] Set Variable [$lastError; Value: Get(LastError)] If [$lastError] Show Custom Dialog ["Error"; "Error: " & getErrorDescription ($lastError) & "."] Else # nějaký náš kód, který se vykoná v případě úspěchu End If Set Error Capture [Off]
Pro otestování stačí mít otevřený daný soubor a zkusit znova uložit jeho záznamy. Vyvolá se pak chybová hláška. Soubor se ukládá do aktuálního adresáře.