Base solution for your next web application
Open Closed

Handling User Defined Errors on Controller #468


User avatar
0
thobiasxp created

Hi,

We want to throw some user defined errors from controller now we were using throw User friendly Exception for showing Errors it works in services but it not perfect in Controller. if we throw errors from controller it just work less. so how we can throw errors from controller expect a possible guidance from you.

Currently used This: throw new UserFriendlyException(L("CloumnNameMustbeSameDownloadTemplateFileforReference"));

Thanks...


8 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    I couldn't understand what's the problem with throwing UserFriendlyException in a Controller. Isn't it working at all or are there bugs in special cases?

  • User Avatar
    0
    thobiasxp created

    Hi,

    how to throw errors from(Asp MVC) controller to Angularjs controller while uploading a file. User friendly Exception is not working in file uploading case i need an explanation about this.

    Thanks.

  • User Avatar
    0
    hikalkan created
    Support Team

    OK, now understood. I created an issue about this: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/issues/58">https://github.com/aspnetzero/aspnet-zero/issues/58</a> I will resolve it in v1.6.1. I hope it's not critical for you and you can wait for a while. Thanks.

  • User Avatar
    0
    thobiasxp created

    Hi,

    But it's most important for me. because we could not identified what are the issues arise in controller. can i get any other alternative way for solve this..for resolving this only i will move to forward. kindly give a possible solution.

    Thanks...

  • User Avatar
    0
    aranhico created

    I'm having the same problem, is this resolved?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    This was related to AspNet Zero (<a class="postlink" href="http://www.aspnetzero.com/">http://www.aspnetzero.com/</a>) and resolved. Do you have similar problem? Can you sahre your controller action?

  • User Avatar
    0
    aranhico created

    Hi! sure

    // GET: FileUploader
            public async virtual Task<JsonResult> Index()
            {
     string output;
                try
                {
    
                  
                    int teste = Request.Files.Count;
                   
    
                    CreateUpdateImovelByPortfolioInput dto = new CreateUpdateImovelByPortfolioInput();
                    dto.PortfolioId = Convert.ToInt16(Request.Params.Get("portfolioId"));
                    using (ZipArchive archive = ZipFile.OpenRead(@"C:\sd.zip"))
                    {
                        foreach (ZipArchiveEntry entry in archive.Entries)
                        {
    
                            if (entry.FullName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
                            {
                                using (MemoryStream ms = new MemoryStream())
                                {
                                                                 entry.Open().CopyTo(ms);
    //etc
    
                                }
                            }
                        }
                    }
                 output=  await _service.Create(dto); 
                     
                }
                catch(Exception o)
                {
                    throw new UserFriendlyException(o.ToString()); 
                }
                return Json(new { Data = output });
    }
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    OK, understood it. You are probably using a 3rd party ajax file upload library which does not handle ABP's responses. You have two options:

    1. Add [DontWrapResult] attribute to your Index action, so ABP does not wrap/change your return value. And return a proper object which is understood by your library instead of throwing exceptions. UserFriendlyException does not work in this case.
    2. This depends on your library. I used angular file upload (<a class="postlink" href="https://github.com/nervgh/angular-file-upload">https://github.com/nervgh/angular-file-upload</a>) and handled errors on onSuccessItem as shown below:
    vm.uploader.onSuccessItem = function (fileItem, response, status, headers) {
                    $uibModalInstance.close();
    
                    if (response.success) {
                        //...
                    } else {
                        abp.message.error(response.error.message);
                    }
                };
    

    Note: Be sure that you're using the latest Abp.* packages.