Coder Social home page Coder Social logo

Comments (15)

vallamost avatar vallamost commented on September 28, 2024 1

Ahh! Good catch! That was it. As soon as I switched to axios it returned the Unknown Symbol response. Is axios typically preferred over fetch in most cases?

from node-iex-cloud.

JBooker10 avatar JBooker10 commented on September 28, 2024

This error is on me. Since IEX gives a string of 400 on Unknown Symbols, I didn't handle it correctly. I was expecting a json response for 400 level errors. I will be updating this package to accommodate your changes. Give me 15 min and this should be resolve. Thanks for the notice!

@awarmfastbear feel free to let me know if you came across any other bugs

from node-iex-cloud.

JBooker10 avatar JBooker10 commented on September 28, 2024

@awarmfastbear If you do npm i node-iex-cloud you should be on version 2.0.2 error handling should be fixed

from node-iex-cloud.

JBooker10 avatar JBooker10 commented on September 28, 2024

@awarmfastbear Hey notice one small bug with error handling, after bring this to my attention. I've updated node-iex-cloud again to 2.0.3 from 2.0.2. Feel free to pushup any changes also

catching error should be easier now, i edited your code for an example. If the symbol is invalid, it should return the IEX standard error response which is a of string "Symbol Unknown" . Sorry for having you in circles I hope this helps

