MONEI / Shopify-api-node

Node Shopify connector sponsored by MONEI
https://monei.com/shopify-payment-gateway/
MIT License
946 stars 278 forks source link

HTTP status code error on Article creation #469

Closed f1rstsurf closed 3 years ago

f1rstsurf commented 3 years ago

Hi,

I'm facing a strange issue while creating a new article for my blog :

          triggerUncaughtException(err, true /* fromPromise */);
          ^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: {
  id: HIDDEN,
  title: 'test',
  created_at: '2021-03-19T14:29:20+01:00',
  body_html: 'kdjhlsqkjD',
  blog_id: HIDDEN,
  author: 'postArticleAuthor',
  user_id: null,
  published_at: null,
  updated_at: '2021-03-19T14:29:20+01:00',
  summary_html: null,
  template_suffix: null,
  handle: 'test-3',
  tags: 'postArticleTags',
  admin_graphql_api_id: 'gid://shopify/OnlineStoreArticle/555489460270'
}
    at new NodeError (node:internal/errors:329:5)
    at ServerResponse.writeHead (node:_http_server:279:11)
    at ServerResponse._implicitHeader (node:_http_server:270:8)
    at write_ (node:_http_outgoing:746:9)
    at ServerResponse.end (node:_http_outgoing:835:5)
    at ServerResponse.send (/home/adrien/Documents/dropshapp/node_modules/express/lib/response.js:221:10)
    at /home/adrien/Documents/dropshapp/main.js:339:11
    at /home/adrien/Documents/dropshapp/constructors/createArticle.js:18:15
    at processTicksAndRejections (node:internal/process/task_queues:94:5) {
  code: 'ERR_HTTP_INVALID_STATUS_CODE'
}

This issue occurs when I'm returning the datas into the callback :

  createArticle: async function(postApiKey, postApiPass, postApiSite, postArticleContent, postBlogName, callback) {
    if (postApiKey && postApiPass && postApiSite) {
      //Check in database if the values exist
      db.checkIds(postApiKey, postApiPass, postApiSite, async function(error, data) {
        if (error) {
          //If an issue occured while querrying the DB let's display it
          callback(error, null);
        } else {
            if (data.length > 0) {
              //let's retrieve the blog id according to the blog BlogName
              const blogInfos = await shopify.fetchBlogInfos(postBlogName);
              const blogId = blogInfos[0].id;
              //Let's create the new article with the BlogId
              const datas = await shopify.createArticle(postArticleContent,blogId);
              callback(null, datas);  **// The error fires here**
          } else {
            //If the ids don't exist, let's display it too
            callback('Error : The provided credentials mismatch', null);
          }
        }
      });
    } else {
      callback('Error : No credentials have been defined', null);
    }
  }
};
    try {
      let params = {
        title: "test",
        author:"postArticleAuthor",
        tags:"postArticleTags",
        body_html: postArticleContent,
        published :"false"
      };
      const blogDatas = await shopify().article.create(blogId,params);
      console.log(blogDatas); **//ScreenShot results**
      return blogDatas;
    } catch (err) {
      // handle err
      return err;
    }
  },

What is pretty weird, is that the article is successfully created, and the promise does return correct data (See attached screen).

Also, I'm using the latest Webhook API version (2021-01).

Do you have any idea ?

Screenshot 2021-03-19 at 15 17 17
lpinca commented 3 years ago

It does not seem to be an issue in shopify-api-node. The error is thrown when you write the HTTP response from express. It seems that the article data is used as the status code for HTTP response instead of the body. You should investigate why this happens. Are you calling the express response.send() method with two arguments?

f1rstsurf commented 3 years ago

Hi, thank you for your help, here is my http response code handling :

app.post('/createArticle', (req, res) => {
  shopify.createArticle(req.body.postApiKey, req.body.postApiPass, req.body.postApiSite,req.body.postArticleContent, req.body.postBlogName, function(error, data) {
    if (error) {
      //If an issue occured let's display it
      console.log(error);
      res.end(error, null);
    } else {
      //console.log(data);
      res.sendStatus(data);
    }
  });
});

I've got about 30 function with the same structure (createproduct, getCollections ...etc), and it's the first time I'm facing this issue. I'll take a closer look to my code, as it does not seems to be related with shopify-api-node.

Thank you

lpinca commented 3 years ago

res.sendStatus(data);

Check that data here is a valid status code and not the article data.

f1rstsurf commented 3 years ago

I just rechecked today, everything is working well now, maybe a Shopify side issue ?