Skip to content

Commit

Permalink
fix(api): Unhandled exceptions fix (#592)
Browse files Browse the repository at this point in the history
* added catch for invalid operation exception to controllers where this is throwable.

* removed unnecessary comment

* revert chages to utillitiesController
  • Loading branch information
BouVid authored Sep 28, 2023
1 parent 972bbe2 commit e30d277
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 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 @@ -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 @@ -57,14 +59,16 @@ 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}).");
throw 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 +121,7 @@ public class ExcelHeader
/// </summary>
public int ColIndex { get; set; }
}

public class ExcelDataRow
{
/// <summary>
Expand Down Expand Up @@ -146,10 +151,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 e30d277

Please sign in to comment.