response = await iex
    .batchSymbols(stocks_to_submit)
    .price()
    .catch(err => console.log(err)

from node-iex-cloud.

vallamost avatar vallamost commented on September 28, 2024

Thanks for your quick response!

I updated to 2.0.3 however I'm not getting any exceptions now and using the example above, the console.log statement does not print anything. Any ideas? Do I need to adjust anything else?

From my server:

15:49:45 ✨ Server listening at http://127.0.0.1:4000
15:49:49 ✨ incoming request GET xxx /batch_stock_prices/?stocks=asdasdfasdf
asdasdfasdf
Response: undefined
15:49:49 ✨ request completed 316ms

Function:

async function getCurrentPriceOfBatchStocks(_stock) {

  const stocks_to_submit = _stock['stocks'];
  console.log(stocks_to_submit)

  const response = await iex
    .batchSymbols(stocks_to_submit)
    .price()
    .catch(err => console.log(test: " + err) ); // < ---- doesn't print anything

  console.log("Response: " + response)

  return new Promise((resolve, reject) => {
    if (response) {
      resolve(response)
    } else {
      reject(response); 
    }
  });
}

from node-iex-cloud.

JBooker10 avatar JBooker10 commented on September 28, 2024

The statement below is missing the then() block. All successful request should be returned in the success block. If it's not a successful request the catch() block should "catch" the errors which should be empty or "Unknown Symbol"

if you add the then() block and pass in the response, this should be good to go

response = await iex
    .batchSymbols(stocks_to_submit)
    .price()
    .then(res => console.log(res))
    .catch(err => console.log(err))

from node-iex-cloud.

vallamost avatar vallamost commented on September 28, 2024

Hmmm, I don't see a difference in behavior, it instead prints out the undefined response from the then. You can see in the output below 'Test1' in the console log output. Seems like it is still getting swallowed somewhere in the iex client.

async function getCurrentPriceOfBatchStocks(_stock) {

  const stocks_to_submit = _stock['stocks'];
  console.log(stocks_to_submit)

  const response = await iex
    .batchSymbols(stocks_to_submit)
    .price()
    .then(res => console.log("Test1: " + res))
    .catch(err => console.log("Test2: " + err))

  console.log("Response: " + response)

  return new Promise((resolve, reject) => {
    if (response) {
      resolve(response)
    } else {
      reject(response); // <-- response is undefined. why?
    }
  });
}

Output:

16:43:44 ✨ incoming request GET xxx /batch_stock_prices/?stocks=asdasdfasdf
asdasdfasdf
Test1: undefined
Response: undefined
16:43:45 ✨ request completed 313ms

from node-iex-cloud.

JBooker10 avatar JBooker10 commented on September 28, 2024

let me know if the above function runs. I'm kinda shooting in the dark here. but essentially you don't need this method getCurrentPriceOfBatchStocks(_stock). I'm not too sure what this function does but this is how you would grab the response using async awaits.

module.exports = async function (fastify, opts) {
  fastify.get(
    "/batch_stock_prices/",
    {
      schema: batchStocksSchema,
    },
    async function (request, reply) {
      try {
        const stocks_to_submit = request.query["stocks"]
       // This is the price response  that you're looking for
        const response = await iex
          .batchSymbols(stocks_to_submit)
          .price();
         // now you have access to the data to log or to send somewhere else
        console.log(response);
        // should log it correctly here
        return reply.code(200).send(response);
      } catch (e) {
        console.log(e);
        return reply.code(400).send("Bad Request, exception: " + e);
      }
    }
  );
};

Also if you need getCurrentPriceOfBatchStocks(_stock) you can write it out as this

async function getCurrentPriceOfBatchStocks(_stock) {
  const stocks_to_submit = _stock["stocks"];
  console.log(stocks_to_submit);
  try {
    let response = await iex.batchSymbols(stocks_to_submit).price();
    return response;
  } catch (error) {
    console.log("Exception: " + error);
    return error;
  }
}

const test = { stocks: "aapl" };

getCurrentPriceOfBatchStocks(test).then(res => console.log(res));

from node-iex-cloud.

vallamost avatar vallamost commented on September 28, 2024

Both functions return data for valid symbols but they do not catch or throw errors for invalid / unknown symbols. They just print undefined for the catch :/. Both functions also look to be returning 200s with an invalid symbol..

I used a separate function so that the web API function can remain operable and stable.

Do these functions catch invalid symbols for you and provide you an exception of invalid symbol?

I saw that you made a test on your recent commit for testing the API which would normally get a single symbol. However, have you tested the batchSymbols API with invalid symbols?

The recent test you made:

test("Unknown Symbol", () => {
  return iex
    .symbol("UnknownSymbol")
    .company()
    .catch(err => expect(err).toMatch("Unknown symbol"));
});

from node-iex-cloud.

JBooker10 avatar JBooker10 commented on September 28, 2024

Yes this should work for both success and errors. You can change the line here to gibberish in the function I've made for getCurrentPriceOfBatchStocks(_stock)
const test = { stocks: "iejrigjri" };

Also both symbol and batchSymbol both share the same request. So these changes should also be reflected in batchSymbol

from node-iex-cloud.

vallamost avatar vallamost commented on September 28, 2024

It doesn't catch the errors for me:

iejrigjri
20:46:46 ✨ Server listening at http://127.0.0.1:4000
undefined

If I change it to aapl it works fine:

aapl
20:47:26 ✨ Server listening at http://127.0.0.1:4000
{ AAPL: { price: 354.8 } }

The function I am using:

async function getCurrentPriceOfBatchStocks(_stock) {
  const stocks_to_submit = _stock["stocks"];
  console.log(stocks_to_submit);
  try {
    let response = await iex.batchSymbols(stocks_to_submit).price();
    return response;
  } catch (error) {
    console.log("Exception: " + error);
    return error;
  }
}

const test = { stocks: "iejrigjri" };

getCurrentPriceOfBatchStocks(test).then(res => console.log(res));

from node-iex-cloud.

JBooker10 avatar JBooker10 commented on September 28, 2024

I think I understand what's going on now. I was using axios as my http client and you are using node-fetch. These libraries handle errors slightly differently. I will fix this bug. But can you try using axios for now to see what happens?

If you have axios it should be coming back as Unkown Symbol. Sorry that's my fault, good find. Will update this to support node-fetch

from node-iex-cloud.

JBooker10 avatar JBooker10 commented on September 28, 2024

Awesome! Axios has much better error handling, but It's really all about preference. I typically use axios on both server and client side

Also I updated the package if you would like to still use node-fetch

from node-iex-cloud.

vallamost avatar vallamost commented on September 28, 2024

Yeah just seeing how it supports timeouts out of the box is a no brainer to switch over to it. Thank you for your help on this!

from node-iex-cloud.

JBooker10 avatar JBooker10 commented on September 28, 2024

yeah that's a good feature too and yeah no problem! If you find any other bugs. Feel free to open up another ticket

from node-iex-cloud.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.