Web.Download() function

Hi everyone,

I have few questions about function web.Download();

  1. What type of responses does it return?

  2. What possible values can it return?

  3. What kind of errors can it return? If it returns SyntaxError can we pull out the message?

Basically I am looking for ways to handle errors so it doesn’t crash SambaPOS in case API returns some rubish.

Thanks in advance!

I beleive its basically get contents, it just returns the raw data for whatever page/content is returned.
Api returning json does so in plain text, hence the parse you would do after download.
Syntax error in the responce would cause failed parse.
Most api return a valid data set even in a failed request obviously as long as all is up and running.
So if your request is bad a good api would return an error with some form of reason it.
There are a couple of web. Type functions depending on what your doing and auth etc.

1 Like

I am downloading a JSON string from an exchange rate API. It’s all good when it downloads it but when you are offline or some other type of error occurs it just kind of breaks the script.

Yes, that’s specifically what I was trying to catch. Made a way around it but would still like to catch an error so I know what I am dealing with. At the moment if JSON.parse() fails it just return Syntax Error if the string was empty for whatever reason.

It’s a bit of a hassle to debug lol

Thats why I am wondering whether web.Download() natively returns some sort of error in these cases

Is the returned value null/empty? First think after download would be to check if it has a value.

However I have noticed and tried to ask before but dont think I explained well that I think there has been changed in the framework as used to get a error message on pms intergration (even an error message returned from api) returns null.
My guess is .net picks up on error code header of response and returns null instead.
Checking if null would fail safe either way however now you say it yes errors have stopped being returned however I noticed similar in powerbi that even a responce from api that was error message wasn’t returned which is why I think its a windows/.net change.

Yes, if you are offline or something it returns an empty string.

Yup! Took me good few hours to figure that out haha

Added more, you reminding me, yes error messages are changed to null by something. Think its windows rather than samba. Did try and find a solution, expect a regedit would change but havnt found a solution.

Yeah, it doesn’t return an error it just returns empty or null for which if you dont check breaks up your script.
Though, it only crashed SambaPOS once in like 400 API calls.

Still though, would be nice if you could catch an error, handle it & log/output it on the screen. At the moment I am thinking its not possible.

Try using try catch, as shown in the next post, I haven’t tried honestly if that catch an api call error properly in samba. Report filtering email as pdf

2 Likes

As JTR said, always check response if null or empty.

Make a bad request to the API to get an error response. Whatever token holds the error information, check if that token is set. If it’s missing, or null, you’re good to go.

Another option would be loading RestSharp using reflection and parse the IRestResponse response that contains the response code.

1 Like