-1

What am I doing wrong in this GET method?

I'm making a GET request from an external API in my API controller.

but when performing the request, I get this error in my terminal:

 node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "AxiosError: Request failed with status code 404".] {   
  code: 'ERR_UNHANDLED_REJECTION'

That's the code:

    static async getCentroMontagemByLoc(req, res) {
    const { cep, raio } = req.query;

    const response = await axios
      //pdr?cep=89245000&raio=40000
      .get(`${apiCentroMontagem}/pdr?cep=${cep}&raio=${raio}`, {
        httpsAgent,
      }).catch(err => res.send(err))

    res.status(200).json(response.data);
  }

EDIT(return the same error):

try {
      await axios.get(`${apiCentroMontagem}/pdr?cep=${cep}&raio=${raio}`, {
        httpsAgent,
      }).then(response => response.data)
    } catch (err) {
      console.log(err);
    } 

This is the url I'm making the request:

http://localhost:8080/centro-de-montagem/position?cep=joinville&raio=4000

this is an error returned in the browser when trying to access this function:

[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.

however, when making the request in the source API without going through the controller, it returns the request data ( it's not a 404 status, after all there is content on this endpoint )

What am I doing wrong in this GET method?

  • you're doing your `catch` wrong the error you see is saying that you have an error which is not handled because you doint have a catch statement, use `trycatch` like `try {axios.get(....)} catch(err) {console.log(err)}` and see the specific error – Mohamed Oraby Sep 22 '22 at 12:44
  • It keeps returning me the same error present in this post ;( – Vito Sandrin Sep 22 '22 at 12:51
  • can you update the code in the post? – Mohamed Oraby Sep 22 '22 at 12:52
  • @MohamedOraby the `.catch()` is chained, that's perfectly fine (there _is_ an issue with sending two responses in case of errors being thrown, though, but that would not cause an Axios error). – robertklep Sep 22 '22 at 12:53
  • @VitoSandrin did you try removing the `httpsAgent`? – robertklep Sep 22 '22 at 12:56
  • @robertklep Yes! keeps return the same error. in httpsAgent just set "rejectUnauthorized: false" – Vito Sandrin Sep 22 '22 at 12:58
  • You say you made the request to the source API without going through the controller. Did you do that with Node.js code, or with another method (Postman, cURL, browser, ...)? – robertklep Sep 22 '22 at 13:00
  • @robertklep didn't say it was causing an axios error, the way it is written like `const x = async axios.get().then().catch()` he is using an async function without a `catch block` which means there is no place to catch the actual error – Mohamed Oraby Sep 22 '22 at 13:01
  • @MohamedOraby what do you think the `.catch()` does? – robertklep Sep 22 '22 at 13:02
  • @robertklep yes, I made a request in API Client's (imnsonia, postman) and via browser, and everything is ok – Vito Sandrin Sep 22 '22 at 13:04
  • @MohamedOraby would that throw an `AxiosError`? – robertklep Sep 22 '22 at 13:06
  • @VitoSandrin it doesn't really make sense that you're getting a 404 error that is uncaught, while your code is clearly catching Axios errors. Are you sure there isn't some other part of your app that might be causing this error? – robertklep Sep 22 '22 at 13:08
  • @robertklep Sure, because it's just this controller function that is returning an error, all the others are ok. OBS: I tried to make the request also with node-fetch, and the funny thing is that it returns the same error at the end of the log "AxiosError: Request failed with status code 404". Even if I make the request with a fetch instead of using axios... – Vito Sandrin Sep 22 '22 at 13:28
  • @VitoSandrin that points to another issue then. Perhaps it's the text of the actual response you receive from the API? Or there's another part of your app (perhaps a middleware) that's actually causing the error, and not your controller. – robertklep Sep 22 '22 at 13:33
  • @robertklep I also thought that it could be a middleware, I disabled all on that endpoint. I synthesized my code to the maximum to debug and the error comes exactly from the request function. Cracking my head here trying to understand huahua – Vito Sandrin Sep 22 '22 at 13:38
  • @VitoSandrin can you double check the url you are passing to to axios ? you are making request to `/pdr` endpoint where as at end of your question you mentioned url `http://localhost:8080/centro-de-montagem/position?cep=joinville&raio=4000` – Faizan Ul Haq Sep 22 '22 at 13:47
  • @VitoSandrin see [this post](https://stackoverflow.com/a/43994999) and its comments: it might be useful to add that `unhandledRejection` handler to your app to see if you get a better insight on where the error originates. – robertklep Sep 22 '22 at 13:51

1 Answers1

-1

You are doing the catch wrong. instead of this:

try {
      await axios.get(`${apiCentroMontagem}/pdr?cep=${cep}&raio=${raio}`, {
        httpsAgent,
      }).then(response => response.data)
    } catch (err) {
      console.log(err);
    } 

do this:

axios.get(`${apiCentroMontagem}/pdr?cep=${cep}&raio=${raio}`, {
    httpsAgent,
}).then(response => console.log(response.data))
    .catch(err => console.log(err))

source: https://github.com/axios/axios/blob/v1.x/README.md

example

BlueBeret
  • 474
  • 3
  • 23