Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
537 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
docs/docs/snippets/exceptions/custom-global-error-handler-usage.ts
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
docs/docs/snippets/exceptions/custom-global-error-handler.ts
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
docs/docs/snippets/exceptions/resource-not-found-filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {Catch, ExceptionFilterMethods, PlatformContext, ResourceNotFound} from "@tsed/common"; | ||
|
||
@Catch(ResourceNotFound) | ||
export class ResourceNotFoundFilter implements ExceptionFilterMethods { | ||
async catch(exception: ResourceNotFound, ctx: PlatformContext) { | ||
const {response} = ctx; | ||
|
||
const obj = { | ||
status: exception.status, | ||
message: exception.message, | ||
url: exception.url | ||
}; | ||
// Json response | ||
response | ||
.status(exception.status) | ||
.body(obj); | ||
|
||
// Or with ejs/handlers/etc... | ||
await response | ||
.status(exception.status) | ||
.render("404.ejs", obj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
|
||
|
||
## GlobalErrorHandler middleware to Exception filter | ||
|
||
[Exception filter]() is the | ||
|
||
To facilitate your migration, remove the line where you add you custom middleware in the server: | ||
|
||
```typescript | ||
$afterRoutesInit() { | ||
this.app.use(CustomGlobalErrorHandlerMiddleware); // remove this | ||
} | ||
``` | ||
|
||
Then, use the @@OverrideProvider@@ decorator over your custom middleware: | ||
|
||
```typescript | ||
import {OverrideProvider} from "@tsed/di"; | ||
import {GlobalErrorHandlerMiddleware} from "@tsed/common"; | ||
|
||
@OverrideProvider(GlobalErrorHandlerMiddleware) | ||
export class CustomGlobalErrorHandlerMiddleware extends GlobalErrorHandlerMiddleware { | ||
|
||
} | ||
``` | ||
|
||
Now you are able to create your own exception filter. Start with the HttpException example: | ||
|
||
<<< @/docs/docs/snippets/exceptions/http-exception-filter.ts | ||
|
||
Then try with another error type and finally, remove your custom middleware. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,29 @@ | ||
# Customize 404 | ||
|
||
The guide shows you how you can customize the 404 response error emitted by Express.js. | ||
The guide shows you how you can customize the 404 response error when a resource or route isn't resolved by | ||
the router. | ||
|
||
To begin, create a new Middleware named `NotFoundMiddleware`: | ||
Customizing error is possible by using the [Exception filter feature](/docs/exceptions.html#exception-filter) and by catching | ||
the @@ResourceNotFound@@ error class. This error is thrown by Ts.ED when nothing routes are resolved. | ||
|
||
```typescript | ||
import {Middleware, Res, Next} from "@tsed/common"; | ||
Create a new ResourceNotFoundFilter in the filters directories and copy/paste this example: | ||
|
||
@Middleware() | ||
export class NotFoundMiddleware { | ||
use(@Res() response: Res, @Next() next: Next) { | ||
// Json response | ||
response.status(404).json({status: 404, message: 'Not found'}); | ||
<<< @/docs/docs/snippets/exceptions/resource-not-found-filter.ts | ||
|
||
// Or with ejs | ||
response.status(404).render("404.ejs", {}, next); | ||
} | ||
} | ||
``` | ||
::: warning | ||
`response.render()` require to configure the template engine before. See our page over [Templating engine](/tutorials/templating.html#installation) installation for more details. | ||
::: | ||
|
||
Then register your middleware on the `$afterRoutesInit` in your Server: | ||
Then import the custom filter in your server: | ||
|
||
```typescript | ||
import {Inject} from "@tsed/di"; | ||
import {Configuration, PlatformApplication} from "@tsed/common"; | ||
import {NotFoundMiddleware} from "./middlewares/NotFoundMiddleware"; | ||
import "./filters/ResourceNotFoundFilter"; // Importing filter with ES6 import is enough | ||
|
||
@Configuration({ | ||
// ... | ||
}) | ||
export class Server { | ||
@Inject() | ||
app: PlatformApplication; | ||
|
||
public $afterRoutesInit() { | ||
this.app.use(NotFoundMiddleware); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/common/src/platform-exceptions/errors/ResourceNotFound.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {NotFound} from "@tsed/exceptions"; | ||
|
||
export class ResourceNotFound extends NotFound { | ||
readonly url: string; | ||
|
||
constructor(url: string) { | ||
super(`Resource "${url}" not found`); | ||
|
||
this.url = url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.