Skip to content
This repository was archived by the owner on May 8, 2020. It is now read-only.

Commit 7d9edf7

Browse files
committed
Merge feat-4-add-custom-fields
2 parents 5e790bb + 0194a24 commit 7d9edf7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+255
-171
lines changed

docs/getting-started.md

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,57 @@ You can get the latest release using npm:
88
$ npm install --save ts-httpexceptions
99
```
1010

11-
> **Important!** Ts.ED requires Node >= 6, Express >= 4 and TypeScript >= 2.0.
11+
> **Important!** Ts.ED requires Node >= 8 and TypeScript >= 2.0.
1212
1313

1414
## Example
1515

1616
```typescript
17-
import {BadRequest, Exception} from 'ts-httpexceptions';
18-
let express = require('express');
19-
let app = express();
20-
21-
app.get('/my/route', (req, res) => {
17+
import {BadRequest, Exception, NotFound} from 'ts-httpexceptions';
18+
const express = require('express'); // Koa works also
19+
const app = express();
20+
21+
app.get('/my/route/:id', async (req, res, next) => {
22+
if (req.params.id === undefined) {
23+
const error = new BadRequest("ID is required")
2224

23-
throw new BadRequest('Custom Message'); //Emit
24-
// OR
25-
// throw new Exception(510, 'Custom Message');
25+
// Additionally
26+
error.headers = {
27+
'x-header': 'value'
28+
}
29+
error.errors = [{'message': "ID is required"}]
30+
error.body = [{'message': "ID is required"}]
2631

27-
});
32+
next(error);
33+
}
34+
35+
try {
36+
const user = await getUser(res.params.id)
37+
res.json(user);
38+
} catch(origin) {
39+
next(new NotFound('User not found', origin))
40+
}
41+
});
42+
43+
44+
//GlobalHandler middleware catch exception and send response to the client
45+
app.use((err, request, response, next) => {
46+
if(err instanceof Exception) {
47+
if (err.errors) { // If errors is provided
48+
response.set({'x-errors': JSON.stringify(err.errors)})
49+
}
2850

29-
app.get('/my/route/params', (req, res) => {
30-
31-
if (req.params.id === undefined){
32-
throw new BadRequest();
33-
}
34-
35-
});
51+
if (err.headers) {
52+
response.set(err.headers)
53+
}
3654

55+
if (err.body) { // If a body is provided
56+
return response.status(err.status).json(err.body)
57+
}
3758

38-
//GlobalHandler middleware catch exception and send response to the client
39-
app.use((err, request, response) => {
40-
41-
if(err instanceof Exception){
42-
response.status(err.status).send(err.message);
43-
}
44-
45-
});
59+
return response.status(err.status).send(err.message);
60+
}
61+
62+
next()
63+
});
4664
```

readme.md

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,35 +83,53 @@ $ npm install ts-httpexceptions
8383
## API
8484

8585
```typescript
86-
import {BadRequest, Exception} from 'ts-httpexceptions';
87-
let express = require('express');
88-
let app = express();
89-
90-
app.get('/my/route', (req, res) => {
86+
import {BadRequest, Exception, NotFound} from 'ts-httpexceptions';
87+
const express = require('express');
88+
const app = express();
89+
90+
app.get('/my/route/:id', async (req, res, next) => {
91+
if (req.params.id === undefined) {
92+
const error = new BadRequest("ID is required")
9193

92-
throw new BadRequest('Custom Message'); //Emit
93-
// OR
94-
// throw new Exception(510, 'Custom Message');
94+
// Additionally
95+
error.headers = {
96+
'x-header': 'value'
97+
}
98+
error.errors = [{'message': "ID is required"}]
99+
error.body = [{'message': "ID is required"}]
95100

96-
});
101+
next(error);
102+
}
103+
104+
try {
105+
const user = await getUser(res.params.id)
106+
res.json(user);
107+
} catch(origin) {
108+
next(new NotFound('User not found', origin))
109+
}
110+
});
111+
112+
113+
//GlobalHandler middleware catch exception and send response to the client
114+
app.use((err, request, response, next) => {
115+
if(err instanceof Exception) {
116+
if (err.errors) { // If errors is provided
117+
response.set({'x-errors': JSON.stringify(err.errors)})
118+
}
97119

98-
app.get('/my/route/params', (req, res) => {
99-
100-
if (req.params.id === undefined){
101-
throw new BadRequest();
102-
}
103-
104-
});
120+
if (err.headers) {
121+
response.set(err.headers)
122+
}
105123

124+
if (err.body) { // If a body is provided
125+
return response.status(err.status).json(err.body)
126+
}
106127

107-
//GlobalHandler middleware catch exception and send response to the client
108-
app.use((err, request, response) => {
109-
110-
if(err instanceof Exception){
111-
response.status(err.status).send(err.message);
112-
}
113-
114-
});
128+
return response.status(err.status).send(err.message);
129+
}
130+
131+
next()
132+
});
115133
```
116134

117135

src/clientErrors/BadMapping.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import {Exception} from "../core/Exception";
22

3-
/**
4-
*
5-
*/
63
export class BadMapping extends Exception {
4+
static readonly STATUS = 421;
75
name: string = "BAD_MAPPING";
86

9-
/**
10-
*
11-
* @param message
12-
*/
13-
constructor(message: string) {
14-
super(421, message);
7+
constructor(message: string, origin?: Error | string | any) {
8+
super(BadMapping.STATUS, message, origin);
159
}
1610
}

src/clientErrors/BadRequest.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {Exception} from "../core/Exception";
22

33
export class BadRequest extends Exception {
4+
static readonly STATUS = 400;
45
name: string = "BAD_REQUEST";
56

6-
constructor(message: string) {
7-
super(400, message);
7+
constructor(message: string, origin?: Error | string | any) {
8+
super(BadRequest.STATUS, message, origin);
89
}
910
}

src/clientErrors/Conflict.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {Exception} from "../core/Exception";
22

33
export class Conflict extends Exception {
4+
static readonly STATUS = 409;
45
name: string = "CONFLICT";
56

6-
constructor(message: string) {
7-
super(409, message);
7+
constructor(message: string, origin?: Error | string | any) {
8+
super(Conflict.STATUS, message, origin);
89
}
910
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {Exception} from "../core/Exception";
22

33
export class ExpectationFailed extends Exception {
4+
static readonly STATUS = 417;
45
name: string = "EXPECTATION_FAILED";
56

6-
constructor(message: string) {
7-
super(417, message);
7+
constructor(message: string, origin?: Error | string | any) {
8+
super(ExpectationFailed.STATUS, message, origin);
89
}
910
}

src/clientErrors/Forbidden.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {Exception} from "../core/Exception";
22

33
export class Forbidden extends Exception {
4+
static readonly STATUS = 403;
45
name: string = "FORBIDDEN";
56

6-
constructor(message: string) {
7-
super(403, message);
7+
constructor(message: string, origin?: Error | string | any) {
8+
super(Forbidden.STATUS, message, origin);
89
}
910
}

src/clientErrors/Gone.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {Exception} from "../core/Exception";
22

33
export class Gone extends Exception {
4+
static readonly STATUS = 410;
45
name: string = "GONE";
56

6-
constructor(message: string) {
7-
super(410, message);
7+
constructor(message: string, origin?: Error | string | any) {
8+
super(Gone.STATUS, message, origin);
89
}
910
}

src/clientErrors/ImATeapot.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {Exception} from "../core/Exception";
22

33
export class ImATeapot extends Exception {
4+
static readonly STATUS = 418;
45
name: string = "IM_A_TEAPOT";
56

6-
constructor(message: string) {
7-
super(418, message);
7+
constructor(message: string, origin?: Error | string | any) {
8+
super(ImATeapot.STATUS, message, origin);
89
}
910
}

src/clientErrors/LengthRequired.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {Exception} from "../core/Exception";
22

33
export class LengthRequired extends Exception {
4+
static readonly STATUS = 411;
45
name: string = "LENGTH_REQUIRED";
56

6-
constructor(message: string) {
7-
super(411, message);
7+
constructor(message: string, origin?: Error | string | any) {
8+
super(LengthRequired.STATUS, message, origin);
89
}
910
}

0 commit comments

Comments
 (0)