Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
destruk
Streaming Star

BreakIfRunError

I was looking for a way to include user-defined error handling in a script for Roku. I see this -

Sub Main()
Run("pkg:/test.brs")
BreakIfRunError(LINE_NUM)
Print Run("test2.brs", "arg 1", "arg 2")
if Run(["pkg:/file1.brs","pkg:/file2.brs"])<>4 then stop
BreakIfRunError(LINE_NUM)
stop
End Sub

Sub BreakIfRunError(ln)
el=GetLastRunCompileError()
if el=invalid then
el=GetLastRunRuntimeError()
if el=&hFC or el=&hE2 then return
'FC==ERR_NORMAL_END, E2=ERR_VALUE_RETURN
print "Runtime Error (line ";ln;"): ";el
stop
else
print "compile error (line ";ln;")"
for each e in el
for each i in e
print i;": ";e[i]
end for
end for
stop
end if
End Sub


This doesn't work if your script you run tries to make use of an invalid component method - such as screen.getmyvalue()
It crashes back to the debugger without proceeding with execution to print the runtime error.
Is there a way to check and handle invalid methods for components?
0 Kudos
9 REPLIES 9
YungBlood
Streaming Star

Re: BreakIfRunError

Can you post the test script that is crashing? I use that kind of code to keep Games 4 Roku from crashing when I'm testing games, and it works fine for me.
YungBlood

Bringing more fun to Roku!
0 Kudos
destruk
Streaming Star

Re: BreakIfRunError

I have it load "pkg:/source/test.brs"

test.brs has

Function Main()
Return CreateObject("roDeviceInfo").GetCountryCode()
End Function

It crashes with member function not found in brightscript interface rather than returning the runtime error code to the original app.
I know it's not valid - I just want it to properly handle the error and set variables based on failure.

And yes, removing the stop in the calling routine and adding code to it doesn't execute. So it doesn't appear to handle this properly. 😞
0 Kudos
destruk
Streaming Star

Re: BreakIfRunError

changing the loaded script to alternates doesn't produce different results either - like
Function Main()
m.country=CreateObject("roDeviceInfo").GetCountryCode()
End Function

Basically I thought the whole load a script and return/print errors would work like "on error resume next" for vbscript, or an exception handler try block in C++, but no matter what, in brightscript, a crash crashes in this instance.
Any other solutions?

I suppose I could get around it by not using the command that fails, but sending a request to the server and have the server determine which country the request is coming from based on the external request IP, but that is more work than having the app try to determine it, and then set to USA if it fails.
http://www.go4expert.com/forums/showthread.php?t=3511
0 Kudos
TheEndless
Channel Surfer

Re: BreakIfRunError

Have you tried an Eval() on the line of code you want to execute instead of trying to run a full script?

Side note, is GetCountryCode() supposed to work? I'm not sure I understand what you're trying accomplish, unless that's a method that's not documented somewhere...?
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
destruk
Streaming Star

Re: BreakIfRunError

Eval fails too as it doesn't exist in the current firmware.
I'm trying to find a way to not have to rely on the version number reported by the device before trying to execute new code elements provided in future firmware.
The method is supposed to report the region the roku device is in, so you can contact the closest CDN to the user. - so it would return "US" or "UK" or other or something for the country code.
0 Kudos
TheEndless
Channel Surfer

Re: BreakIfRunError

"destruk" wrote:
Eval fails too as it doesn't exist in the current firmware.
I'm trying to find a way to not have to rely on the version number reported by the device before trying to execute new code elements provided in future firmware.
The method is supposed to report the region the roku device is in, so you can contact the closest CDN to the user. - so it would return "US" or "UK" or other or something for the country code.

The following works for me...

' Initialize the country variable
country = invalid

' Create a device info object
deviceInfo = CreateObject("roDeviceInfo")

' Attempt to use GetCountryCode() to set country
' Eval returns the error code, so the print should print it to the console (252 = success)
print Eval("country = deviceInfo.GetCountryCode()")

' If the eval failed, set the country to US
If country = invalid Then
country = "US"
End If
print country

Where did you read about GetCountryCode, if I may ask? I have a need for that, but don't see it documented anywhere...
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
destruk
Streaming Star

Re: BreakIfRunError

Thanks for that TheEndless - we're trying to port a channel to the european channel store and RokuPatrick told us to use GetCountryCode to host the same app in all channel stores.
The method still fails (error code 244) - but with your code it proceeds as desired so I'll say it's resolved until I can test it again with a working method in a future firmware revision.
My eval wasn't working as I didn't use quotes.



Function GetCountryCode()
result=invalid

' Create a device info object
deviceInfo=CreateObject("roDeviceInfo")

' Attempt to use GetCountryCode() to set country
' Eval returns the error code, so the print should print it to the console (252 = success)
result=Eval("country=deviceInfo.GetCountryCode()")

if result<>252
Return "US"
else
Return deviceInfo.GetCountryCode()
end if

'returns country code
'US
'CA (Canada)
'GB (UK)
'IE (Ireland)
'OT (Other)
End Function
0 Kudos
TheEndless
Channel Surfer

Re: BreakIfRunError

Nice. Thanks for the extra info.

As for your code, you may already realize it, but you're duplicating a call. The Eval runs in the context of the current script, so it's setting the value of "country" (assuming a successful result) which is accessible to the rest of your code, so you're second call to GetCountryCode isn't really necessary.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
destruk
Streaming Star

Re: BreakIfRunError

Thankyou for the help.


Function GetCountryCode()
result=invalid
country=invalid

' Create a device info object
deviceInfo=CreateObject("roDeviceInfo")

' Attempt to use GetCountryCode() to set country
' Eval returns the error code, so the store the return code in result (252 = success)
result=Eval("country=deviceInfo.GetCountryCode()")

if result<>252
Return "US"
else
Return country
end if

'returns country code
'US
'CA (Canada)
'GB (UK)
'IE (Ireland)
'OT (Other)
End Function


That should be better.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.