Skip to content

Get concrete error message of CouldNotSaveException

We need to get concrete error message when MagentoCatalogModelProductRepository throws CouldNotSaveException during saveProduct()

private function saveProduct($product): void
{
    try {
        $this->removeProductFromLocalCacheBySku($product->getSku());
        $this->removeProductFromLocalCacheById($product->getId());
        $this->resourceModel->save($product);
    } catch (ConnectionException $exception) {
...
    } catch (Exception $e) {
        throw new CouldNotSaveException(
            __('The product was unable to be saved. Please try again.'),
            $e
        );
    }
}

An easy way would be just adding the message in that code

__('The product was unable to be saved. Please try again.' . $e->getMessage())

But we can’t edit that file, as this is a project hosted in Adobe Cloud. So, how can we get that message instead of application default message?