Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
Jeff_001
New Member

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!

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Jeff_001 

 

Download example PBIX file

 

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.

 

errec.png

 

 

You can trap the error by adding a subsequent step

 

errec2.png

 

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
    Page

 

Regards

 

Phil

 

 

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

1 REPLY 1
PhilipTreacy
Super User
Super User

Hi @Jeff_001 

 

Download example PBIX file

 

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.

 

errec.png

 

 

You can trap the error by adding a subsequent step

 

errec2.png

 

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
    Page

 

Regards

 

Phil

 

 

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.

October NL Carousel

Fabric Community Update - October 2024

Find out what's new and trending in the Fabric Community.