Forum Discussion
try... otherwise do not execute the otherwise clause in Web.Contents function
Hi,
I'm getting data from a server on my LAN and I need to redirect the request to another endpoint on web in case the script found an error in the LAN endpoint.
The error that I am expecting to found in the LAN endpoint is knowed: the server is out of reach since it's not in the same LAN.
Here is the step I'm getting the issue:
---------------------------
UrlLocal = "192.168.0.1:5588/endpoint",
UrlWEB = "https://somewebcontent.net/otherendpoint ",
Response = try Web.Contents(UrlLocal) otherwise Web.Contents(UrlWEB)
[...]
---------------------------
I had try the try... otherwise clause, but the Power Query does not seens return the otherwise clause, it simply block on DataSource.Error: impossible to contact the remote host.
The Web.Contents(UrlLocal) and Web.Contents(UrlWEB) works just fine if they I use them without the try...otherwise.
Obs.: the try...otherwise also does not work if:
Response = try Web.Contents(UrlLocal) otherwise error "Custom error"
Response = try Web.Contents(UrlLocal) otherwise "anything else"
What I am possible doing wrong?
Thanks a lot!
Hi Jeff_001
try .. otherwise (annoyingly) doesn't always flag errors even when an error has clearly occurred.
For example, if you do this with a non-existant URL
= try Web.Contents(UrlLocal)an error occurs but the result of that step is this, showing no error, yet the Value of the record is an error.
You can trap the error by adding a subsequent step
then testing the HasError part of that record, and doing different things depending on whether HasError is true or false
= if Value[HasError] then Web.Contents(UrlWEB) else Web.Page(Value[Value])Here's the full query
let UrlLocal = "192.168.0.1:5588/endpoint", UrlWEB = "https://bbc.co.uk", //"https://somewebcontent.net/otherendpoint ", Response = try Web.Contents(UrlLocal), Value = try Text.From(Response[Value]), Page = if Value[HasError] then Web.Contents(UrlWEB) else Web.Page(Value[Value]) in PageRegards
Phil
1 Reply
- PhilipTreacySuper User
Hi Jeff_001
try .. otherwise (annoyingly) doesn't always flag errors even when an error has clearly occurred.
For example, if you do this with a non-existant URL
= try Web.Contents(UrlLocal)an error occurs but the result of that step is this, showing no error, yet the Value of the record is an error.
You can trap the error by adding a subsequent step
then testing the HasError part of that record, and doing different things depending on whether HasError is true or false
= if Value[HasError] then Web.Contents(UrlWEB) else Web.Page(Value[Value])Here's the full query
let UrlLocal = "192.168.0.1:5588/endpoint", UrlWEB = "https://bbc.co.uk", //"https://somewebcontent.net/otherendpoint ", Response = try Web.Contents(UrlLocal), Value = try Text.From(Response[Value]), Page = if Value[HasError] then Web.Contents(UrlWEB) else Web.Page(Value[Value]) in PageRegards
Phil