Location error handling
Deploy Promiscuity Auth API / deploy (push) Successful in 45s
Deploy Promiscuity Character API / deploy (push) Successful in 45s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 1m0s
k8s smoke test / test (push) Successful in 8s
Deploy Promiscuity Auth API / deploy (push) Successful in 45s
Deploy Promiscuity Character API / deploy (push) Successful in 45s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 1m0s
k8s smoke test / test (push) Successful in 8s
This commit is contained in:
@@ -17,12 +17,14 @@ public class LocationsController : ControllerBase
|
||||
private readonly LocationStore _locations;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<LocationsController> _logger;
|
||||
|
||||
public LocationsController(LocationStore locations, IHttpClientFactory httpClientFactory, IConfiguration configuration)
|
||||
public LocationsController(LocationStore locations, IHttpClientFactory httpClientFactory, IConfiguration configuration, ILogger<LocationsController> logger)
|
||||
{
|
||||
_locations = locations;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
@@ -140,26 +142,56 @@ public class LocationsController : ControllerBase
|
||||
if (!string.IsNullOrWhiteSpace(token))
|
||||
request.Headers.Authorization = AuthenticationHeaderValue.Parse(token);
|
||||
|
||||
using var response = await client.SendAsync(request);
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
if (!response.IsSuccessStatusCode)
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
response = await client.SendAsync(request);
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
if (interact.PreviousObject is not null)
|
||||
await _locations.RestoreObjectInteractionAsync(id, interact.PreviousObject);
|
||||
return StatusCode((int)response.StatusCode, responseBody);
|
||||
|
||||
_logger.LogError(
|
||||
ex,
|
||||
"Failed to reach InventoryApi while processing location interaction for location {LocationId}, character {CharacterId}, object {ObjectId}",
|
||||
id,
|
||||
req.CharacterId,
|
||||
req.ObjectId
|
||||
);
|
||||
|
||||
return StatusCode(StatusCodes.Status502BadGateway, new
|
||||
{
|
||||
type = "https://httpstatuses.com/502",
|
||||
title = "Bad Gateway",
|
||||
status = 502,
|
||||
detail = $"Failed to reach InventoryApi at {inventoryBaseUrl}.",
|
||||
traceId = HttpContext.TraceIdentifier
|
||||
});
|
||||
}
|
||||
|
||||
return Ok(new InteractLocationObjectResponse
|
||||
using (response)
|
||||
{
|
||||
LocationId = id,
|
||||
CharacterId = req.CharacterId,
|
||||
ObjectId = interact.ObjectId,
|
||||
ObjectType = interact.ObjectType,
|
||||
ItemKey = interact.ItemKey,
|
||||
QuantityGranted = interact.QuantityGranted,
|
||||
RemainingQuantity = interact.RemainingQuantity,
|
||||
Consumed = interact.Consumed,
|
||||
InventoryResponseJson = responseBody
|
||||
});
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
if (interact.PreviousObject is not null)
|
||||
await _locations.RestoreObjectInteractionAsync(id, interact.PreviousObject);
|
||||
return StatusCode((int)response.StatusCode, responseBody);
|
||||
}
|
||||
|
||||
return Ok(new InteractLocationObjectResponse
|
||||
{
|
||||
LocationId = id,
|
||||
CharacterId = req.CharacterId,
|
||||
ObjectId = interact.ObjectId,
|
||||
ObjectType = interact.ObjectType,
|
||||
ItemKey = interact.ItemKey,
|
||||
QuantityGranted = interact.QuantityGranted,
|
||||
RemainingQuantity = interact.RemainingQuantity,
|
||||
Consumed = interact.Consumed,
|
||||
InventoryResponseJson = responseBody
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user