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: 
nmaves
Visitor

Handling / Throwing errors

I am trying to figure out the best way to have reusable code with different error conditions.  Below is a sample of a reusable function to execute network requests.  In most languages you can throw/catch errors.  There is nothing like that in BS.  So what do you do when you get a non 2xx message back from an api and you want to use their message in the payload?  How does the calling function know that the was an error?  I really don't want to have the boilerplate code below everywhere in my app.

function request(url As String, body as string, httpType as string) as Object
    ' ? "POST URL :"; url
    ' ? "POST BODY :"; body
    obj = CreateObject("roUrlTransfer")
    obj.setUrl(url)
    obj.setRequest(httpType)
    obj.setCertificatesFile("common:/certs/ca-bundle.crt")
    obj.addHeader("ws-api", m.global.apiVersion)
    obj.addHeader("app", m.global.app)
    obj.addHeader("user-agent", m.global.userAgent)
    obj.setPort(CreateObject("roMessagePort"))
    obj.addHeader("Content-Type", "application/x-www-form-urlencoded")
    obj.enableEncodings(true)

    if (m.global.authToken <> invalid and m.global.authToken <> "")
        obj.addHeader("authorization", "Bearer " + m.global.authToken)
    end if

    if (obj.AsyncPostFromString(body))
        msg = wait(10000, obj.GetPort())
        if (type(msg) = "roUrlEvent")
          code = msg.GetResponseCode()
          ? "response code "; code

          if (code = 200)
             ? msg.GetString()
             return msg.GetString()
          else
            ' error case
            return msg.GetString()
          endif
        else if (msg = invalid)
          request.AsyncCancel()
        endif
    endif

    return invalid
end function
0 Kudos
4 REPLIES 4
tim_beynart
Channel Surfer

Re: Handling / Throwing errors

obj.RetainBodyOnError(true) will allow you to read the response body
0 Kudos
tim_beynart
Channel Surfer

Re: Handling / Throwing errors

BTW, in SceneGraph you can write one task to handle all your HTTP requests and reuse it all over your app
0 Kudos
nmaves
Visitor

Re: Handling / Throwing errors

Thanks Tim,

I have chosen to use multiple small tasks that all use some helper functions like the one above. 

Even if I read the response body and return that from the above function, the caller would not know if it was an error or not.  I am now thinking about returning a object with a status and response.  Heck, maybe I should just return the message itself.
0 Kudos
tim_beynart
Channel Surfer

Re: Handling / Throwing errors

There are so many ways to design this, but personally I use a task with a `response` field and an `error` field, and the clients will observe both. I include the HTTP code and the body of the response for both.
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.