Search code examples
stringfileopa

opa : binary_to_string / String.contains error


My opa application fails when I use the String.contains method on a string read from a file :

import stdlib.io.file
import stdlib.core


function start()
{
        txt = string_of_binary(File.content("data.txt"))
        jlog(txt)
        b = String.contains(txt, "Rabbit")

        <>Hello Bug</>
}

Server.start(
   {port:8092, netmask:0.0.0.0, encryption: {no_encryption}, name:"bug"},
   [ {resources: @static_resource_directory("resources")},
     {register: {css: []} },
     {page: start, title: "bug"},
   ]
)

I've got the following error :

bug serving on http://ks3098156.kimsufi.com:8092
[Opa] Server dispatch Decoded URL to /
STDERR:rabbit


/home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:26
_error = true;global.console.log("Uncaught exception : " + global.e.toString()
                                                                    ^
TypeError: Cannot call method 'toString' of undefined
    at /home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:26:222
    at /home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:27:78
    at /home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:22:128
    at /home/kayhman/website/rstEditor/code/bug_depends/qmlCpsClientLib.js:28:263
    at dispatcher_cps (/home/kayhman/website/rstEditor/code/bug_depends/bslNet.nodejs:60:165)
    at Server.<anonymous> (/home/kayhman/website/rstEditor/code/bug_depends/bslNet.nodejs:59:437)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1514:12)
    at HTTPParser.onHeadersComplete (http.js:102:31)
    at Socket.ondata (http.js:1410:22)

The data file contains only one line :

rabbit 

What's wrong with my code ?

Thanks


Solution

  • Indeed there is a bug in File.content, it will be fixed in the next release. You can use the preferable (and working) safe File.content_opt function:

    function start() {
            match(File.content_opt("data.txt")){
            case {none} :<>No file</>
            case {some:content} :
                txt = string_of_binary(content)
                jlog(txt)
                b = String.contains(txt, "Rabbit")
                <>Hello Bug</>
            }
    }