Skip to main content

Notifications

Customer Service forum
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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,867 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,173 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans