Skip to content

Commit dcc685d

Browse files
[PM-34822] Consistent error response 400 and 404 in Org Integrations controller (#7458)
1 parent 0230b83 commit dcc685d

16 files changed

Lines changed: 85 additions & 74 deletions

src/Api/Dirt/Controllers/OrganizationIntegrationController.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Bit.Api.Dirt.Models.Response;
33
using Bit.Core.Context;
44
using Bit.Core.Dirt.EventIntegrations.OrganizationIntegrations.Interfaces;
5-
using Bit.Core.Exceptions;
65
using Microsoft.AspNetCore.Authorization;
76
using Microsoft.AspNetCore.Mvc;
87

@@ -18,17 +17,17 @@ public class OrganizationIntegrationController(
1817
IGetOrganizationIntegrationsQuery getQuery) : Controller
1918
{
2019
[HttpGet("")]
21-
public async Task<List<OrganizationIntegrationResponseModel>> GetAsync(Guid organizationId)
20+
public async Task<ActionResult<List<OrganizationIntegrationResponseModel>>> GetAsync(Guid organizationId)
2221
{
2322
if (!await HasPermission(organizationId))
2423
{
25-
throw new NotFoundException();
24+
return NotFound();
2625
}
2726

2827
var integrations = await getQuery.GetManyByOrganizationAsync(organizationId);
29-
return integrations
28+
return Ok(integrations
3029
.Select(integration => new OrganizationIntegrationResponseModel(integration))
31-
.ToList();
30+
.ToList());
3231
}
3332

3433
/// <summary>
@@ -38,7 +37,7 @@ public async Task<List<OrganizationIntegrationResponseModel>> GetAsync(Guid orga
3837
/// <param name="organizationId"></param>
3938
/// <param name="model"></param>
4039
/// <returns></returns>
41-
/// <exception cref="NotFoundException">Not enough permissions to access the organization.</exception>
40+
/// <exception cref="NotFoundResult">Not enough permissions to access the organization.</exception>
4241
/// <exception cref="ConflictResult">When an integration of the same type already exists for the organization.</exception>
4342
[HttpPost("")]
4443
public async Task<ActionResult<OrganizationIntegrationResponseModel>> CreateAsync(Guid organizationId, [FromBody] OrganizationIntegrationRequestModel model)
@@ -50,7 +49,7 @@ public async Task<ActionResult<OrganizationIntegrationResponseModel>> CreateAsyn
5049

5150
if (!await HasPermission(organizationId))
5251
{
53-
throw new NotFoundException();
52+
return NotFound();
5453
}
5554

5655
var integration = model.ToOrganizationIntegration(organizationId);
@@ -62,40 +61,40 @@ public async Task<ActionResult<OrganizationIntegrationResponseModel>> CreateAsyn
6261
}
6362

6463
var created = await createCommand.CreateAsync(integration);
65-
6664
return Ok(new OrganizationIntegrationResponseModel(created));
65+
6766
}
6867

6968
[HttpPut("{integrationId:guid}")]
70-
public async Task<OrganizationIntegrationResponseModel> UpdateAsync(Guid organizationId, Guid integrationId, [FromBody] OrganizationIntegrationRequestModel model)
69+
public async Task<ActionResult<OrganizationIntegrationResponseModel>> UpdateAsync(Guid organizationId, Guid integrationId, [FromBody] OrganizationIntegrationRequestModel model)
7170
{
7271
if (!await HasPermission(organizationId))
7372
{
74-
throw new NotFoundException();
73+
return NotFound();
7574
}
7675

7776
var integration = model.ToOrganizationIntegration(organizationId);
7877
var updated = await updateCommand.UpdateAsync(organizationId, integrationId, integration);
79-
80-
return new OrganizationIntegrationResponseModel(updated);
78+
return Ok(new OrganizationIntegrationResponseModel(updated));
8179
}
8280

8381
[HttpDelete("{integrationId:guid}")]
84-
public async Task DeleteAsync(Guid organizationId, Guid integrationId)
82+
public async Task<IActionResult> DeleteAsync(Guid organizationId, Guid integrationId)
8583
{
8684
if (!await HasPermission(organizationId))
8785
{
88-
throw new NotFoundException();
86+
return NotFound();
8987
}
9088

9189
await deleteCommand.DeleteAsync(organizationId, integrationId);
90+
return NoContent();
9291
}
9392

9493
[HttpPost("{integrationId:guid}/delete")]
9594
[Obsolete("This endpoint is deprecated. Use DELETE method instead")]
96-
public async Task PostDeleteAsync(Guid organizationId, Guid integrationId)
95+
public async Task<IActionResult> PostDeleteAsync(Guid organizationId, Guid integrationId)
9796
{
98-
await DeleteAsync(organizationId, integrationId);
97+
return await DeleteAsync(organizationId, integrationId);
9998
}
10099

101100
private async Task<bool> HasPermission(Guid organizationId)

src/Core/Dirt/EventIntegrations/OrganizationIntegrationConfigurations/CreateOrganizationIntegrationConfigurationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task<OrganizationIntegrationConfiguration> CreateAsync(
2727
var integration = await integrationRepository.GetByIdAsync(integrationId);
2828
if (integration == null || integration.OrganizationId != organizationId)
2929
{
30-
throw new NotFoundException();
30+
throw new BadRequestException();
3131
}
3232
if (!validator.ValidateConfiguration(integration.Type, configuration))
3333
{

src/Core/Dirt/EventIntegrations/OrganizationIntegrationConfigurations/DeleteOrganizationIntegrationConfigurationCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public async Task DeleteAsync(Guid organizationId, Guid integrationId, Guid conf
2121
var integration = await integrationRepository.GetByIdAsync(integrationId);
2222
if (integration == null || integration.OrganizationId != organizationId)
2323
{
24-
throw new NotFoundException();
24+
throw new BadRequestException("Integration not found for this organization.");
2525
}
2626
var configuration = await configurationRepository.GetByIdAsync(configurationId);
2727
if (configuration is null || configuration.OrganizationIntegrationId != integrationId)
2828
{
29-
throw new NotFoundException();
29+
throw new BadRequestException("Configuration not found for this integration.");
3030
}
3131

3232
await configurationRepository.DeleteAsync(configuration);

src/Core/Dirt/EventIntegrations/OrganizationIntegrationConfigurations/Interfaces/ICreateOrganizationIntegrationConfigurationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface ICreateOrganizationIntegrationConfigurationCommand
1414
/// <param name="integrationId">The unique identifier of the integration.</param>
1515
/// <param name="configuration">The configuration to create.</param>
1616
/// <returns>The created configuration.</returns>
17-
/// <exception cref="Exceptions.NotFoundException">Thrown when the integration does not exist
17+
/// <exception cref="Exceptions.BadRequestException">Thrown when the integration does not exist
1818
/// or does not belong to the specified organization.</exception>
1919
/// <exception cref="Exceptions.BadRequestException">Thrown when the configuration or filters
2020
/// are invalid for the integration type.</exception>

src/Core/Dirt/EventIntegrations/OrganizationIntegrationConfigurations/Interfaces/IUpdateOrganizationIntegrationConfigurationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface IUpdateOrganizationIntegrationConfigurationCommand
1515
/// <param name="configurationId">The unique identifier of the configuration to update.</param>
1616
/// <param name="updatedConfiguration">The updated configuration data.</param>
1717
/// <returns>The updated configuration.</returns>
18-
/// <exception cref="Exceptions.NotFoundException">
18+
/// <exception cref="Exceptions.BadRequestException">Thrown when the integration or the configuration does not exist,
1919
/// Thrown when the integration or the configuration does not exist,
2020
/// or the integration does not belong to the specified organization,
2121
/// or the configuration does not belong to the specified integration.</exception>

src/Core/Dirt/EventIntegrations/OrganizationIntegrationConfigurations/UpdateOrganizationIntegrationConfigurationCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public async Task<OrganizationIntegrationConfiguration> UpdateAsync(
2828
var integration = await integrationRepository.GetByIdAsync(integrationId);
2929
if (integration == null || integration.OrganizationId != organizationId)
3030
{
31-
throw new NotFoundException();
31+
throw new BadRequestException("Integration not found for this organization.");
3232
}
3333
var configuration = await configurationRepository.GetByIdAsync(configurationId);
3434
if (configuration is null || configuration.OrganizationIntegrationId != integrationId)
3535
{
36-
throw new NotFoundException();
36+
throw new BadRequestException("Configuration not found for this integration.");
3737
}
3838
if (!validator.ValidateConfiguration(integration.Type, updatedConfiguration))
3939
{

src/Core/Dirt/EventIntegrations/OrganizationIntegrations/DeleteOrganizationIntegrationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public async Task DeleteAsync(Guid organizationId, Guid integrationId)
2020
var integration = await integrationRepository.GetByIdAsync(integrationId);
2121
if (integration is null || integration.OrganizationId != organizationId)
2222
{
23-
throw new NotFoundException();
23+
throw new BadRequestException("Integration not found for this organization.");
2424
}
2525

2626
await integrationRepository.DeleteAsync(integration);

src/Core/Dirt/EventIntegrations/OrganizationIntegrations/Interfaces/IDeleteOrganizationIntegrationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface IDeleteOrganizationIntegrationCommand
1010
/// </summary>
1111
/// <param name="organizationId">The unique identifier of the organization.</param>
1212
/// <param name="integrationId">The unique identifier of the integration to delete.</param>
13-
/// <exception cref="Exceptions.NotFoundException">Thrown when the integration does not exist
13+
/// <exception cref="Exceptions.BadRequestException">Thrown when the integration does not exist
1414
/// or does not belong to the specified organization.</exception>
1515
Task DeleteAsync(Guid organizationId, Guid integrationId);
1616
}

src/Core/Dirt/EventIntegrations/OrganizationIntegrations/Interfaces/IUpdateOrganizationIntegrationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface IUpdateOrganizationIntegrationCommand
1414
/// <param name="integrationId">The unique identifier of the integration to update.</param>
1515
/// <param name="updatedIntegration">The updated organization integration data.</param>
1616
/// <returns>The updated organization integration.</returns>
17-
/// <exception cref="Exceptions.NotFoundException">Thrown when the integration does not exist,
17+
/// <exception cref="Exceptions.BadRequestException">Thrown when the integration does not exist,
1818
/// does not belong to the specified organization, or the integration type does not match.</exception>
1919
Task<OrganizationIntegration> UpdateAsync(Guid organizationId, Guid integrationId, OrganizationIntegration updatedIntegration);
2020
}

src/Core/Dirt/EventIntegrations/OrganizationIntegrations/UpdateOrganizationIntegrationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task<OrganizationIntegration> UpdateAsync(
2727
integration.OrganizationId != organizationId ||
2828
integration.Type != updatedIntegration.Type)
2929
{
30-
throw new NotFoundException();
30+
throw new BadRequestException("Integration not found for this organization with the specified type.");
3131
}
3232

3333
updatedIntegration.Id = integration.Id;

0 commit comments

Comments
 (0)