Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Exception passing to WPF #4670


User avatar
0
itzoar2 created

<a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Handling-Exceptions">https://aspnetboilerplate.com/Pages/Doc ... Exceptions</a>

May i know if i want to pass Exception Message to my WPF client

on the WPF client, i have used the library Abp.WebApi.Client.IAbpWebApiClient

but i am not sure how to pass the exception from the backend to this client.

Please advise. THanks a lot


7 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @itzoar2,

    This sample might be helpful for you <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/ConsoleRemoteWebApiCall">https://github.com/aspnetboilerplate/as ... WebApiCall</a>.

  • User Avatar
    0
    itzoar2 created

    Hi

    So on the Backend server side can we still throw UserFriendlyException ?

    Will AbpRemoteCallException class be able to catch UserFriendlyException ?

    <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Handling-Exceptions">https://aspnetboilerplate.com/Pages/Doc ... Exceptions</a>

    Look forward your reply. Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Yes, because UserFriendlyException is serializable, see <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/UI/UserFriendlyException.cs">https://github.com/aspnetboilerplate/as ... ception.cs</a>.

  • User Avatar
    0
    itzoar2 created

    When my backend service throw back a new UserFriendlyException

    then on my WPF cleint (below code), i didn't catch on the AbpRemoteCallException block

    So my question is , should I throw userfriendlyxception or abpremotecallexception if know client is not client calling back end while API layer

    Please advise. Thanks a lot

        public async Task&lt;int&gt; AddAsync&lt;TInput&gt;(TInput input, string serviceLink)
        {
            try
            {
                var result = await _abpWebApiClient.PostAsync&lt;object&gt;(BaseUrl + serviceLink, input, timeout: null);
                return Convert.ToInt32(result);
            }
            catch (AbpRemoteCallException e)
            {
                Logger.Error("Error on AddAsync : input " + input.ToString());
                Logger.Error("Error on AddAsync : serviceLink" + serviceLink);
                Logger.Error("Error on AddAsync : Error Message" + e.Message);
                throw;
            }
    
            catch (Exception e)
            {
                Logger.Error("Error on AddAsync : input " + input.ToString());
                Logger.Error("Error on AddAsync : serviceLink" + serviceLink);
                Logger.Error("Error on AddAsync : Error Message" + e.Message);
                throw;
            }
        }
    
  • User Avatar
    0
    itzoar2 created

    I have made use the example console app <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/ConsoleRemoteWebApiCall">https://github.com/aspnetboilerplate/as ... WebApiCall</a>

    I find that the webapi client won't catch any UserFriendlyException or AbpRemoteCallException exception

    It can only catch Abp.AbpException

    and it always return somethings like

    "Could not made request to <a class="postlink" href="http://localhost:6240/api/services/app/role/GetRoles">http://localhost:6240/api/services/app/role/GetRoles</a>! StatusCode: InternalServerError, ReasonPhrase: Internal Server Error"

    Please advise how can a WPF client that call WebApi layer can get meaningful exception. Thanks

    My backend GetRoles function

        public async Task&lt;ListResultDto&lt;RoleListDto&gt;> GetRoles(GetRolesInput input)
        {
    

    throw new UserFriendlyException(404, "message", "MessageDetail"); var roles = await _roleManager .Roles .WhereIf( !input.Permission.IsNullOrWhiteSpace(), r => r.Permissions.Any(rp => rp.Name == input.Permission && rp.IsGranted) ) .ToListAsync();

            return new ListResultDto&lt;RoleListDto&gt;(roles.MapTo&lt;List&lt;RoleListDto&gt;>());
        }
    
  • User Avatar
    0
    itzoar2 created

    Just check the source code of AbpWebApiClient

    <a class="postlink" href="http://sourcebrowser.io/Browse/aspnetboilerplate/aspnetboilerplate/src/Abp.Web.Api/WebApi/Client/AbpWebApiClient.cs">http://sourcebrowser.io/Browse/aspnetbo ... iClient.cs</a>

    Looks like the web api client always throw AbpException. May i know how to change the backend function in order to generate the AbpRemoteCallException in this case. Thanks

        public async Task&lt;TResult&gt; PostAsync&lt;TResult&gt;(string url, object input, int? timeout = null)
            where TResult : class, new()
        {
            using (var client = new HttpClient())
            {
                client.Timeout = timeout.HasValue ? TimeSpan.FromMilliseconds(timeout.Value) : Timeout;
    
                if (!BaseUrl.IsNullOrEmpty())
                {
                    client.BaseAddress = new Uri(BaseUrl);
                }
    
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                using (var requestContent = new StringContent(Object2JsonString(input), Encoding.UTF8, "application/json"))
                {
                    using (var response = await client.PostAsync(url, requestContent))
                    {
                        if (!response.IsSuccessStatusCode)
                        {
    

    <span style="color:#0040FF"> throw new AbpException("Could not made request to " + url + "! StatusCode: " + response.StatusCode + ", ReasonPhrase: " + response.ReasonPhrase);</span> }

                        var ajaxResponse = JsonString2Object&lt;AjaxResponse&lt;TResult&gt;>(await response.Content.ReadAsStringAsync());
                        if (!ajaxResponse.Success)
                        {
                            throw new AbpRemoteCallException(ajaxResponse.Error);
                        }
    
                        return ajaxResponse.Result;
                    }
                }
            }
        }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    @itzoar2 The problem is, even when you throw UserFriendlyException on the server side, the returned status code will be 500 to client. So, if you want to get the UserFriendlyException message on the client side, you need to run your code in a try-catch block like below;

    try
    {
        //Make your Http request here...
    }
    catch (WebException wex)
    {
        var pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
    }