fix: Handle null response in SaleController index method

- Add try-catch block to handle null response from API
- Log errors and return appropriate HTTP responses
- Update error message in 503.blade.php to show exception message if available
This commit is contained in:
Rico van Zelst
2024-06-18 12:31:21 +02:00
parent f01703830d
commit 68dd3dd2f8
2 changed files with 26 additions and 8 deletions

View File

@@ -9,15 +9,33 @@ class SaleController extends Controller
{
public function index()
{
$sales = Cache::remember('sales_data', 60 * 60 * 8, static function () {
$lmi_api_key = config('services.lmi.api_key');
try {
$sales = Cache::remember('sales_data', 60 * 60 * 8, static function () {
$lmi_api_key = config('services.lmi.api_key');
$response = Http::withHeaders([
'Authorization' => 'Bearer '.$lmi_api_key,
])->get('https://lmi.orianna.dev/api/lol-sales');
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $lmi_api_key,
])->get('https://lmi.orianna.dev/api/lol-sales');
$response = $response->json();
if ($response['champion_sales'] === null) {
logger()->error('LMI has broken');
return abort(503, 'Trying to access array offset on value of type null');
}
return $response;
});
} catch (\Exception $e) {
if ($e->getMessage() === 'Trying to access array offset on value of type null') {
logger()->error('LMI has broken');
abort(503, 'Sorry, the Sale Rotation is currently under maintenance. Please try again later.');
} else {
logger()->error('An error occurred while trying to fetch the Sale Rotation', ['error' => $e->getMessage()]);
abort(500, 'Sorry, an error occurred while trying to fetch the Sale Rotation. Please try again later.');
}
}
return $response->json();
});
return view('sales.index', ['sales' => $sales]);
}