Jump to content




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


  • You cannot reply to this topic
3 replies to this topic

#1 Admicos

  • Members
  • 207 posts
  • LocationTurkey

Posted 11 September 2014 - 08: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


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 11 September 2014 - 08:40 AM

http://www.lua.org/pil/8.4.html

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 11 September 2014 - 12: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


#4 Admicos

  • Members
  • 207 posts
  • LocationTurkey

Posted 13 September 2014 - 01:16 PM

View Posttheoriginalbit, on 11 September 2014 - 12:44 PM, said:

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users