Problems with AddProduct method in Javacript SDK

#1

Hello,

I’m working with the Javascript SDK. I’m downloading the file from unpkg.com and it appears it is giving me 3.19.0.

I need to get error information from the AddProduct method. For example:

Moltin.Cart().AddProduct( item_id, ‘15’)
.then( function(info) { console.log(info)});

If I add 15 items to the cart and only 10 are available the API responds with a 400 status and an error message in the response body. However, the function call doesn’t seem to handle the response. I would expect the method to return something, but it doesn’t seem to. Does the response function have a way of handling a response other that a 200 status?

Please advise.

0 Likes

#2

Hey Martins,

Sorry for the delay. There is no error handling in the SDK. You will want to handle that with your own logic.

Thanks,
George

0 Likes

#3

Hi @mzinbergs

The SDK will throw an error that you will want to catch in your code.

Moltin
  .Cart()
  .AddProduct( item_id, '15')
  .then(function(info) { console.log(info)})
  .catch(err => console.log(err) // <--- Act on your error

I hope this helps :smile:

2 Likes

#4

Thank you. The method returns a nice message in the body of the response. In the example it would be:
{“errors”:[{“status”:400,“title”:“Insufficient stock”,“detail”:“There is not enough stock to add xxx-item to your cart”}]}
Is it possible to access that through the SDK?

Thanks for your time,
Martins.

0 Likes

#5

Hey

I’m confused. That’s the error that’s being returned from the API via the SDK.

You should handle the error on the frontend how you see best.

Regards
Jamie

0 Likes

#6

Jamie,
Using your code example, the console only logs { status: 400} Is that by design? Or can I get the entire message ?

Martins.

0 Likes

#7

Are you able to provide an example of the code on CodeSandbox that I could run to see the error you’re running into?

Any errors returned from the API are sent back to the SDK.

0 Likes

#8

Jamie,
I’m interested in capturing a detailed error message from the Javascript SDK. If I run:

Moltin .Cart()
.AddProduct( item_id, ‘15’)
.then(function(info) { console.log(info)})
.catch(err => console.log(err));

and only 10 items are available the SDK will throw an error and the end result is that the console will log {status:400}.

However, the API call returns a nice message in the body of the response viz. {“errors”:[{“status”:400,“title”:“Insufficient stock”,“detail”:“There is not enough stock to add xxx-item to your cart”}]}

That information is lost. The API returns a nice message, but the SDK doesn’t forward it.

Martins.

0 Likes

#9

Ah! My apologies, let me dive into this a bit further. I’ve created a placeholder issue over on GitHub where we can track the issue.

0 Likes