Skip to content

Commit

Permalink
Merge pull request #5 from yldrmzffr/fix/async-error
Browse files Browse the repository at this point in the history
fix(async error): fixed Async Exception handler and added async tests
  • Loading branch information
yldrmzffr authored Nov 2, 2023
2 parents 44519ae + b5e356d commit 63db545
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
11 changes: 6 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ export class MuzuServer {
statusCode: number,
body: Object
): Promise<void> {
if (body instanceof Promise) {
body = await body;
}

res.writeHead(statusCode, {'Content-Type': 'application/json'});
res.end(JSON.stringify(body));
console.log('📤 Response', {statusCode, body});
Expand Down Expand Up @@ -72,7 +68,12 @@ export class MuzuServer {
throw new BadRequestException('Error parsing body', err.details);
}

const result = route.handler(req, res);
let result: Object = route.handler(req, res);

if (result instanceof Promise) {
result = await result;
}

const statusCode = res.statusCode || 200;
return this.sendResponse(res, statusCode, result);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "muzu",
"version": "0.0.5",
"version": "0.0.6",
"description": "",
"main": "build/lib/index.js",
"types": "build/lib/index.d.ts",
Expand Down
42 changes: 41 additions & 1 deletion test/methods.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {MuzuServer, Request} from '../lib';
import {MuzuServer, Request, HttpException} from '../lib';
import * as request from 'supertest';

const muzuServer = new MuzuServer();
Expand Down Expand Up @@ -30,6 +30,26 @@ class TestController {
helloPatch() {
return {message: 'Patch Method Called'};
}

asyncReturn() {
return new Promise(resolve => {
setTimeout(() => {
resolve({message: 'Async Method Called'});
}, 300);
});
}

@Post('/async-hello')
async asyncHello() {
const result = await this.asyncReturn();
console.log('result', result);
return result;
}

@Post('/async-error')
async asyncError() {
throw new HttpException(400, 'Async Error');
}
}

const port = 3000;
Expand Down Expand Up @@ -93,6 +113,26 @@ describe('MuzuServer', () => {
status: 400,
});
});

it('should return 200 on POST /api/async-hello', async () => {
const res = await request(muzuServer.server)
.post('/api/async-hello')
.send({name: 'Muzu'});
expect(res.status).toEqual(200);
expect(res.body).toEqual({message: 'Async Method Called'});
});

it('should return 400 on POST /api/async-error', async () => {
const res = await request(muzuServer.server)
.post('/api/async-error')
.send({name: 'Muzu'});
expect(res.status).toEqual(400);
expect(res.body).toEqual({
kind: 'MuzuException',
message: 'Async Error',
status: 400,
});
});
});

muzuServer.stop(() => {
Expand Down

0 comments on commit 63db545

Please sign in to comment.