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

uncaught promise rejection for /:book/users when entering an invalid book #38

Open
rooberrydev opened this issue Apr 17, 2020 · 2 comments
Labels
bug Something isn't working code review

Comments

@rooberrydev
Copy link

booksModel
.getIdFromTitle(book)
.then((bookInfo) => {
const bookId = bookInfo.rows[0].id
junctionModel
.getUserFromBook(bookId)
.then((userInfo) => {
const userIds = userInfo.rows.map(obj => obj.user_id)
usersModel
.getMultipleUsersById(userIds)
.then((userList) => {
const users = userList.rows.map(e => e.username);
console.log(users);
res.status(200).send(users)
})
})
.catch(next)
})

again, hard to keep track of due to nested brackets, but its a case of a missing .catch()

you're catching junctionModel.getUserFromBook, but before that there's booksModel.getIdFromTitle which will throw an exception when theres an error.

adding .catch(next) on line 77 should do the trick.

btw, absolutely brilliant naming for your model functions!

@rooberrydev rooberrydev added the bug Something isn't working label Apr 17, 2020
@jackherizsmith
Copy link

do all queries need catches?

@rooberrydev
Copy link
Author

@jackherizsmith Ideally you should have a catch somewhere in the chain. If the db query returns a resolved/rejected promise to somewhere else, you can put the catch there(in the handler) to make the error handling a server response.
So queries themselves can be left without cstches, just return the promise in its final state, and then you can propagate all errors up to the handler level and .catch() there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working code review
Projects
None yet
Development

No branches or pull requests

3 participants