Need script error handling help

Some optimization and fix is needed (also work on v6 and v7 now…)

:global dyndnsForce
:global previousIP

:local username "<ME>"
:local clientkey "<CLIENTKEY>"
:local hostname "<MYDOMAIN>"

:local idName [/system identity get name]

:local result ([/tool fetch url="http://checkip.dyndns.org/dyndns.checkip.html" output=user as-value]->"data")

:local currentIP [:pick $result ([:find $result ": " -1] + 2) [:find $result "</body>" -1]]

# if undefined... define...
:if ($dyndnsForce != false) do={ :log warning "UpdateDynDNS: Forced update on" }

:if (($currentIP != $previousIP) or ($dyndnsForce != false)) do={
    :log info "UpdateDynDNS: previousIP = $previousIP ; currentIP = $currentIP"
    :set dyndnsForce false
    :set previousIP  $currentIP
    /file remove [find where name="dyndns.txt"]
    /tool fetch url="https://$username:$clientkey@members.dyndns.org/v3/update\3Fhostname=$hostname&myip=$currentIP" dst-path="dyndns.txt"
    :set result [/file get "dyndns.txt" contents]
    /tool fetch upload=yes mode=ftp ascii=no address=192.168.2.22 port=21 user=mikrotik password=mikrotik \
        src-path="dyndns.txt" dst-path="/mikrotik-backups/$idName\2DIP.txt"
    /file remove [find where name="dyndns.txt"]
    :log info "UpdateDynDNS: Dyndns Update Result: $result"
} else={ :log info "UpdateDynDNS: No dyndns update needed" }