GithubHelp home page GithubHelp logo

Comments (6)

micahjon avatar micahjon commented on September 4, 2024 2

Today I ran into another case where this happened, not b/c of an invalid URL but simply due to a bad response from the iTunes API:

<html><head><title>Error</title></head><body>Your request produced an error.  <BR>[newNullResponse]</body></html>

Trying the request again worked fine. It appears that once in a great while the iTunes API doesn't work, which is to be expected from any real-world API I suppose. It could be a rate limiting feature (happened when I was making a bunch of requests to autocomplete some search results), but I'm not sure.

Regardless, I submitted a PR that handles invalid JSON from the iTunes API. By decoding the response with phar instead of manually, the Promise is rejected instead of a fatal error being thrown that aborts the Node process. In my case, it's the difference between my Fastify server's Node process being shut down and it being able to continue to handle requests.
#18

from node-itunes-search.

micahjon avatar micahjon commented on September 4, 2024 1

This happens if the iTunes URL gets screwed up, for instance if you pass an ItunesEntityPodcast object instead of a podcast string as the entity (as I mistakenly did)

import {
  searchItunes,
  ItunesSearchOptions,
  ItunesEntityPodcast,
} from 'node-itunes-search';

async function search() {
  const results = await searchItunes(
      new ItunesSearchOptions({
        term: 'this american life',
        entity: ItunesEntityPodcast, // Should be a "podcast" string, not an object
        limit: 10,
      })
  );
}

In the above case, the requested URL is invalid:
https://itunes.apple.com/search?term=revisionist&entity=[object Object]&limit=1

If you paste this URL into your browser you'll see a JSON error message in response--I think b/c the browser is url-encoding the square brackets. If you try it with curl it'll give you an error message, e.g.

$ curl "https://itunes.apple.com/search?term=revisionist&entity=[object Object]&limit=1"
curl: (3) bad range in URL position 58:
https://itunes.apple.com/search?term=revisionist&entity=[object Object]&limit=1
                                                         ^

Likewise, phin balks at this and will populate res in search.js with a buffer instead of a string. I tried converting the buffer to a string, but when I do this I get back a bunch of strange characters, not the JSON I'd expect.

I think the best solution would be ensuring the URL is valid before making the request, such as by using the URL() constructor.

from node-itunes-search.

micahjon avatar micahjon commented on September 4, 2024 1

Just for reference, here's the exact error I ran into:

undefined:1
<html><head><title>Error</title></head><body>Your request produced an error.  <BR>[newNullResponse]</body></html>
^

SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at /Users/micah/Sites/micahs-project/backend/node_modules/node-itunes-search/build/search/search.js:16:33
    at IncomingMessage.<anonymous> (/Users/micah/Sites/micahs-project/backend/node_modules/phin/lib/phin.compiled.js:1:2100)
    at IncomingMessage.emit (node:events:406:35)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

from node-itunes-search.

jacob-shuman avatar jacob-shuman commented on September 4, 2024 1

Just published the new release 😄! I really appreciate the help @micahjon!

from node-itunes-search.

jacob-shuman avatar jacob-shuman commented on September 4, 2024

I can't seem to reproduce this, do you recall the structure of the request?

from node-itunes-search.

micahjon avatar micahjon commented on September 4, 2024

Hey @jacob-shuman, thanks for merging #18 . Looks like we just need a new npm release and then we can close this issue.

from node-itunes-search.

Related Issues (4)

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.