Jump to content

Error management


This topic is 1596 days old. Please don't post here. Open a new topic instead.

Recommended Posts

 
Hello
I use a function that allows me to recover data on openbooks (title, author, publisher ... cover image ...); if one of the elements is missing I have an error returned. Attached is the one returned if the image is missing.
How to handle this and prevent the message from appearing?
Thank you for your help

error.png

Link to comment
Share on other sites

Here is the function

Quote

RegisterGroovy( "JSON2FMP( url )" ; "import groovy.json.*¶

vsm_titre = vsm_auteur1 = vsm_auteur2 = vsm_nbPages = vsm_urlImage = vsm_Image = vsm_Editeur = \"\"¶

def parsedResponse = new JsonSlurper().parse(new URL(url))¶

if (parsedResponse.size() == 1 ){¶

    root = parsedResponse.keySet()[0]¶
    keySet = parsedResponse.\"${root}\".keySet()¶

    vsm_titre = parsedResponse.\"${root}\".title¶
    if (parsedResponse.\"${root}\".authors != null) {¶
        vsm_auteur1 = parsedResponse.\"${root}\".authors[0].name¶
        vsm_auteur2 = parsedResponse.\"${root}\".authors.size() > 1 ? parsedResponse.\"${root}\".authors[1].name : \"\" ¶
    }¶
    vsm_nbPages = parsedResponse.\"${root}\".number_of_pages¶

    vsm_Editeur = parsedResponse.\"${root}\".publishers.name¶
        
   
    vsm_urlImage = parsedResponse.\"${root}\".cover.medium¶
    vsm_Image = new URL ( vsm_urlImage )¶
    1¶
}"; "isGui=false" )

 
Honesty obliges I am not the creator! Clem present on the forum is the daddy of this job!

I try to use openbooks since amazon allows its API only to those who sell the apps !!!

Edited by Noél Dubau
Link to comment
Share on other sites

Try this

 

RegisterGroovy( "JSON2FMP( url )" ; "import groovy.json.*¶
¶
vsm_titre = vsm_auteur1 = vsm_auteur2 = vsm_nbPages = vsm_urlImage = vsm_Image = vsm_Editeur = ''¶
¶
parsedResponse = new JsonSlurper().parse(new URL(url))¶
¶
if (parsedResponse.size() == 1 ){¶
	root = parsedResponse.keySet()[0]¶
	keySet = parsedResponse.\"${root}\"¶
¶
vsm_titre = keySet.title?:''¶
¶
vsm_auteur1 = keySet.authors[0]?keySet.authors[0].name:''¶
vsm_auteur2 = keySet.authors[1]?keySet.authors[1].name:''¶
vsm_nbPages = keySet.number_of_pages?:''¶
vsm_Editeur = keySet.publishers.name?:''¶
vsm_urlImage = keySet.cover.medium?:null¶
¶
vsm_Image = vsm_urlImage?new URL ( vsm_urlImage ):''¶
¶
} else {¶
	return ''¶
}" ; "isGui = false" )

it just tests each node is there and then extracts the value if it is

Link to comment
Share on other sites

I just tried it ;

  • I  encountered the error about the cover and many times a blank cover was added in the container.
  • A new error appeared and I don't know the reason

1677772602_Capturedecran2019-10-22a14_25_35.png.96e78d18f3dcae1964c32252e265e2f3.png

Capture d’écran 2019-10-22 à 14.21.40.png

Edited by Noél Dubau
Link to comment
Share on other sites

can you post the sample URL??

 

I am pretty sure it might be because the 'cover' node does not exists and so it can't find the medium sub-node on it...

 

try replacing the line with...

vsm_urlImage = keySet.cover?keySet.cover.medium:null

 

Edited by john renfrew
Link to comment
Share on other sites

For the error "Cannot invoke method getAt() on null object the following ones : a webviewer on these url is not empty but we don't get any info about the book

 

Link to comment
Share on other sites

The issue is that these books have no authors - so it fails quite early on...

I changed the code to test for each presence

 

Quote

RegisterGroovy( "JSON2FMP( url )" ; "import groovy.json.*¶
¶
//vsm_titre = vsm_auteur1 = vsm_auteur2 = vsm_nbPages = vsm_urlImage = vsm_Image = vsm_Editeur = ''¶
¶
parsedResponse = new JsonSlurper().parse(new URL(url))¶
¶
if (parsedResponse.size() == 1 ){¶
	root = parsedResponse.keySet()[0]¶
	keySet = parsedResponse.\"${root}\"¶
¶
	vsm_titre = keySet.title?:''¶
	vsm_auteur1 = keySet.authors?keySet.authors[0].name:''¶
	vsm_auteur2 = (keySet.authors && keySet.authors[1])?keySet.authors[1].name:''¶
	vsm_nbPages = keySet.number_of_pages?:''¶
	vsm_Editeur = keySet.publishers?keySet.publishers.name:''¶
	vsm_urlImage = keySet.cover?keySet.cover.medium:null¶
¶
	vsm_Image = vsm_urlImage?new URL ( vsm_urlImage ):''¶
} else {¶
	return ''¶
}" ; "isGui = false " )

 

 

Edited by john renfrew
Link to comment
Share on other sites

you could also be really smart and return the whole thing as a JSON block too... (this definitely needs SM 5.2+)

 

return JsonOutput.toJson([vsm_titre: vsm_titre, vsm_auteur1: vsm_auteur1, vsm_auteur2: vsm_auteur2, vsm_nbPages:  vsm_nbPages, vsm_Editeur: vsm_Editeur, vsm_Image: vsm_Image])

 

Link to comment
Share on other sites

I presume at the moment you are doing lots of SMGetVariable calls after running this function

If you added the return statement right at the end you would get back all the answers as a transformed JSON block you could process in a different way...

Link to comment
Share on other sites

1 hour ago, john renfrew said:

I presume at the moment you are doing lots of SMGetVariable calls after running this function

If you added the return statement right at the end you would get back all the answers as a transformed JSON block you could process in a different way...

Sorry to be so bad pupil but where exactly must I add that line  ☹️

Link to comment
Share on other sites

What you get back is JSON version of the information you asked the function for..

so instead of SMGetVariable calls you would use JSONGetElement instead..

 

The benefit was to show how this was possible - not to make you change how you do things, it's up to you if change your implementation, but was to show how to export the data out as a JSON block

Link to comment
Share on other sites

  • 2 weeks later...

hello John

Sorry to come back with a new problem. I launched the acquisition of datas on 1117 isbn or ean. It runs fine except these four references.

9782874260377 - 9782070511914 - 9782092021637 - 2713506859

These references returns me 2 alerts

Have you ideas to void these alerts ?

Thanks

ND

Capture d’écran 2019-11-01 à 09.44.21.png

Capture d’écran 2019-11-01 à 09.44.45.png

Link to comment
Share on other sites

  • 2 weeks later...

Hello John

My question was answered privately by a participant in the French forum, but also on this one, I named ericire. His strategy was to modify the function by detecting these error messages to avoid the display of information boxes. I allow myself to join his code (still Hebrew for the novice that I am).

RegisterGroovy( "JSON2FMP( url )" ; "import groovy.json.*¶try {¶parsedResponse = new JsonSlurper().parse(new URL(url))¶¶if (parsedResponse.size() == 1 ){¶    root = parsedResponse.keySet()[0]¶    keySet = parsedResponse.\"${root}\"¶¶    vsm_titre = keySet.title?:''¶    vsm_auteur1 = keySet.authors?keySet.authors[0].name:''¶    vsm_auteur2 = (keySet.authors && keySet.authors[1])?keySet.authors[1].name:''¶    vsm_nbPages = keySet.number_of_pages?:''¶    vsm_Editeur = keySet.publishers?keySet.publishers.name:''¶    vsm_urlImage = keySet.cover?keySet.cover.medium:null¶    vsm_Image = vsm_urlImage?new URL ( vsm_urlImage ):''¶} else {¶return 'pas de résultat'¶}¶} catch (Exception e) {¶return 'Erreur : ' + e¶}" )

Merci à tous deux

Noël

Link to comment
Share on other sites

This topic is 1596 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.