Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Suggested answer

Catch server's response message in plugin

Posted on by 165

Hello,

I've written a plugin which consumes a business central web service.

It works great when the server's response is 200.

When the server's reponse is 500 (internal server error) I need to show the user the error message. The problem is that I think I'm not getting the error message.

When I consume the web service from postman or Wizdler, I get an error like this:

pastedimage1632745138851v1.png

When I consume the web service from the plugin, I only get this:

pastedimage1632745264667v2.png

How am I supposed to get the error and show it to user?

here is part of the code:

                       HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                        if (response.StatusCode == HttpStatusCode.OK)
                        {

                            Stream responseStream = response.GetResponseStream();
                            string responseStr = new StreamReader(responseStream).ReadToEnd();

                            string result = responseStr;
                            context.OutputParameters["resultString"] = result;
                        }
                        else if(response.StatusCode == HttpStatusCode.InternalServerError)
                        {
                            string result = response.StatusDescription;
                            context.OutputParameters["resultString"] = result;
                        }
                    }

                }

                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
                }

                catch (Exception ex)
                {
                    tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
                    throw;
                }
            }

  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: Catch server's response message in plugin

    Hi,

    In code only use Exception catch and in the same catch try to add below code:

    throw new InvalidPluginExecutionException(ex.message);

    Thanks,
    Pradeep.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans