Catching non lethal Wix SDK errors

Hi

I’m pulling in external image files in a repeater. However, because the URLs are being supplied by members, I want to be able to able to control what is displayed if the file at the end of the URL turns out not to be an image (rather than just do basic validation on the string for https://, jpg etc).

Currently when I pass a bad link to image.src, I get a Wix SDK error in the console, and the image doesn’t show, but I haven’t found a way to catch this error in my code so I can display something else in that scenario. The workaround I’ve been using is to have an 'image URL Error" image behind the dynamic image, which only gets seen if the dynamic one doesn’t show, but I need something a bit more flexible.

So I’d be looking for something like the below, but the try/catch doesn’t work, possibly because the error is non lethal - or perhaps because my code is bad?

Thank you for any advice !

Tom

export function showImageByUrl(imageTarget, imageUrl) {

if (imageUrl.length > 0) {
try {
$w(imageTarget).src = imageUrl;
$w(imageTarget).tooltip = imageUrl;
}
catch (err) {
displayImageError)
}
}
}

What’s that:

 displayImageError) 

That looks wrong!!!

Try this:


 displayImageError() 

Thanks ! Actually I just put in the displayImageError as a placeholder to show where it should fire, though of course you’re right I’d missed the brackets. The problem is that the Catch doesn’t fire when I use a bad URL - I’m actually using Console Log as below at the moment - though it never fires, I just get: "Wix code SDK error: The “src” property cannot be set to “…etc” in the console.

export function showImageByUrl(imageTarget, imageUrl) {
console.log(“showImageByURL running”)
if (imageUrl.length > 0) {
try {
$w(imageTarget).src = imageUrl;
$w(imageTarget).tooltip = imageUrl;
}
catch (err) {
console.log("ERROR CAUGHT: ")
}
}
}

What about:

$w("#image").src = imageUrl;
if ($w("#image").src !== imageUrl){/*do whatever you want*/}

@jonatandor35
What?
How make that sense? :smiley:

First you initalize the value and then you check for equality?
I mean that if-statment should be always false - defacto: useless!

@benibrodi first you try to assign the value, and if the link doesn’t fit the assignment fails (“src” property cannot be set to…" ).
Then you check if the assignment failed or not. Doesn’t make sense?

@jonatandor35 Yes you’re right - I see your trick :smiley: