Diligence: IllegalArgumentException when querying with invalid ObjectID

16 views
Skip to first unread message

Peter O'Malley

unread,
Jan 20, 2014, 5:09:42 PM1/20/14
to prudence-...@googlegroups.com
I've been playing around with Diligence, specifically the REST over MongoDb part, and I'm running into an IllegalArgumentException when I submit an invalid ObjectID. For example, using the diligence-example application:
http://localhost:8080/diligence-example/mongo/users/Not12Bytes/

I would think this should return a 404 rather than 500, since it seems equivalent to looking up a non-existent ObjectID (which does give a 404). (Not to mention it breaks the use case of people using their own ID's, which is admittedly a minor concern.)

I'm not sure what the best fix (if any) for this is, but I temporarily caught the exception in mongo-db.js as I detailed in the issue I opened here:

http://localhost:8080/diligence-example/mongo/users/Not12Bytes/

Thanks!

Peter O'Malley

unread,
Jan 20, 2014, 6:23:43 PM1/20/14
to prudence-community
While on the subject of Diligence's REST service, it's not clear to me
how I can get the MongoDb resource to output HTML. In the
diligence-example it does not (just says "Could not represent as
HTML", despite claims to the contrary on the example page :-), and
there's this bit in the documentation:

You can customize this view as you please: just create
"/diligence/service/rest/singular.html" and
"/diligence/service/rest/plural.html" files in your "/fragments/"
directory. You can start with the default files under your container's
"/libraries/prudence/" directory as a template.

But I'm not sure where my /fragments/ should be as I can find no
reference to it in Prudence documentation, nor is there an obvious (to
me) place in the diligence example code.

I guess I'm having a documentation searching problem and any pointers
would be helpful.

Thanks again!
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Prudence Community" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/prudence-community/s0jSps0_VNg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> prudence-commun...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Tal Liron

unread,
Jan 21, 2014, 2:18:16 AM1/21/14
to prudence-...@googlegroups.com
The MongoDB API knows nothing about REST! It's up to you to catch the exception and return 404 if indeed it's a not-found error. Something like this should work:

try { ... } catch(x) {
 if (x.code == MongoDB.Error.NotFound) { return 404 }
}

Otherwise, all uncaught exceptions in Prudence indeed become 500 errors, which I think is appropriate. 500 errors are generally handled specially in Prudence according to the "debug" mode:

http://threecrickets.com/prudence/manual/debugging/#debug-page

I'm traveling right now and can't easily


--
You received this message because you are subscribed to the Google Groups "Prudence Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prudence-commun...@googlegroups.com.

Peter O'Malley

unread,
Jan 21, 2014, 3:51:43 AM1/21/14
to prudence-community
Ah. I see my question was poorly worded. What I meant was that when
the MongoDb driver gets an invalid objectID it throws an exception,
rather than returning null, which is what it does when it gets a valid
but non-existent objectID. This means that when you have a
Diligence.REST.MongoDbResource you get 500s for invalid object IDs,
and I'm not sure how to fix that without changing the MongoDb driver,
or extending MongoDbResource, for which the documentation is "todo"
;-).

Enjoy your travels and don't worry about it, I'm patient. :-)
> You received this message because you are subscribed to a topic in the
> Google Groups "Prudence Community" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/prudence-community/s0jSps0_VNg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Tal Liron

unread,
Jan 21, 2014, 2:15:16 PM1/21/14
to prudence-...@googlegroups.com
If you put try/catch around the exception, would it indeed be caught?

Feel very free to suggest or submit a patch! Part of the fun of working with JavaScript is that it's so easy to "hack" without having to recompile anything. Try something, see if it works.

Peter O'Malley

unread,
Jan 21, 2014, 9:09:15 PM1/21/14
to prudence-community
Yeah I ended up putting a try/catch in the MongoDb driver and that
suits my purposes. I just couldn't figure out where to try to catch
the exception later on, because I set the dispatcher in the setup and
whatnot and then after that I'm not sure where it's executing when the
call happens. But hacking the MongoDb driver for now works fine.
Reply all
Reply to author
Forward
0 new messages