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

Return proper error objects instead regular objects / arrays / strings. #123

Open
jsdalton opened this issue Oct 22, 2012 · 5 comments
Open

Comments

@jsdalton
Copy link

It would be great if this library returned Error objects with useful types and functionality (e.g. ValidationError, DatabaseError) instead of just objects and/or arrays as present.

Here's a great example of how the present implementation creates a pain point:

var resourceful = require('resourceful');

// Caution: Script creates db named 'test_dummy'
resourceful.use('couchdb', { database: 'test_dummy' });

var db = resourceful.connection.connection;

var Author = resourceful.define('author');
Author.string('name', { required: true });

db.create(function (err, result) {
  Author.create({
    id: 'Marak',
    name: 'Marak',
  }, function(err, marak){
    marak.name = undefined;

    // Save resource fails because of validation error
    marak.save(function(err, result){
      console.log(err); // [ { attribute: 'required', property: 'name', expected: true, actual: undefined, message: 'is required' } ]
      marak.name = 'Marak';
      db.destroy(function (err, result) {

        // Save resource fails because db does not exist
        marak.save(function(err, result) {
          console.log(err); // { error: 'not_found', reason: 'no_db_file', status: 404 }
          db.destroy(function(err, result) {});
        });
      });
    })
  });
});

Note that the save() method on the resource returns an array of objects on a validation error and a different object on a database error. Now imagine you're coding up an API controller that has to determine whether to return a 400 in the case of a validation error and a 500 for a database failure. The logic to make that determination would be quite awkward and ugly.

I'll observe that Mongoose returns custom error objects in situations like this (https://github.com/LearnBoost/mongoose/tree/master/lib/errors) and I've certainly read plenty of arguments in support of this approach (http://dustinsenos.com/articles/customErrorsInNode).

I also have a Python background so I'm used to making liberal use of exceptions. You guys might have your reasons for the way things are now, so this is just a feature suggestion.

@mmalecki
Copy link
Contributor

+1

@RobinQu
Copy link

RobinQu commented May 22, 2013

Express will not handle correctly with these plain objects. Won't send them to global error handlers. Wried!

@jfhbrook
Copy link
Contributor

I think this can be fixed almost entirely in cradle flatiron/cradle#216

@RobinQu
Copy link

RobinQu commented May 22, 2013

There are more than one database solution than cradle...... not to mention a large group of people are using other DBs like mongo and mysql.

@RobinQu
Copy link

RobinQu commented May 22, 2013

resourceful may be the most "resourceful" module in flatiron series. Hope it will get better and more active.

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