felixhageloh / uebersicht

ˈyːbɐˌzɪçt
GNU General Public License v3.0
4.54k stars 166 forks source link

Try/catch in update #53

Closed farski closed 9 years ago

farski commented 9 years ago

I'm JSON.parseing an API that doesn't have a very good data policy; they return XML rather than JSON in a lot of situations. I never care about the XML, so I just need a way of avoiding parsing errors, but doing a try/catch inside of update seems to break the widget. Is there a better way to handle things like this?

felixhageloh commented 9 years ago

I don't care much for XML either :). But seriously, can you give more details on how the widget breaks - it doesn't update anymore?

farski commented 9 years ago

If I add an empty try/catch I usually get 9/25/14 1:07:06.322 PM Übersicht[446]: error in widget mbta_85in-school-coffee: unexpected (, if I have anything in the try it's similar, but depends on what the code is actually doing (eg unexpected =, etc)

felixhageloh commented 9 years ago

could you paste your try/catch block here? I just tried using one and it works fine, so maybe it is just a syntax mistake

farski commented 9 years ago
update: (output, element) ->
  try {
    json = JSON.parse(output)

    predictedArrival = json['mode'][0]['route'][0]['direction'][0]['trip'][0]['pre_dt']
    t = new Date(predictedArrival* 1000)

    $(element).find('h3').text(@getHHMMString(t) )
  } catch(e) {

  }
felixhageloh commented 9 years ago

hey and sorry for the late reply! You are using the javascript syntax, which is slightly different. This is the correct CoffeeScript syntax is:

update: (output, element) ->
  try
    json = JSON.parse(output)

    predictedArrival = json['mode'][0]['route'][0]['direction'][0]['trip'][0]['pre_dt']
    t = new Date(predictedArrival* 1000)

    $(element).find('h3').text(@getHHMMString(t) )
  catch e
    # do nothing