magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

REST API "The product can't be saved"

Open Zyles opened this issue 5 years ago • 16 comments

Another case of bad error message feedback.

No logs other than the REST API giving terrible error message and a whole bunch of Googling has to be done.

In my case I tried to upload an image through the rest API using POST to /rest/V1/products/<sku>/media

The issue has to do with products having required fields that are not filled in. This could arise after changing attribute sets and not going back to fix it on old products for example.

The solution is to go into admin, filling in the required fields for the product, then trying to upload the image again.

Preconditions (*)

  1. Magento 2.3.3

Steps to reproduce (*)

  1. Have required fields on product that are not filled in
  2. Try to upload image using REST API

Expected result (*)

  1. Better error message that is actually helpful.
  2. Log output in var/log

Actual result (*)

  1. Terrible error message.
  2. No logs
  3. Not being able to upload the product image

Zyles avatar Dec 15 '19 10:12 Zyles

Hi @Zyles. Thank you for your report. To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

@Zyles do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

m2-assistant[bot] avatar Dec 15 '19 10:12 m2-assistant[bot]

Hi @engcom-Delta. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Dec 19 '19 09:12 m2-assistant[bot]

@Zyles , thank you for your report. Looks like it would be good solution to add output with stack trace in var/log. For now response is "message": "The product can't be saved." Marked as Future Request cc @sdzhepa

engcom-Delta avatar Dec 19 '19 11:12 engcom-Delta

Related files.

vendor/magento/module-catalog/Model/Product/Gallery/GalleryManagement.php
79:            throw new StateException(__("The product can't be saved."));
129:            throw new StateException(__("The product can't be saved."));

vendor/magento/module-catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php
85:     * @expectedExceptionMessage The product can't be saved.
160:     * @expectedExceptionMessage The product can't be saved.

Change to include exception as 2nd argument to new StateException so that it is actually helpful.

 throw new StateException(__("The product can't be saved 1."), $e);

 throw new StateException(__("The product can't be saved 2."), $exception);

Also can throw for other reasons such as "The image MIME type is not valid or not supported." (Corrupted image or wrong extension/mimetype?)

LiamKarlMitchell avatar Jul 29 '20 05:07 LiamKarlMitchell

try {
            $product = $this->productRepository->save($product);
        } catch (InputException $inputException) {
            throw $inputException;
        } catch (\Exception $e) {
            throw new StateException(__('Cannot save product. [%1]', $e->getMessage()));
        }

Responds with

{
    "message": "Cannot save product. [%1]",
    "parameters": [
        "The image does not exist."
    ]
}

So then the client using the API has to put the parameters into the message.

Why "The image does not exist." I want to upload an image. (Magento 2.2.0) The image is base64 encoded.

Another inner exception I got when I changed the filename was "Unable to save product"

In this case that I ran into, When saving from Magento admin turns out permissions are denied on the cache folder...

Unable to write file into directory /var/www/html/pub/media/catalog/product/cache/7690288cef886e3600a2dbabc56d79c1/t/e. Access forbidden.

Ownership of the directory was wrong after migrating with rsyncing the folder to another server.

It does look like var/log/exception.log would have told me this if I had looked, so long as the critical/warning inner exceptions get logged I suppose that is fine, but the wording on the error could indicate a bit more as to why. E.g. "Permission error check exception log for more details." main.CRITICAL: Warning: rename(/var/www/html/pub/media/tmp/catalog/product/T/1/Test-1.jpg): failed to open stream: Permission denied in /var/www/html/vendor/magento/framework/File/Uploader.php on line 283

LiamKarlMitchell avatar Sep 09 '20 12:09 LiamKarlMitchell

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 14 days if no further activity occurs. Is this issue still relevant? If so, what is blocking it? Is there anything you can do to help move it forward? Thank you for your contributions!

stale[bot] avatar Jan 21 '21 14:01 stale[bot]

Why has this gone stale? This is a very annoying bump I'm also hitting. My import worked fine 5 minutes ago, added a custom_attribute to determine the url_key because I'm importing products with same names. And boom an error which I can't debug because all it tells me is: "Product is unable to be saved"...

mariusjp avatar Jan 27 '21 09:01 mariusjp

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 14 days if no further activity occurs. Is this issue still relevant? If so, what is blocking it? Is there anything you can do to help move it forward? Thank you for your contributions!

stale[bot] avatar Apr 13 '21 09:04 stale[bot]

Posting a comment. Issues should not be automatically closed.

dverkade avatar Apr 18 '21 20:04 dverkade

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 28 days if no further activity occurs. Is this issue still relevant? If so, what is blocking it? Is there anything you can do to help move it forward? Thank you for your contributions!

stale[bot] avatar Sep 18 '21 23:09 stale[bot]

I'm now getting the very same issue on a new Magento 2.4.3 instance.

TysvdHeuvel avatar Oct 20 '21 10:10 TysvdHeuvel

