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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user