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

How can i write something if http.get gives a error?

Started by Admicos, 11 September 2014 - 07:19 AM
Admicos #1
Posted 11 September 2014 - 09:19 AM
How can i write something if http.get gives a error?

My current Code is:

--ADMI-GET
--Made by admicos
--http://mchub.minecrafttr.com/

--kodlar asagidadir
local arg = { ... }
if arg[1] then
  package = arg[1]
  yn = "n"
  print("Paket aliniyor...")
  local pInfo = http.get("http://mchub.minecrafttr.com/files/Admicos/admi-get/"..package..".ainf")
  print(pInfo.readAll())
  pInfo.close()
  print("Bu program indirmek istiyormusunuz? [E/H]")
  yn = read()
  if yn == "e" or yn == "E" then
	local packageURL = http.get("http://mchub.minecrafttr.com/files/Admicos/admi-get/"..package..".apkg")
	packageToWrite = packageURL.readAll()
	local file = fs.open(package, "w")
	file.write(packageToWrite)
	file.close()
	packageURL.close()
	term.clear()
	term.setCursorPos(1,1)
	print("Paket Alindi! Admi-Get'i kullandiginiz icin tesekkur ederim!")
  end
else
  error("Kullanm: admi-get <program>", 0)
end
Bomb Bloke #2
Posted 11 September 2014 - 09:40 AM
http://www.lua.org/pil/8.4.html
theoriginalbit #3
Posted 11 September 2014 - 01:44 PM
To further extend upon what Bomb Bloke should have said. Take a look into pcall, this function is able to capture any errors and stop your program from halting. It simply returns the success or failure of the function call as the first return value. On failure it will return the reason for the error as the second return value. On success it will return whatever the function would return as the second (and higher) return value. For example

local ok, handle = pcall(http.get, "https://www.google.com")
if ok then
  print("Success!")
  --# handle contains the table you may read from
else
  print("Failed to contact Google due to: ", handle)
end
Admicos #4
Posted 13 September 2014 - 02:16 PM
To further extend upon what Bomb Bloke should have said. Take a look into pcall, this function is able to capture any errors and stop your program from halting. It simply returns the success or failure of the function call as the first return value. On failure it will return the reason for the error as the second return value. On success it will return whatever the function would return as the second (and higher) return value. For example

local ok, handle = pcall(http.get, "https://www.google.com")
if ok then
  print("Success!")
  --# handle contains the table you may read from
else
  print("Failed to contact Google due to: ", handle)
end

Thanks!