magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.48k stars 9.29k forks source link

REST API "The product can't be saved" #26053

Open Zyles opened 4 years ago

Zyles commented 4 years ago

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
m2-assistant[bot] commented 4 years ago

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

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?


m2-assistant[bot] commented 4 years ago

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:

engcom-Delta commented 4 years ago

@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

LiamKarlMitchell commented 4 years ago

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 commented 4 years ago
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

stale[bot] commented 3 years ago

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!

mariusjp commented 3 years ago

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"...

stale[bot] commented 3 years ago

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!

dverkade commented 3 years ago

Posting a comment. Issues should not be automatically closed.

stale[bot] commented 3 years ago

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!

TysvdHeuvel commented 2 years ago

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

LiamKarlMitchell commented 2 years ago

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.

LudwigDev9 commented 2 years ago

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

stereoscope commented 2 years ago

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:

LudwigDev9 commented 2 years ago

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?)

TysvdHeuvel commented 2 years ago

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.

ViniciusKoiti commented 1 year ago

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 commented 1 year ago

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

yhling commented 1 year ago

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

woutertje99 commented 1 year ago

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 commented 1 year ago

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...