Skip to content

Commit

Permalink
Merge pull request #375 from nestjsx/fix/request-exceptions
Browse files Browse the repository at this point in the history
fix(request): request validation error code 400
  • Loading branch information
michaelyali authored Dec 28, 2019
2 parents 7a18f33 + f8c5921 commit 9639da5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
51 changes: 32 additions & 19 deletions packages/crud/src/interceptors/crud-request.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {
BadRequestException,
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { RequestQueryParser, SCondition, QueryFilter } from '@nestjsx/crud-request';
import {
RequestQueryException,
RequestQueryParser,
SCondition,
QueryFilter,
} from '@nestjsx/crud-request';
import { isNil, isFunction, isArrayFull, hasLength } from '@nestjsx/util';

import { PARSED_CRUD_REQUEST_KEY } from '../constants';
Expand All @@ -19,27 +25,34 @@ export class CrudRequestInterceptor extends CrudBaseInterceptor
intercept(context: ExecutionContext, next: CallHandler) {
const req = context.switchToHttp().getRequest();

/* istanbul ignore else */
if (!req[PARSED_CRUD_REQUEST_KEY]) {
const { ctrlOptions, crudOptions, action } = this.getCrudInfo(context);
const parser = RequestQueryParser.create();

parser.parseQuery(req.query);

if (!isNil(ctrlOptions)) {
const search = this.getSearch(parser, crudOptions, action, req.params);
const auth = this.getAuth(parser, crudOptions, req);
parser.search = auth.or
? { $or: [auth.or, { $and: search }] }
: { $and: [auth.filter, ...search] };
} else {
parser.search = { $and: this.getSearch(parser, crudOptions, action) };
try {
/* istanbul ignore else */
if (!req[PARSED_CRUD_REQUEST_KEY]) {
const { ctrlOptions, crudOptions, action } = this.getCrudInfo(context);
const parser = RequestQueryParser.create();

parser.parseQuery(req.query);

if (!isNil(ctrlOptions)) {
const search = this.getSearch(parser, crudOptions, action, req.params);
const auth = this.getAuth(parser, crudOptions, req);
parser.search = auth.or
? { $or: [auth.or, { $and: search }] }
: { $and: [auth.filter, ...search] };
} else {
parser.search = { $and: this.getSearch(parser, crudOptions, action) };
}

req[PARSED_CRUD_REQUEST_KEY] = this.getCrudRequest(parser, crudOptions);
}

req[PARSED_CRUD_REQUEST_KEY] = this.getCrudRequest(parser, crudOptions);
return next.handle();
} catch (error) {
/* istanbul ignore next */
throw error instanceof RequestQueryException
? new BadRequestException(error.message)
: error;
}

return next.handle();
}

getCrudRequest(
Expand Down
12 changes: 6 additions & 6 deletions packages/crud/test/crud.decorator.base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ describe('#crud', () => {
return request(server)
.get('/test')
.query(query)
.expect(500)
.end((_, res) => {
const expected = { statusCode: 400, message: 'Invalid filter value' };
expect(res.status).toEqual(400);
expect(res.body).toMatchObject(expected);
done();
});
Expand All @@ -73,12 +73,12 @@ describe('#crud', () => {
it('should return status 400', (done) => {
return request(server)
.get('/test/invalid')
.expect(400)
.end((_, res) => {
const expected = {
statusCode: 400,
message: 'Invalid param id. Number expected',
};
expect(res.status).toEqual(400);
expect(res.body).toMatchObject(expected);
done();
});
Expand Down Expand Up @@ -107,8 +107,8 @@ describe('#crud', () => {
return request(server)
.post('/test')
.send(send)
.expect(400)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('age');
done();
});
Expand Down Expand Up @@ -145,8 +145,8 @@ describe('#crud', () => {
return request(server)
.post('/test/bulk')
.send(send)
.expect(400)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('bulk');
done();
});
Expand Down Expand Up @@ -176,8 +176,8 @@ describe('#crud', () => {
return request(server)
.put('/test/1')
.send(send)
.expect(400)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('id');
done();
});
Expand Down Expand Up @@ -207,8 +207,8 @@ describe('#crud', () => {
return request(server)
.patch('/test/1')
.send(send)
.expect(400)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('id');
done();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/crud/test/crud.decorator.override.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ describe('#crud', () => {
return request(server)
.get('/test')
.query(query)
.expect(500)
.end((_, res) => {
const expected = { statusCode: 400, message: 'Invalid filter value' };
expect(res.status).toEqual(400);
expect(res.body).toMatchObject(expected);
done();
});
Expand Down Expand Up @@ -124,8 +124,8 @@ describe('#crud', () => {
return request(server)
.post('/test/bulk')
.send(send)
.expect(400)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('bulk');
done();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/crud/test/crud.dto.options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ describe('#crud', () => {
return request(server)
.post('/test')
.send(send)
.expect(400)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('age');
done();
});
Expand Down Expand Up @@ -97,8 +97,8 @@ describe('#crud', () => {
return request(server)
.patch('/test/1')
.send(send)
.expect(400)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('email');
done();
});
Expand Down

0 comments on commit 9639da5

Please sign in to comment.