Probably unrelated but ran into yesterday. On a 2.3.0 had a csv import that created a different category with the same name as another category. So the product was in "Promo" category twice two different id. Saving the product in admin caused timeout/error. Wonder if a similar issue.

Also seemed to have an issue adding product images with rest api when image name had a . in it. Easy to replace it and specify cleaned name, but the names match the SKU's of products.

LiamKarlMitchell avatar Oct 21 '21 19:10 LiamKarlMitchell

Any update to the issue? We are having the issue as well.

LudwigDev9 avatar May 09 '22 08:05 LudwigDev9

hello. the same issue occurred just yesterday. while tests where running against Magento v2.4.4. i get the same error ....

here my docker-compose script

version: '2' services: mariadb: image: docker.io/bitnami/mariadb:10.3 environment: # ALLOW_EMPTY_PASSWORD is recommended only for development. - ALLOW_EMPTY_PASSWORD=yes - MARIADB_USER=bn_magento - MARIADB_DATABASE=bitnami_magento volumes: - 'mariadb_data:/bitnami/mariadb' magento: image: docker.io/bitnami/magento:2.4.4 ports: - '80:8080' - '443:8443' environment: - MAGENTO_HOST=localhost - MAGENTO_DATABASE_HOST=mariadb - MAGENTO_DATABASE_PORT_NUMBER=3306 - MAGENTO_DATABASE_USER=bn_magento - MAGENTO_DATABASE_NAME=bitnami_magento - ELASTICSEARCH_HOST=elasticsearch - ELASTICSEARCH_PORT_NUMBER=9200 # ALLOW_EMPTY_PASSWORD is recommended only for development. - ALLOW_EMPTY_PASSWORD=yes volumes: - 'magento_data:/bitnami/magento' depends_on: - mariadb - elasticsearch elasticsearch: image: docker.io/bitnami/elasticsearch:7.16.3 volumes: - 'elasticsearch_data:/bitnami/elasticsearch/data' volumes: mariadb_data: driver: local magento_data: driver: local elasticsearch_data: driver: local

stereoscope avatar May 11 '22 10:05 stereoscope

Bitnami has a php/tmp write issue on their images. Add the exception printing as mentioned above and make sure the tmp folder is writable.

Related files.

vendor/magento/module-catalog/Model/Product/Gallery/GalleryManagement.php
79:            throw new StateException(__("The product can't be saved."));
129:            throw new StateException(__("The product can't be saved."));

vendor/magento/module-catalog/Test/Unit/Model/Product/Gallery/GalleryManagementTest.php
85:     * @expectedExceptionMessage The product can't be saved.
160:     * @expectedExceptionMessage The product can't be saved.

Change to include exception as 2nd argument to new StateException so that it is actually helpful.

 throw new StateException(__("The product can't be saved 1."), $e);

 throw new StateException(__("The product can't be saved 2."), $exception);

Also can throw for other reasons such as "The image MIME type is not valid or not supported." (Corrupted image or wrong extension/mimetype?)

LudwigDev9 avatar May 11 '22 13:05 LudwigDev9

Thanks Ludwig! 2 hours of research.. but thanks to this hint I managed to solve my problem. How did you find out? My logs don't show anything that points in this direction.

TysvdHeuvel avatar Sep 21 '22 20:09 TysvdHeuvel

I have same problem problem on update, Magento\Catalog\Model\Product\Gallery\GalleryManagement->update('42489', Object(Magento\Catalog\Model\Product\... (1988 bytes)], Magento 2.4.3 Some times this error occurring, other times I having sucess in request with same body someone now anything about?

ViniciusKoiti avatar Feb 20 '23 18:02 ViniciusKoiti

How I try to send images, I give that sometimes, but other times I gave success in same object

ViniciusKoiti avatar Feb 28 '23 19:02 ViniciusKoiti

If you're using a Bitnami docker image, make sure /opt/bitnami/php/tmp is writeable by PHP

Run this inside the magento container terminal:

chmod 777 /opt/bitnami/php/tmp

Doing this solved the "The product can't be saved" for me

yhling avatar Jun 09 '23 17:06 yhling

I am doing a batch image import (one by one) and for me, it seems to be the JPG extension. As far as I can see, my PNG files all seem to be working, but the JPG give errors. All of them are uploaded in Base64. I'll check and comment back if I manage to find a solution!

woutertje99 avatar Aug 02 '23 11:08 woutertje99

I am doing a batch image import (one by one) and for me, it seems to be the JPG extension. As far as I can see, my PNG files all seem to be working, but the JPG give errors. All of them are uploaded in Base64. I'll check and comment back if I manage to find a solution!

Nope, that is not the issue. Second guess is corrupted Base64, or missing data in the import. Magento is such a pain in the ass to work with, no error logs, no support given anymore, not the best documentation...

woutertje99 avatar Aug 02 '23 13:08 woutertje99