lars icon indicating copy to clipboard operation
lars copied to clipboard

override 404 and 405 handlers

Open dvic opened this issue 7 years ago • 8 comments

Is it possible to override the 404 and 405 handlers of a route/group? Is it something you would like to have? I could submit a PR, making a NewFromOpts where you can pass the following:

  • 404 handlersChain HandlersChain
  • 405 handlersChain HandlersChain
  • redirectTrailingSlash bool
  • handleMethodNotAllowed bool
  • automaticallyHandleOPTIONS bool

By the way: just wanted to say that I really like this router, AWESOME work! Lately I have been looking extensively into the 'router ecosystem' of Go 😉 and this is exactly what I was looking for! 👍

dvic avatar Jul 01 '17 13:07 dvic

Hey @dvic glad you like the router, there are too many out there re-inventing the wheel, but this one is the only one that you can use a customer Context object that I'm aware of; but I dirges.

I'm just curious , I can't see a reason to have differing values per group for redirectTrailingSlash

What's the use case for having this differ per route group? So I understand the use case.

P.S. I would make them options on the RouteGroup object themselves rather than a new constructor.

deankarn avatar Jul 01 '17 17:07 deankarn

Ah yes it makes sense to have SetRedirectTrailingSlash only available on LARS (which is already the case). How about adding the following methods to the IRouteGroup interface (which LARS should continue to implement):

  • SetHttp404HandlersChain (currently also missing on LARS)
  • SetHttp405HandlersChain (currently also missing on LARS)
  • SetHandle405MethodNotAllowed
  • SetAutomaticallyHandleOPTIONS

dvic avatar Jul 01 '17 21:07 dvic

I'm just trying to understand the use case for doing so I always try and understand the roadblock before proceeding as sometimes there are better ways...

Why would you need a separate 404 or 405 handler per group?

deankarn avatar Jul 01 '17 23:07 deankarn

Sure, no problem. Let's say I have /assets/* (css, images, etc.), /uploads/* (files uploaded by users) and /api/*. For the /assets/* group I don't care so much, because requests to this endpoint are done by browsers. However, for /uploads/* I would like to show a nicely formatted error pages (probably specific to the current web app) as these requests will be made by users directly in the browser. For /api/* the same applies, there I would like to show a JSON/XML/etc. 404 response. Is this something I can achieve with the current API? Thanks :)

dvic avatar Jul 01 '17 23:07 dvic

I see @dvic how that could be useful, I'll have to think about it for a bit on how to do it. RouteGroups aren't really used passed the registration of handlers.... don't yet know how I would link the 404 to a particular group the way the router works.

I mean there is a workaround with the current implementation that may be just as good though, why not add a middleware to each of the route groups you wish to handle differently and if a 404 status code is present when request is returning do your custom page rendering or json 404.

Sorry for the late replies, you just caught me in the middle of moving...likely be busy for the next couple weeks at least.

deankarn avatar Jul 03 '17 22:07 deankarn

Ah just to make sure I understand: I would use a 404 handler that only sets status code to 404 and then use (as last) a middleware on each group to render the actual response?

dvic avatar Jul 03 '17 22:07 dvic

Yep that should work

deankarn avatar Jul 03 '17 22:07 deankarn

Cool, thanks!

dvic avatar Jul 03 '17 23:07 dvic