Skip to content

Commit

Permalink
added catch for invalid operation exception to controllers where this…
Browse files Browse the repository at this point in the history
… is throwable.
  • Loading branch information
BouVid committed Sep 26, 2023
1 parent 9b016d8 commit e6d2358
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public async Task<ActionResult<ApiResourceAllocationRequest>> CreateProjectAlloc

return Created($"/projects/{projectIdentifier}/requests/{newRequest!.RequestId}", new ApiResourceAllocationRequest(newRequest));
}
catch (InvalidOperationException iv)
{
return ApiErrors.InvalidOperation(iv);
}
catch (ValidationException ex)
{
return ApiErrors.InvalidOperation(ex);
Expand Down Expand Up @@ -193,6 +197,10 @@ public async Task<ActionResult<ApiResourceAllocationRequest>> CreateProjectAlloc
// Using the requests for position endpoint as created ref.. This is not completely accurate as it could return more than those created. Best option though.
return Created($"/projects/{projectIdentifier}/positions/{request.OrgPositionId}/requests", requests.Select(x => new ApiResourceAllocationRequest(x)).ToList());
}
catch (InvalidOperationException iv)
{
return ApiErrors.InvalidOperation(iv);
}
catch (ValidationException ex)
{
return ApiErrors.InvalidOperation(ex);
Expand Down Expand Up @@ -287,6 +295,10 @@ public async Task<ActionResult<ApiResourceAllocationRequest>> CreateResourceOwne

return Created($"/departments/{departmentPath}/resources/requests/{newRequest!.RequestId}", new ApiResourceAllocationRequest(newRequest));
}
catch (InvalidOperationException iv)
{
return ApiErrors.InvalidOperation(iv);
}
catch (ValidationException ex)
{
return ApiErrors.InvalidOperation(ex);
Expand Down Expand Up @@ -363,6 +375,10 @@ public async Task<ActionResult<ApiResourceAllocationRequest>> PatchInternalReque

return new ApiResourceAllocationRequest(updatedRequest!);
}
catch (InvalidOperationException iv)
{
return ApiErrors.InvalidOperation(iv);
}
catch (ValidationException ve)
{
return ApiErrors.InvalidOperation(ve);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Fusion.Resources.Api.Controllers.Requests
[ApiController]
public class SecondOpinionController : ResourceControllerBase
{
// projects/d5c79bb2-5135-47f0-81f2-739ebe46f752/requests/32d3a26c-a13c-473b-889e-beb8d0a97992
[HttpOptions("/resources/requests/internal/{requestId}/second-opinions")]
public async Task<IActionResult> CheckSecondOpinionAccess(Guid requestId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ public class UtilitiesController : ResourceControllerBase
private readonly IFusionTokenProvider tokenProvider;
private readonly IOptions<FusionIntegrationOptions> fusionOptions;

public UtilitiesController(IHttpClientFactory httpClientFactory, IFusionTokenProvider tokenProvider, IOptions<FusionIntegrationOptions> fusionOptions)
public UtilitiesController(IHttpClientFactory httpClientFactory, IFusionTokenProvider tokenProvider,
IOptions<FusionIntegrationOptions> fusionOptions)
{
this.httpClientFactory = httpClientFactory;
this.tokenProvider = tokenProvider;
this.fusionOptions = fusionOptions;
}

[HttpPost("/utilities/parse-spreadsheet")]
public async Task<ActionResult<ExcelConversion>> ValidateContractorImportSpreadsheet([FromForm] ConvertSpreadsheetRequest request)
public async Task<ActionResult<ExcelConversion>> ValidateContractorImportSpreadsheet(
[FromForm] ConvertSpreadsheetRequest request)
{
if (request == null)
return FusionApiError.InvalidOperation("MissingBody", "Could not locate any body payload");
Expand All @@ -43,7 +45,8 @@ public async Task<ActionResult<ExcelConversion>> ValidateContractorImportSpreads


if (string.IsNullOrEmpty(url))
throw new InvalidOperationException("Missing configuration for fusion utility function");
return ApiErrors.InvalidOperation(
new InvalidOperationException("Missing configuration for fusion utility function"));


using var streamContent = new StreamContent(request.File!.OpenReadStream());
Expand All @@ -57,14 +60,17 @@ public async Task<ActionResult<ExcelConversion>> ValidateContractorImportSpreads
if (response.IsSuccessStatusCode)
return JsonConvert.DeserializeObject<ExcelConversion>(content)!;

throw new InvalidOperationException($"Parser function returned non-successfull response ({response.StatusCode}).");
return ApiErrors.InvalidOperation(
new InvalidOperationException(
$"Parser function returned non-successfull response ({response.StatusCode})."));
}

[HttpGet("/utilities/templates/import-personnel")]
public async Task<FileResult> DownloadImportPersonnelTemplate()
{
const string fileName = "fusion personnel import.xlsx";
using var templateFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Fusion.Resources.Api.Data.personnel-import-template.xlsx");
using var templateFile = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("Fusion.Resources.Api.Data.personnel-import-template.xlsx");
using var memoryStream = new MemoryStream();

if (templateFile == null)
Expand Down Expand Up @@ -117,6 +123,7 @@ public class ExcelHeader
/// </summary>
public int ColIndex { get; set; }
}

public class ExcelDataRow
{
/// <summary>
Expand Down Expand Up @@ -146,10 +153,14 @@ public class ExcelParserMessage
/// </summary>
public string Cell { get; set; } = null!;

public enum ExcelParserMessageLevel { Information, Warning, Error }

public enum ExcelParserMessageLevel
{
Information,
Warning,
Error
}
}

#endregion
}
}
}

0 comments on commit e6d2358

Please sign in to comment.