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

How to return 404? #289

Open
adamlamar opened this issue Sep 19, 2022 · 2 comments
Open

How to return 404? #289

adamlamar opened this issue Sep 19, 2022 · 2 comments
Labels
improvement New feature or request waiting for info We're waiting for a response

Comments

@adamlamar
Copy link

In the show action, I'd like to return 404 if no item in the database exists with that ID.

Is there a specific error to return for the client to get a 404, both with html and json? Is there a pattern for returning an error partial?

@matthewmueller
Copy link
Contributor

matthewmueller commented Sep 23, 2022

Hey @adamlamar, it's definitely not perfect right now but you can use the pull in the http.ResponseWriter as a dependency to the controller. Then write the response status directly.

So something like:

package users

func New(w http.ResponseWriter, db *pgx.DB) *Controller {
  return &Controller{w, db}
}

type Controller struct {
  w http.ResponseWriter
  db *pgx.DB
}

func (c *Controller) Index() ([]*User, error) {
  users, _ := db.Query(`...`)
  if len(users) == 0 {
    w.WriteStatus(404)
    return nil, nil
  }
}

Also,

Is there a pattern for returning an error partial?

Not yet. As part of building the documentation, I want to add support for layouts and error pages, so this should come soon.

@matthewmueller matthewmueller added waiting for info We're waiting for a response improvement New feature or request labels Sep 23, 2022
@adamlamar
Copy link
Author

Gotcha, thank you! Looking forward to the layouts and error pages too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement New feature or request waiting for info We're waiting for a response
Projects
None yet
Development

No branches or pull requests

2 participants