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

Validation errors return an HTTP 500 Error #56

Closed
kulakowka opened this issue Feb 18, 2016 · 3 comments
Closed

Validation errors return an HTTP 500 Error #56

kulakowka opened this issue Feb 18, 2016 · 3 comments

Comments

@kulakowka
Copy link

In User model, I changed username attribute to unique and required.

// user-model.js - A mongoose model
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.

import mongoose from 'mongoose';
const Schema = mongoose.Schema;

let userSchema = new Schema({
  username: {type: String, required: true, unique: true, index: true},
  password: {type: String, required: true},
  createdAt: {type: Date, 'default': Date.now},
  updatedAt: {type: Date, 'default': Date.now}
});

let userModel = mongoose.model('user', userSchema);

export default userModel;

When I try to send a POST request to http://localhost:3030/users with empty body parameters, I get a 500 Internal Server Error.

2016-02-18 17 46 04

However, I see in the console:

error: (400) Route: /users - user validation failed
info: BadRequest: user validation failed
    at Object.construct (/Users/kulakowka/Documents/node.js/sandbox/feathers-chat/node_modules/core-js/modules/es6.reflect.construct.js:22:24)
    at BadRequest.ExtendableBuiltin (/Users/kulakowka/Documents/node.js/sandbox/feathers-chat/node_modules/feathers-errors/lib/index.js:27:28)
    at BadRequest.FeathersError (/Users/kulakowka/Documents/node.js/sandbox/feathers-chat/node_modules/feathers-errors/lib/index.js:105:87)
    at new BadRequest (/Users/kulakowka/Documents/node.js/sandbox/feathers-chat/node_modules/feathers-errors/lib/index.js:145:79)
    at errorHandler (/Users/kulakowka/Documents/node.js/sandbox/feathers-chat/node_modules/feathers-mongoose/lib/error-handler.js:23:25)
    at run (/Users/kulakowka/Documents/node.js/sandbox/feathers-chat/node_modules/core-js/modules/es6.promise.js:104:47)
    at /Users/kulakowka/Documents/node.js/sandbox/feathers-chat/node_modules/core-js/modules/es6.promise.js:115:28
    at flush (/Users/kulakowka/Documents/node.js/sandbox/feathers-chat/node_modules/core-js/modules/$.microtask.js:19:5)
    at nextTickCallbackWith0Args (node.js:452:9)
    at process._tickCallback (node.js:381:13)

In documents written that I'll get a nice error handling out of the box. But this is not the case.

Maybe I'm doing something wrong?
How to check the uniqueness of user name in another way?

@kulakowka
Copy link
Author

I think I understand.

I removed the error handling from /src/middleware/index.js and added simple error handler to the end of file:

import path from 'path';
import notFound from './not-found-handler';
import { handler as error } from 'feathers-errors';
import logger from './logger';

export default function() {
  const app = this;

  // Add your custom middleware here. Remember, that
  // just like Express the order matters, so error
  // handling middleware should go last.
  app.use(notFound())
    .use(logger(app))
    // .use(error());


  // This simple error handler was added
  app.use(function(error, req, res, next){
    res.json(error);
  });
}

Now the error displayed in the proper format:

2016-02-18 18 04 11

It works, but I'm not sure I did it right.

@daffl
Copy link
Member

daffl commented Feb 18, 2016

You need to set the Accept header in your request to application/json otherwise the default error handler thinks you want HTML.

The error page should probably show a better message though. You get a 400 code but it shows up as a 500 error page. It should probably just say something like "General error".

@kulakowka
Copy link
Author

Thank you @daffl. I forgot about this header! :)

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