Enhance error handling in productsRoute for customer retrieval

- Added exception handling with logging when fetching customer by ID fails.
- Improved safety checks to ensure customer existence before returning the object.
- Return `null` in case of failure to prevent unhandled errors.
This commit is contained in:
Jeppe Bundgaard
2025-11-10 16:59:41 +01:00
parent a519478788
commit a6d427e3d3
+11 -1
View File
@@ -22,7 +22,17 @@ class productsRoute
global $response;
if (self::isParametersSet(['customer_id'])) {
$customerId = (int)self::getParameter('customer_id');
return (new users_o())->getUserByCustomerNumber((int)$customerId);
try {
$customerObject = (new users_o())->getUserByCustomerNumber((int)$customerId);
if ($customerObject->exists()) {
return $customerObject;
}
} catch (\Exception $e) {
// Log the incident
(new logs_o())->add('products', 'global', 3, 0, 'GET_CUSTOMER_FAILED', 'Failed to get customer with id ' . $customerId . '. Error: ' . $e->getMessage());
// Return null
return null;
}
}
return null;
}