I am working on a java backend that is accessible via rest api. For example there is a resource to create and update a product. Depending on the user the products can have different properties which i handled in a dynamic way using a hashmap (so which products has which properties are defined somewhere else). Properties could be name, source, supplier, amount, etc.
Sample:
public class Product
int id;
Set<ProductProperty>;
The products should be updated via REST API with sending a JSON in the body:
{productId: 1
supplier: XY
amount: 23
productCode: ZZ}
The problem I have is that a specific property could be deleted for a product. Of course all other properties should be updated accordingly. And for the failed properties i would like to return the error to the requester. So my idea is to return a 200 respone which has a JSON object in the response body like:
{[errors:
{
"productCode": "ZZ",
"errorText": "Code lenght is less than 12 digits",
},
{
"foobarProperty": "123124124",
"errorText": "Property doest not exist for this product",
}
]}
With that i could show the errors in the UI (will be setup with react later) so the user knows which properties could not be updated and why. So i wanted to know if this is a good approach or if there are best practice i dont know yet.
If i think more, lets say the product should be also returned would it be ok to set the product as a json including the error part like:
{
productId: 1
supplier: XY
amount: 23
productCode: YY}, // has the updated prop
},
{
[errors:
{
"productCode": "ZZ",
"errorText": "Code lenght is less than 12 digits",
},
{
"foobarProperty": "123124124",
"errorText": "Property doest not exist for this product",
}
]}
question from:
https://stackoverflow.com/questions/66052692/http-request-always-add-error-and-warning-in-response-body 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…