How to add value to the default exception response in ABP?

北城以北 提交于 2019-12-14 03:09:42

问题


I want to add an ID (GUID) to the exception and:

  1. Log it
  2. Return it to the client json response

Where should I generate this log ID value and add it to the exception message that is logged. And where to change the following default response?

{
  "targetUrl": null,
  "result": null,
  "success": false,
  "error": {
    "message": "An internal error occurred during your request!",
    "details": "..."
  },
  "unAuthorizedRequest": false
}

I am using .NET Core version.


回答1:


If you want to disable displaying the message for a particular AJAX call, add abpHandleError: false into the abp.ajax options.

Or you can disable the default behavior of the framework exception wrapper

public class PeopleController : AbpController
{
    [HttpPost]
    [WrapResult(WrapOnSuccess = false, WrapOnError = false)]
    public JsonResult SavePerson(SavePersonModel person)
    {
        //TODO: save new person to database and return new person's id
        return Json(new {PersonId = 42});
    }
}

https://aspnetboilerplate.com/Pages/Documents/Javascript-API/AJAX?searchKey=wrap#asp-net-mvc-controllers


Another thing is; you can send exception details to the client by the below configuration

...
using Abp.Web.Configuration;
...
public override void PreInitialize() 
{
    Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
}
...

https://aspnetboilerplate.com/Pages/Startup-Configuration#configuring-modules


Result Wrapping & Exception Handling:

ASP.NET Boilerplate does not wrap Web API actions by default if an action has successfully executed. It, however, handles and wraps exceptions. You can add the WrapResult/DontWrapResult attributes to actions and controllers for finer control. You can change this default behavior from the startup configuration (using Configuration.Modules.AbpWebApi()...). See the AJAX document for more info about result wrapping.

https://aspnetboilerplate.com/Pages/Documents/Web-API-Controllers?searchKey=wrap#result-wrapping-exception-handling


Wrapping Results

ASP.NET Boilerplate wraps the return values of dynamic Web API actions using an AjaxResponse object. See the ajax documentation for more information on wrapping. You can enable/disable wrapping per method or per application service. See this example application service:

public interface ITestAppService : IApplicationService
{
    [DontWrapResult]
    DoItOutput DoIt(DoItInput input);
}

https://aspnetboilerplate.com/Pages/Documents/Dynamic-Web-API?searchKey=wrap#wrapping-results


Lastly you can write your own ResultWrapperHandler...

public class CustomResultWrapperHandler : ResultWrapperHandler, ITransientDependency
{

    //...
    protected override void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
    {

        //...

        base.WrapResultIfNeeded(request, response);
    }
}


来源:https://stackoverflow.com/questions/50912659/how-to-add-value-to-the-default-exception-response-in-abp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!