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: 
JesUltra
Streaming Star

roByteArray.ReadFile() occasional error

My channel obtains configuration by downloading a json file from my server on startup, as a fallback, I have included a version of the json file in the channel itself as res/config.json in the zip file.  Below is the code that my channel runs in case the HTTP request fails:

  byteArray = CreateObject("roByteArray")
  if byteArray.ReadFile("pkg:/res/config.json") then
    ' Apply configuration
  else
    logError("Unable to load config.json")
  end if

My issue is, that I occasionally, less than 1 out of 100 times, see the error "Unable to load config".  (By "see", I mean that the message is sent to my server by logError(), from some user's device.)

I have not been able to find any documentation of ReadFile(), that suggest it is possible to get a more detailed error message, besides the return value.

Does anyone have a good idea, what could be the cause for ReadFile() occasionally failing to read a local file from pkg: ?  (I can of course rule out the file not existing, since this works most of the time, with the exact same channel package.)

0 Kudos
6 REPLIES 6
renojim
Community Streaming Expert

Re: roByteArray.ReadFile() occasional error

I don't have an answer to your question, but I do have two questions of my own:

Is there another "logError" anywhere else that it could be hitting?

If it's JSON, why are you using roByteArray?  Why not just use ReadAsciiFile?

Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
JesUltra
Streaming Star

Re: roByteArray.ReadFile() occasional error

There are other calls to logError() but not with the same message, so I know that this particular line is being hit.

I was not aware of the ReadAsciiFile() function until now, it is clearly a better choice, thanks for pointing that out.  I have tried using that instead in my channel, to see if it makes a difference.

0 Kudos
JesUltra
Streaming Star

Re: roByteArray.ReadFile() occasional error

I have pushed the code below to production, but today got the first instance of "Unable to load config.json".  So it seems that byteArray.ReadFile() and ReadAsciiFile() both fail, in some case, possibly due to some error accessing the underlying storage.

I am wondering if anyone has a suggestion of what circumstances the Roku system may succeed in loading the xml and brs files of my channel, in order to begin executing it, but then immediately after startup fail to load a resource file from the same channel package?

jsonString = ReadAsciiFile("pkg:/res/config.json")
if jsonString <> "" then
  applyAppConfig(ParseJson(jsonString), ...)
else if byteArray.ReadFile("pkg:/res/config.json") then
  logWarning("Unable to load config.json through ReadAsciiFile")
  applyAppConfig(ParseJson(byteArray.ToAsciiString()), ...)
else
  logError("Unable to load config.json")
end if
0 Kudos
DavidS1
Channel Surfer

Re: roByteArray.ReadFile() occasional error

How big is your json file?

I am using pretty much the same code without any issues. The only difference I can see from what is posted is my json files are located in the source folder.

The json file I am using is pretty small, around 900 characters long

0 Kudos
JesUltra
Streaming Star

Re: roByteArray.ReadFile() occasional error

My config.json file is about 6 kilobytes.

I have now resorted to modifying the Makefile to generate a brs source file with the entire content of the configuration file embedded as a literal string, then I do not need to invoke any loading methods to get the content.

Content of my new config.brs:

function defaultConfig() as String
  return "{""rowTemplates"": [ ..... ] }"
end function

Adding a new <script/> node referring to this new brs file next to my "main" brs file, allow me to call the defaultConfig() method anywhere from within my main brs file.

0 Kudos
DavidS1
Channel Surfer

Re: roByteArray.ReadFile() occasional error

You could try to use a recursive function to get the config, it may not be the prettiest function but might work : 

 

sub GetConfig()
    fileContent = TryToReadFile("pkg:/res/config.json", 5)
    ' do whatever with the retrieved content
end sub

function TryToReadFile(file, numberOfTries) as string

    if (numberOfTries < 0)
        return invalid
    end if 

    jsonString = ReadAsciiFile(file)
    if jsonString <> "" then
        return jsonString
    else
         remainingTries = numberOfTries - 1
         return TryToReadFile(file, remainingTries)
end function

 

 

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.