Pajk/apipie-rails — pull request #48 by ronen (2012-08-16 14:42:01+00:00)
Title: Added exception classes Apipie::ParamInvalid and Apipie::ParamMissing

Thanks for creating Apipie! Here's a small enhancement...

Apipie currently raises ArgumentError for missing or invalid request params. But that makes it hard for the controller to distinguish between an internal argument error that's a bug in the server code, which should respond with status 500, vs a client making an invalid request which should respond with status 400.

So I've created two Exception classes, Apipie::ParamInvalid and Apipie::ParamMissing that Apipie raises. An added benefit is that the classes have attributes for parameter name and value, allowing detailed information to be returned to the client. E.g. the base controller for my API now has:

rescue_from Apipie::ParamInvalid do |exception|
render status: 400, json: {
param: exception.param,
value: exception.value,
error: exception.to_s,
}
end

I also took the liberty of tweaking the description strings slightly, though that's in a separate commit that you can skip if you don't like.

Cheers,
-Ronen