Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Res.status returns 404 when using promises in routes #20

Closed
imolorhe opened this issue Jan 11, 2017 · 11 comments
Closed

Res.status returns 404 when using promises in routes #20

imolorhe opened this issue Jan 11, 2017 · 11 comments

Comments

@imolorhe
Copy link

Hello,

The response object returns a 404 whenever I use promises with the routes. When I remove the promise, the response is resolved normally. The examples on the site are done with async/await, so I think that is likely the issue. Is there any example using regular promises?

@DylanPiercey
Copy link
Member

Hey,

Thanks for your question. If you didn't mind providing a code snippet that would be a huge help.
Here is an arbitrary example of using Rill with vanilla promises.

require('isomorphic-fetch')

app.get('/home', (ctx) => {
  return fetch('http://google.ca')
    .then(response => response.text())
    .then(html => {
      ctx.res.body = html
    })
})

@imolorhe
Copy link
Author

imolorhe commented Jan 11, 2017

@DylanPiercey I have something like that too. At what point is the res.status set? When the ctx.res.body is set?

@DylanPiercey
Copy link
Member

If the body is non empty the status will default to 200. You can also manually set ctx.res.status = 200.

@imolorhe
Copy link
Author

@DylanPiercey So the promise needs to be returned for it to properly resolve.

@DylanPiercey
Copy link
Member

@imolorhe that is correct, otherwise Rill has no idea when the promise finishes.

@imolorhe
Copy link
Author

@DylanPiercey Okay so what exactly determines the value of ctx.res.status? Is it setting the value of ctx.res.body? Or there is another condition that is applied? I am asking this because it seems I am getting 404 even without a promise involved.

@DylanPiercey
Copy link
Member

ctx.res.status will only automatically set if there is a ctx.res.body, if there is a ctx.res.body it will default to 200. Otherwise it is 404 unless explicitly set by the user.

@imolorhe
Copy link
Author

@DylanPiercey Thanks! I know it's probably too much to ask, but what is the promise-based equivalent of this snippet:

app.use(async (ctx, next)=> {
  try {
    await next()
  } catch (error) {
    // Catch errors later on in the stack.
    log.error('server error', err)
  }
})

@DylanPiercey
Copy link
Member

@imolorhe no problem -

app.use((ctx, next) => {
  return next().catch((err) => {
    log.error('server error', err)
  })
})

@DylanPiercey
Copy link
Member

@imolorhe i'm going to close this for now. Hit me up on gitter if you have any other questions regarding this or promises 😄.

@imolorhe
Copy link
Author

@DylanPiercey Okay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants