Tsed: How to use a 404 handler?

Created on 23 Mar 2018  路  6Comments  路  Source: tsedio/tsed

Informations

Type |聽Version
---|---
Question | 4.5.3


Description

Hi! @Romakita

I want to customize 404 error page.
Cannot GET /v1/api/...

So I overridden middleware GlobalErrorHandlerMiddleware, but it didn't come this way.
How can I customize that page?

Most helpful comment

Worked for me as well. Maybe should be added to docs?

All 6 comments

HI. @yangukmo
there is guide describe about globalErrorhanlder and endPointErrorHandler

global: https://romakita.github.io/ts-express-decorators/#/docs/middlewares/global-error-middleware
endpoint: https://romakita.github.io/ts-express-decorators/#/docs/middlewares/endpoint-error-middleware

Hi, @jindev
I tried it.
This guide only works when successfully passed through the router.
But, if router is not found, it's not passed to GlobalErrorHandler.

I think 404 does not seem to handle error.

@yangukmo I think you've right. 404 isn't an error with the your code (like 500).
To customize your 404 page, you just need to add a final middleware to handler all routes that not match with the routing table.

So an example is better:

class Server extends ServerLoader聽{

   $afterRoutesInit() {
      this.use(Custom404Middleware);
   }
}

@Middleware()
class Custom404Middleware {
     use(@Res response) {
         response.send("404 Custom");
     }
}

See you,
Romain

It's really simple 馃憤
I thought GlobalErrorHandler would handle 404 of course.

Thank you! @Romakita

Worked for me as well. Maybe should be added to docs?

Yes :)

Was this page helpful?
0 / 5 - 0 ratings