abp icon indicating copy to clipboard operation
abp copied to clipboard

BusinessException is not being converted to proper HTTP Response

Open ebicoglu opened this issue 6 years ago • 7 comments

When throwing a BusinessException, the HTTP response should write Error Code, Error Message, and Error Details properties to the response.

This is where a business exception is being thrown: https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs#L40

But there's no response text. image

Besides, it would be good if we can specify the HTTP Error code when throwing the business exceptions. If the resource is not found, I would like to throw 404; if the user is not granted permission: 403, etc...

Example: https://stackoverflow.com/a/38935583/1767482

ebicoglu avatar Apr 07 '20 18:04 ebicoglu

Actually it is converted to Forbidden by default:

https://github.com/abpframework/abp/blob/bb46a964bce85eeef39efb70641f2ec0f560ee75/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs#L23-L64

ABP handles exceptions with an MVC filter. However, you are throwing in a middleware. In order to handle it we need to add another middleware before it (I don't suggest it).

I suggest you to not throw business exceptions inside an asp.net core middleware (since it is not a "business" layer), but directly return any HTTP Status code and message you need.

hikalkan avatar Apr 08 '20 07:04 hikalkan

@hikalkan is this the recommended location to have a BusinessException

https://docs.abp.io/en/abp/latest/Tutorials/Part-6?UI=NG&DB=EF#authormanager-the-domain-service

image

buaziz avatar Jul 26 '20 11:07 buaziz

@ebicoglu can you assist on the above.

buaziz avatar Jul 28 '20 07:07 buaziz

hi @ebicoglu

We should also configure the AbpExceptionLocalizationOptions

https://github.com/abpframework/abp/commit/4bc619c4c2c6f5a32efc87bd293e85dfe1eb5b1a

maliming avatar Aug 06 '20 07:08 maliming

@maliming just a note, it worked after applying your fix.

But the title of the toaster service in angular shows

"An error has occurred!" and my localized message was shown correctly.

I tried to set the BusinessException "Message" and "Details" properties. but still no go.

buaziz avatar Aug 06 '20 17:08 buaziz

hi @buaziz If this problem related angular please create a new issue.

maliming avatar Aug 07 '20 00:08 maliming

@maliming https://github.com/abpframework/abp/issues/4986

buaziz avatar Aug 07 '20 07:08 buaziz