Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

How to handle errors in lua?


tiCeR

Question

Hey there,

 

i would love to know how to handle errors in lua.

 

I searched the web and found the pcall() function but it looks like the function does not work?

 

My problem is:

 

I catch a json table and try to set a value from it to a local variable.

 

The problem is that the value is not always present in the table and the ->

 

local valuefromtable = table.result[1].playerid

 

fails and ends in ERROR -> SelfIId ....

 

So what i need is a function like this....

Please login or register to see this code.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Comeon

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" /> no one? I need that for many code positions where i need to check if erros come up

    Please login or register to see this image.

    /emoticons/default_sad.png" alt=":(" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    Link to comment
    Share on other sites

    • 0

    pcall works but I wouldn't use it here, pcall is for exceptions and this is not an exception. Use pcal with http access.

    I have a long version and a short version to solve this. The long version better explains everything, but the short is what you need. This is very "idiomatic lua".

     

    Please login or register to see this code.

    Does this solve your problem?
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • pcall works but I wouldn't use it here, pcall is for exceptions and this is not an exception. Use pcal with http access.

    I have a long version and a short version to solve this. The long version better explains everything, but the short is what you need. This is very "idiomatic lua".

     

    Please login or register to see this code.

    Does this solve your problem?

     

     

    Looks like i could use it

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" /> the top function! I will try it later and let you know. Thank you!

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • okay seems to work i get a debug with 2 now but the ifs after that does not detect the number for some reason? I already tired to make a tonumber(playerid) and also tried a tostring(playerid) but no result. It looks like the value does not get out of the if so i can work after that with it.

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Okay just added the if into the other if where i set the playerid and it works. I dont know whats the difference to the requestcode on top because there it also works the other way.

    Link to comment
    Share on other sites

    • 0

    This code has a subtle bug, regarding locality of variables. Here's the original commented version and the corrected one.

    Please login or register to see this code.

    It's possible that "playerid" is returned as a string. If that's the case either of these should work:

     

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • oh okay so the local was the problem

    Please login or register to see this image.

    /emoticons/default_sad.png" alt=":(" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" /> damm still learning but seems to work well now

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...