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 change response code and data #102

Closed
supasate opened this issue Jul 17, 2016 · 9 comments
Closed

How to change response code and data #102

supasate opened this issue Jul 17, 2016 · 9 comments

Comments

@supasate
Copy link

I would like my API to return 404 Not Found when a requested data does not exist. Currently, it responds with 400 Bad Request. How can I intercept and modify the response?

Example
Request: GET localhost/mymodels/1234
Response header: 400 Bad Request
Response Data: {"name":"BadRequest","message":"Cast to ObjectId failed for value \"1234\" at path \"_id\"","code":400,"className":"bad-request","errors":{}}

I would like to change the response to something like this:

Response header: 404 Not Found
Response data: {"name":"NotFound","message":"Requested ID does not exist","code":404,"className":"not-found","errors":{}}

or just

Response Header: 404 Not Found
Response data: {"name":"NotFound","message":"Requested ID does not exist","code":404}

@marshallswain
Copy link
Member

Can you try throwing an error using one of the feathers-errors?

@supasate
Copy link
Author

supasate commented Jul 17, 2016

Sound like I have to intercept the error from feathers-mongoose for the GET request and check that error if it's the Cast to ObjectId Failed. Then, throw my modified error with feathers-errors out.

But, I'm not quite sure where should I intercept and throw the error. Please advise.

Here's my code.

const service = require('feathers-mongoose');

module.exports = function() {
  const app = this;
  const options = {
    Model: mymodels,
    paginate: {
      default: 5,
      max: 50
    }
  };
  app.use('/mymodels', service(options));
}

@daffl
Copy link
Member

daffl commented Jul 17, 2016

Not having a 404 here should definitely be fixed in the adapter. To further customize the errors I am thinking of adding afterError hooks but for now you can probably extend the adapter and throw your own errors.

@supasate
Copy link
Author

I found the root cause is caused by using Mongoose findOne with id. So, when a requested id is not in the proper ObjectId format, Mongoose throws CastError.

Should we fix it by first checking the format? If it's not in ObjectId format, we throw new errors.NotFound(No record found for id '${id}') instead.

If you think it's ok to fix this way, I would like to try to send a PR to fix it.

@supasate
Copy link
Author

@daffl I created a PR on this issue. I'm new to this community. Please advise.

@ekryski
Copy link
Member

ekryski commented Jul 17, 2016

I don't think this should actually throw a 404, because if it is a bad ID (which it is) then it actually is a bad request. If you want to have a 404 you can easily add your own before hook to quickly check if hook.id is a valid ObjectId and if not throw a NotFound error.

@ekryski
Copy link
Member

ekryski commented Jul 17, 2016

The other thing is we currently support having an alternative id field as the primary query id instead of the default _id so we can't just throw a 404 on any id that isn't an ObjectId. For example, you could be using something like email as your primary id to query by.

@daffl
Copy link
Member

daffl commented Jul 17, 2016

For the hook implementation for this case see #103 (comment)

@daffl daffl closed this as completed Jul 17, 2016
@supasate
Copy link
Author

@ekryski @daffl I agree. I totally forgot about alternative id. Thank you for your clarification and help.

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

4 participants