This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
gamingdude295's profile picture

Stop code crashing if a website I'm reading is down

Started by gamingdude295, 22 July 2016 - 01:41 PM
gamingdude295 #1
Posted 22 July 2016 - 03:41 PM
Hello.
Me and a couple friends have been coding an entire system for our base, and we're trying to have a login system. If an incorrect password is put in, it goes to TimeAPI, fetches the time, and puts it into a log saying when it was, and what the ID of the system was.

Unfortunately, timeAPI sometimes goes down, and when it is, the system crashes, so then we have to restart the program.

Here is a (simplified) function we use for getting the real time:


function getRealTime()
    local realTimeURL = "http://www.timeapi.org/utc/now?format=%25a%20%25b%20%25d%20%25I:%25M:%25S%20%25Z%20%25Y"
    local realTime = http.get(realTimeURL).readAll() --Crashes at this line when it's down
    return realTime
end

The log writing file is later on, but this is the time getting function.

Is there someway we can write this so that if timeAPI is down, it doesn't crash at line 3, and maybe write a line to say 'TimeAPI was down'.

Cheers in advance.
KingofGamesYami #2
Posted 22 July 2016 - 05:54 PM
Instead of immediately doing a .readAll on the web handle, check if it's non-nil.
gamingdude295 #3
Posted 24 July 2016 - 12:41 AM
Sorry for delayed response.

How would I go about doing this specifically?
Would this work, or would it be wrong?


local timeTest = http.get(realTimeURL)
if not timeTest == nil
    realTime = timeTest.readAll()
else
    realTime = "TimeAPI was down" 
end


I've used Java, and I believe attempting to compare to nil made it have an error, so I'm guessing this'll do the same.
Cheers for the help as well.
CoderPuppy #4
Posted 24 July 2016 - 05:00 AM
snip

There are a couple of minor problems with that code, all on this line:

if not timeTest == nil

Firstly you need a "then" on the end. Secondly that condition will be parsed as:

(not timeTest) == nil
which is probably not what you want, rather you should use Lua's not equal operator: "~="

So after fixing those it should look more like this:

local timeTest = http.get(realTimeURL)
if timeTest ~= nil then
    realTime = timeTest.readAll()
else
    realTime = "TimeAPI was down" 
end

Now personally I would reduce that to:

local timeTest = http.get(realTimeURL)
--# only nil and false are falsy, so this is the same as comparing with nil unless you want a distinction between nil and false
if timeTest then
    realTime = timeTest.readAll()
else
    realTime = "TimeAPI was down" 
end
then to:

local timeTest = http.get(realTimeURL)
--# this is a Lua ternary operator
--# it works unless `timeTest.readAll()` returns a falsy value
realTime = timeTest and timeTest.readAll() or "TimeAPI was down"

Each of those changes the behaviour slightly, first one changes the behaviour when `timeTest` is false and the second when `timeTest.readAll()` is falsy.
gamingdude295 #5
Posted 25 July 2016 - 09:26 AM
Ah, forgot the then, was trying to write it on the fly.
Cheers for that, I'll probably use the first shortened down one as I can read it and already have an easy idea how it works. The second one catches me off a bit. I'll get this on the code for my friend and he can see it too.
Thanks!