Open Closed

Return Response from Controller as badrequest #6516


User avatar
0
ed_developer3 created

ABP Framework version: v7.4.4 UI Type: Angular Database System: EF Core (SQL Server)

I have controller which is calling the service

    public class ContactInformationsAppService : ApplicationService, IContactInformationsAppService
    {
        [Authorize(ClientsPermissions.ContactInformations.Create)]
        public virtual async Task<ContactInformationDto> CreateAsync(ContactInformationCreateDto input)
        {
            if (await _contactInformationManager.checkContactExist(input.Email))
            {
                throw new UserFriendlyException("email {0} already exists", input.Email);
            }

            var contactInformation = await _contactInformationManager.CreateAsync(
            input.ClientInfomationIds, input.ContactTypeId, input.ProvinceTypeId, input.FirstName, input.LastName, input.PhoneNumber, input.PreferredContactWay, input.IsIndemnitor, input.Percentage, input.CellNumber, input.FaxNumber, input.Email, input.Notes, input.MailingAddress, input.City, input.PostalCode, input.BusinessAddress, input.Website, input.Company, input.Description, input.ContactCode, input.AlternateEmail
            );

            return ObjectMapper.Map<ContactInformation, ContactInformationDto>(contactInformation);
        }
    }
// controller method
 public class ContactInformationController : AbpController, IContactInformationsAppService  
    {  private readonly IContactInformationsAppService _contactInformationsAppService;

        [HttpPost]
        public virtual Task<ContactInformationDto> CreateAsync(ContactInformationCreateDto input)
        {

            return _contactInformationsAppService.CreateAsync(input);
        }
    }

How can i return the message to the consumer that Contact was not created because email already exists. Since the return type Task<ContactINformationDTO> i am not able to set the request as bad request and also how can we return a response from service to controller and then controller to consumer that email does not exist.

Thanks


8 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    What is the response when you call this API from Angular when email already exists?

  • User Avatar
    0
    ed_developer3 created

    Its 403 which comes when user does not have permission

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    What is the result when user have the perrmison?

  • User Avatar
    0
    ed_developer3 created

    when we say Busnessexception or friendly exception it results into status code 403 which is wrong code as user has permissions but contact cannot be added due to validation so we wanted to show a message with different status code than 403

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try to override the GetStatusCode method of DefaultHttpExceptionStatusCodeFinder

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs#L70-L73

  • User Avatar
    0
    ed_developer3 created

    hi Swagger response and controller code Actually on frontend 403 is being taken as forbidden access message and code we are using is causing 400 in response text and not response code its still 403. Where to inject DefaultHttpExceptionStatusCodeFinder inherited class to override the response code.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can override the DefaultHttpExceptionStatusCodeFinder in your web module of API website(44314).

  • User Avatar
    0
    ed_developer3 created

    ya it worked thanks

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19