Giraffe icon indicating copy to clipboard operation
Giraffe copied to clipboard

EndpointRouting - Catch All Endpoint for Custom 404 error messages

Open shaif-dorIT opened this issue 2 years ago • 3 comments

Hi,

I started using F# and giraffe in order to learn Functional Programming.

I think that the title says it all, but I elaborate:

In regular route with the routex you can use regex route with "/*" to get catchall route.

How can I mimic that behavior in endpoint routing so I can get a custom handler for dealing with 404 in the endpoint list that is needed in .UseEndpoints(fun e -> e.MapGiraffeEndpoints(endpoints))

shaif-dorIT avatar Jan 30 '23 18:01 shaif-dorIT

Why don't you use the routex?

64J0 avatar Aug 03 '23 12:08 64J0

Why don't you use the routex?

It looks like routex isn't supported for the endpoint router.

The documentation does state the default and endpoint router can be used side by side, but I think it would be more elegant to add support for routex and/or adding a simple wildcard router function to the endpoint router.

esbenbjerre avatar Oct 12 '23 09:10 esbenbjerre

After some searching I saw Giraffe maps route directly to IEndpointRouteBuilder.Map[Methods] so we can just use a catch-all route template.

let health: HttpHandler = setStatusCode StatusCodes.Status200OK >=> json {| Message = "OK" |}
let notFound: HttpHandler = setStatusCode StatusCodes.Status404NotFound >=> json {| Message = "Not found" |}

let endpoints =
  [
    GET [
      route "/health" health
    ]
    route "{*url}" notFound
  ]

esbenbjerre avatar Oct 12 '23 18:10 esbenbjerre