rocketpant how to handle 404 routing error

569 Views Asked by At

I am using rocketpant for rest API implementation. From this doc I can see rocketpant has considered error handling in controller nicely.

However, it seems like it does not consider routing error, so if the requested url has no route defined, instead of returning json error, it render back html 404 page, which is not correct for API design.

Do I miss something? Or is there a way to wrap 404 routing error and render a json error message?

1

There are 1 best solutions below

0
On

I found the solution.

Step1: catch all unmatched routes in the last line of routing file.

 match '*a', to: 'error#routing', via: [:get, :post, :put, :patch]

Step2: define a error controller and routing method as custom error handler. throw rocketpant built in :not_found error

module Api
module V1
    class ErrorController < ApiController

        def routing
            error! :not_found # throw not found error, let rocketpant handle error message rendering
        end
    end
end
end