-
-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge feat-add-httpsexceptions-packages
- Loading branch information
Showing
161 changed files
with
1,657 additions
and
104 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
meta: | ||
- name: description | ||
content: Documentation over Http Exceptions provided by Ts.ED framework. Use class to throw a standard Http error. | ||
- name: keywords | ||
content: http exceptions ts.ed express typescript node.js javascript decorators jsonschema class models | ||
--- | ||
# Http exceptions | ||
|
||
Ts.ED http exceptions provide classes to throw standard HTTP exceptions. Theses exceptions can be used on Controller, Middleware or injectable Service. | ||
Emitted exceptions will be handle by the @@GlobalErrorHandlerMiddleware@@ and formatted to an Express response with the right status code and headers. | ||
|
||
Other thing. This module can be used with a pure Express application. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install @tsed/exceptions | ||
// or | ||
yarn add @tsed/exceptions | ||
``` | ||
|
||
## Usage | ||
|
||
<Tabs class="-code"> | ||
<Tab label="Ts.ED"> | ||
|
||
<<< @/docs/docs/snippets/exceptions/usage-controller.ts | ||
|
||
</Tab> | ||
<Tab label="Express.js"> | ||
|
||
<<< @/docs/docs/snippets/exceptions/usage-express-route.ts | ||
|
||
</Tab> | ||
</Tabs> | ||
|
||
## Custom exception | ||
|
||
It's possible to create your own exception by creating a class which inherit from @@Exception@@ or one of the built-in exception like @@BadRequest@@. | ||
|
||
Example: | ||
```typescript | ||
import {BadRequest} from "@tsed/exceptions"; | ||
|
||
export class IDFormatException extends BadRequest { | ||
constructor() { | ||
super("ID format is not valid"); | ||
} | ||
} | ||
``` | ||
|
||
## Built-in exceptions | ||
|
||
### Redirections (3xx) | ||
|
||
<ApiList query="module == '@tsed/exceptions' && symbolType === 'class' && path.indexOf('redirections') > -1" /> | ||
|
||
### Client errors (4xx) | ||
|
||
<ApiList query="module == '@tsed/exceptions' && symbolType === 'class' && path.indexOf('clientErrors') > -1" /> | ||
|
||
### Server errors (5xx) | ||
|
||
<ApiList query="module == '@tsed/exceptions' && symbolType === 'class' && path.indexOf('serverErrors') > -1" /> | ||
|
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
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,33 @@ | ||
import {Controller, Get, Inject, PathParams} from "@tsed/common"; | ||
import {BadRequest, NotFound} from "@tsed/exceptions"; | ||
import {CalendarsService} from "../services/CalendarsService"; | ||
|
||
@Controller("/calendars") | ||
export class CalendarCtrl { | ||
@Inject() | ||
calendarsService: CalendarsService; | ||
|
||
@Get("/:id") | ||
async get(@PathParams("id") id: number) { | ||
if (isNaN(+id)) { | ||
const error = new BadRequest("Not a number"); | ||
|
||
// Additionally | ||
error.setHeaders({ | ||
"x-header": "value" | ||
}); | ||
|
||
error.errors = [{"message": "ID is not a number"}]; | ||
|
||
throw(error); | ||
} | ||
|
||
const calendar = await this.calendarsService.get(id); | ||
|
||
if (!calendar) { | ||
throw new NotFound("Calendar not found"); | ||
} | ||
|
||
return calendar; | ||
} | ||
} |
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,57 @@ | ||
import {BadRequest, Exception, InternalServerError, NotFound} from "@tsed/exceptions"; | ||
import * as Express from "express"; | ||
import {getCalendar} from "../services/CalendarService"; | ||
|
||
const app = Express(); | ||
|
||
app.get("/calendars/:id", async (req: any, res: any, next: any) => { | ||
const {params: {id}} = req; | ||
|
||
if (isNaN(+id)) { | ||
const error = new BadRequest("ID is not a number"); | ||
|
||
// Additionally | ||
error.setHeaders({ | ||
"x-header": "value" | ||
}); | ||
|
||
error.errors = [{"message": "ID is not a number"}]; | ||
error.body = "Not a number"; | ||
|
||
return next(error); | ||
} | ||
|
||
try { | ||
const calendar = await getCalendar(res.params.id); | ||
|
||
if (!calendar) { | ||
return next(new NotFound("Calendar not found"); | ||
} | ||
|
||
res.json(calendar); | ||
} catch (origin) { | ||
next(new InternalServerError("Oops! Something is wrong", origin)); | ||
} | ||
}); | ||
|
||
|
||
// GlobalHandler middleware catch exception and send response to the client | ||
app.use((err: any, request: any, response: any, next: any) => { | ||
if (err instanceof Exception) { | ||
if (err.errors) { // If errors is provided | ||
response.set({"x-errors": JSON.stringify(err.errors)}); | ||
} | ||
|
||
if (err.headers) { | ||
response.set(err.headers); | ||
} | ||
|
||
if (err.body) { // If a body is provided | ||
return response.status(err.status).json(err.body); | ||
} | ||
|
||
return response.status(err.status).send(err.message); | ||
} | ||
|
||
next(); | ||
}); |
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
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
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.