Skip to content
This repository has been archived by the owner on Nov 5, 2018. It is now read-only.

CouchDB error objects vs. Error objects #22

Closed
jhs opened this issue Sep 20, 2011 · 1 comment
Closed

CouchDB error objects vs. Error objects #22

jhs opened this issue Sep 20, 2011 · 1 comment

Comments

@jhs
Copy link
Collaborator

jhs commented Sep 20, 2011

I have struggled with this myself when using request directly. Often I want to do this:

do_it(function(error, body, header) {
    if(error)
        throw error;

    console.log("Everything is fine");
    // Do more stuff now.
})

Unfortunately, it is better to throw a new Error() instead of a plain object. This provides traceback information. So I must do this:

do_it(function(error, body, header) {
    if(error)
        throw new Error("Couch error: " + JSON.stringify(error));

    console.log("Everything is fine");
    // Do more stuff now.
})

But what if there was a socket error and I already have an Error object? It is all a bit messy.

What do you think about this (I have not tried it in code, just thinking about it now). When Couch returns an error:

  1. Create a new Error with perhaps a helpful description of what went wrong (e.g. "Failed to create document")
  2. Copy the CouchDB error object on top of the Javascript error object
  3. Return that in the callback

This way, the user has a choice:

do_it(tries, function on_done(error, body, header) {
    if(error.message == "no_db_file")
        return do_it(tries+1, on_done);
    else if(error)
        throw error;

    console.log("Everything is fine");
    // Do more stuff now.
})

Would that work or is there a problem with the idea? Thanks!

@dscape dscape closed this as completed Sep 20, 2011
@dscape
Copy link
Contributor

dscape commented Sep 20, 2011

This is how nano errors work :)

Case closed, @daleharvey might reopen complaining this suuuucks :P

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants