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

feat: HttpException and his inherited exception accept an origin parameters #5

Merged
merged 1 commit into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 43 additions & 25 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,57 @@ You can get the latest release using npm:
$ npm install --save ts-httpexceptions
```

> **Important!** Ts.ED requires Node >= 6, Express >= 4 and TypeScript >= 2.0.
> **Important!** Ts.ED requires Node >= 8 and TypeScript >= 2.0.


## Example

```typescript
import {BadRequest, Exception} from 'ts-httpexceptions';
let express = require('express');
let app = express();

app.get('/my/route', (req, res) => {
import {BadRequest, Exception, NotFound} from 'ts-httpexceptions';
const express = require('express'); // Koa works also
const app = express();

app.get('/my/route/:id', async (req, res, next) => {
if (req.params.id === undefined) {
const error = new BadRequest("ID is required")

throw new BadRequest('Custom Message'); //Emit
// OR
// throw new Exception(510, 'Custom Message');
// Additionally
error.headers = {
'x-header': 'value'
}
error.errors = [{'message': "ID is required"}]
error.body = [{'message': "ID is required"}]

});
next(error);
}

try {
const user = await getUser(res.params.id)
res.json(user);
} catch(origin) {
next(new NotFound('User not found', origin))
}
});


//GlobalHandler middleware catch exception and send response to the client
app.use((err, request, response, next) => {
if(err instanceof Exception) {
if (err.errors) { // If errors is provided
response.set({'x-errors': JSON.stringify(err.errors)})
}

app.get('/my/route/params', (req, res) => {

if (req.params.id === undefined){
throw new BadRequest();
}

});
if (err.headers) {
response.set(err.headers)
}

if (err.body) { // If a body is provided
return response.status(err.status).json(err.body)
}

//GlobalHandler middleware catch exception and send response to the client
app.use((err, request, response) => {

if(err instanceof Exception){
response.status(err.status).send(err.message);
}

});
return response.status(err.status).send(err.message);
}

next()
});
```
66 changes: 42 additions & 24 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,53 @@ $ npm install ts-httpexceptions
## API

```typescript
import {BadRequest, Exception} from 'ts-httpexceptions';
let express = require('express');
let app = express();

app.get('/my/route', (req, res) => {
import {BadRequest, Exception, NotFound} from 'ts-httpexceptions';
const express = require('express');
const app = express();

app.get('/my/route/:id', async (req, res, next) => {
if (req.params.id === undefined) {
const error = new BadRequest("ID is required")

throw new BadRequest('Custom Message'); //Emit
// OR
// throw new Exception(510, 'Custom Message');
// Additionally
error.headers = {
'x-header': 'value'
}
error.errors = [{'message': "ID is required"}]
error.body = [{'message': "ID is required"}]

});
next(error);
}

try {
const user = await getUser(res.params.id)
res.json(user);
} catch(origin) {
next(new NotFound('User not found', origin))
}
});


//GlobalHandler middleware catch exception and send response to the client
app.use((err, request, response, next) => {
if(err instanceof Exception) {
if (err.errors) { // If errors is provided
response.set({'x-errors': JSON.stringify(err.errors)})
}

app.get('/my/route/params', (req, res) => {

if (req.params.id === undefined){
throw new BadRequest();
}

});
if (err.headers) {
response.set(err.headers)
}

if (err.body) { // If a body is provided
return response.status(err.status).json(err.body)
}

//GlobalHandler middleware catch exception and send response to the client
app.use((err, request, response) => {

if(err instanceof Exception){
response.status(err.status).send(err.message);
}

});
return response.status(err.status).send(err.message);
}

next()
});
```


Expand Down
12 changes: 3 additions & 9 deletions src/clientErrors/BadMapping.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import {Exception} from "../core/Exception";

/**
*
*/
export class BadMapping extends Exception {
static readonly STATUS = 421;
name: string = "BAD_MAPPING";

/**
*
* @param message
*/
constructor(message: string) {
super(421, message);
constructor(message: string, origin?: Error | string | any) {
super(BadMapping.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/BadRequest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class BadRequest extends Exception {
static readonly STATUS = 400;
name: string = "BAD_REQUEST";

constructor(message: string) {
super(400, message);
constructor(message: string, origin?: Error | string | any) {
super(BadRequest.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/Conflict.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class Conflict extends Exception {
static readonly STATUS = 409;
name: string = "CONFLICT";

constructor(message: string) {
super(409, message);
constructor(message: string, origin?: Error | string | any) {
super(Conflict.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/ExpectationFailed.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class ExpectationFailed extends Exception {
static readonly STATUS = 417;
name: string = "EXPECTATION_FAILED";

constructor(message: string) {
super(417, message);
constructor(message: string, origin?: Error | string | any) {
super(ExpectationFailed.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/Forbidden.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class Forbidden extends Exception {
static readonly STATUS = 403;
name: string = "FORBIDDEN";

constructor(message: string) {
super(403, message);
constructor(message: string, origin?: Error | string | any) {
super(Forbidden.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/Gone.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class Gone extends Exception {
static readonly STATUS = 410;
name: string = "GONE";

constructor(message: string) {
super(410, message);
constructor(message: string, origin?: Error | string | any) {
super(Gone.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/ImATeapot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class ImATeapot extends Exception {
static readonly STATUS = 418;
name: string = "IM_A_TEAPOT";

constructor(message: string) {
super(418, message);
constructor(message: string, origin?: Error | string | any) {
super(ImATeapot.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/LengthRequired.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class LengthRequired extends Exception {
static readonly STATUS = 411;
name: string = "LENGTH_REQUIRED";

constructor(message: string) {
super(411, message);
constructor(message: string, origin?: Error | string | any) {
super(LengthRequired.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/MethodNotAllowed.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class MethodNotAllowed extends Exception {
static readonly STATUS = 405;
name: string = "METHOD_NOT_ALLOWED";

constructor(message: string) {
super(405, message);
constructor(message: string, origin?: Error | string | any) {
super(MethodNotAllowed.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/MisdirectedRequest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class MisdirectedRequest extends Exception {
static readonly STATUS = 421;
name: string = "MISDIRECTED_REQUEST";

constructor(message: string) {
super(421, message);
constructor(message: string, origin?: Error | string | any) {
super(MisdirectedRequest.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/NotAcceptable.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class NotAcceptable extends Exception {
static readonly STATUS = 406;
name: string = "NOT_ACCEPTABLE";

constructor(message: string) {
super(406, "You must accept content-type " + message);
constructor(message: string, origin: Error | string | any = "You must accept content-type " + message) {
super(NotAcceptable.STATUS, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/NotFound.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class NotFound extends Exception {
static readonly STATUS = 404;
name: string = "NOT_FOUND";

constructor(message: string) {
super(404, message);
constructor(message: string, origin?: Error | string | any) {
super(NotFound.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/PaymentRequired.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class PaymentRequired extends Exception {
static readonly STATUS = 402;
name: string = "PAYMENT_REQUIRED";

constructor(message: string) {
super(402, message);
constructor(message: string, origin?: Error | string | any) {
super(PaymentRequired.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/PreconditionFailed.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class PreconditionFailed extends Exception {
static readonly STATUS = 412;
name: string = "PRECONDITION_FAILED";

constructor(message: string) {
super(412, message);
constructor(message: string, origin?: Error | string | any) {
super(PreconditionFailed.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/PreconditionRequired.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class PreconditionRequired extends Exception {
static readonly STATUS = 428;
name: string = "PRECONDITION_REQUIRED";

constructor(message: string) {
super(428, message);
constructor(message: string, origin?: Error | string | any) {
super(PreconditionRequired.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/ProxyAuthentificationRequired.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class ProxyAuthentificationRequired extends Exception {
static readonly STATUS = 407;
name: string = "PROXY_AUTHENTIFICATION_REQUIRED";

constructor(message: string) {
super(407, message);
constructor(message: string, origin?: Error | string | any) {
super(ProxyAuthentificationRequired.STATUS, message, origin);
}
}
5 changes: 3 additions & 2 deletions src/clientErrors/RequestEntityTooLarge.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Exception} from "../core/Exception";

export class RequestEntityTooLarge extends Exception {
static readonly STATUS = 413;
name: string = "REQUEST_ENTITY_TOO_LARGE";

constructor(message: string) {
super(413, message);
constructor(message: string, origin?: Error | string | any) {
super(RequestEntityTooLarge.STATUS, message, origin);
}
}
Loading