Skip to content

Commit

Permalink
bugfix(@nestjs/common) populate multer exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 21, 2018
1 parent 9bfb941 commit b20b3cb
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 36 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 4.6.4
### Bug Fixes
- **common**: logger overrides custom logger scope [#435](https://github.com/nestjs/nest/issues/435)
- **common**: `FileInterceptor` supports only one options at once [#429](https://github.com/nestjs/nest/issues/429)
- **common**: support `symbol` as a token (`NestApplicationContext`)
- **core**: fix exception handler (exceception thrown in the `done()` callback) [#431](https://github.com/nestjs/nest/issues/431)
- **core**: incorrect HTTP response on `SyntaxError` [#430](https://github.com/nestjs/nest/issues/430)
- **microservices**: can't select/get from context when using `NestFactory.createMicroservice` [#398](https://github.com/nestjs/nest/issues/398)

## 4.6.3
Expand Down
8 changes: 1 addition & 7 deletions lib/common/interceptors/file.interceptor.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
import { Observable } from 'rxjs/Observable';
import { MulterOptions } from '../interfaces/external/multer-options.interface';
export declare function FileInterceptor(fieldName: string, options?: MulterOptions): {
new (): {
readonly upload: any;
intercept(request: any, context: any, stream$: Observable<any>): Promise<Observable<any>>;
};
};
export declare function FileInterceptor(fieldName: string, options?: MulterOptions): any;
6 changes: 3 additions & 3 deletions lib/common/interceptors/file.interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", { value: true });
const multer = require("multer");
const component_decorator_1 = require("../decorators/core/component.decorator");
function FileInterceptor(fieldName, options) {
const Interceptor = class {
return component_decorator_1.mixin(class {
constructor() {
this.upload = multer(options);
}
Expand All @@ -20,7 +21,6 @@ function FileInterceptor(fieldName, options) {
return stream$;
});
}
};
return Interceptor;
});
}
exports.FileInterceptor = FileInterceptor;
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export interface INestApplicationContext {
* Makes possible to retrieve the instance of the component or controller available inside the processed module.
* @returns T
*/
get<T>(metatypeOrToken: Metatype<T> | string): T;
get<T>(metatypeOrToken: Metatype<T> | string | symbol): T;
}
6 changes: 3 additions & 3 deletions lib/common/services/logger.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class Logger {
}
log(message) {
const { logger } = Logger;
logger.log(message, this.context, this.isTimeDiffEnabled);
logger.log.call(logger, message, this.context, this.isTimeDiffEnabled);
}
error(message, trace = '') {
const { logger } = Logger;
logger.error(message, trace, this.context, this.isTimeDiffEnabled);
logger.error.call(logger, message, trace, this.context, this.isTimeDiffEnabled);
}
warn(message) {
const { logger } = Logger;
logger.warn(message, this.context, this.isTimeDiffEnabled);
logger.warn.call(logger, message, this.context, this.isTimeDiffEnabled);
}
static overrideLogger(logger) {
this.logger = logger;
Expand Down
1 change: 1 addition & 0 deletions lib/core/nest-application-context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export declare class NestApplicationContext implements INestApplicationContext {
protected contextModule: any;
private readonly moduleTokenFactory;
constructor(container: NestContainer, scope: NestModuleMetatype[], contextModule: any);
selectContextModule(): void;
select<T>(module: Metatype<T>): INestApplicationContext;
get<T>(metatypeOrToken: Metatype<T> | string | symbol): T;
private findInstanceByPrototypeOrToken<T>(metatypeOrToken);
Expand Down
4 changes: 4 additions & 0 deletions lib/core/nest-application-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class NestApplicationContext {
this.contextModule = contextModule;
this.moduleTokenFactory = new module_token_factory_1.ModuleTokenFactory();
}
selectContextModule() {
const modules = this.container.getModules().values();
this.contextModule = modules.next().value;
}
select(module) {
const modules = this.container.getModules();
const moduleMetatype = this.contextModule.metatype;
Expand Down
1 change: 0 additions & 1 deletion lib/core/nest-application.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export declare class NestApplication extends NestApplicationContext implements I
private readonly microservices;
private isInitialized;
constructor(container: NestContainer, express: any, config: ApplicationConfig, appOptions?: NestApplicationOptions);
selectContextModule(): void;
applyOptions(): any;
createServer(): any;
setupModules(): Promise<void>;
Expand Down
4 changes: 0 additions & 4 deletions lib/core/nest-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ class NestApplication extends nest_application_context_1.NestApplicationContext
this.config.setIoAdapter(ioAdapter);
this.routesResolver = new routes_resolver_1.RoutesResolver(container, express_adapter_1.ExpressAdapter, this.config);
}
selectContextModule() {
const modules = this.container.getModules().values();
this.contextModule = modules.next().value;
}
applyOptions() {
if (!this.appOptions) {
return undefined;
Expand Down
1 change: 1 addition & 0 deletions lib/core/router/routes-resolver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export declare class RoutesResolver implements Resolver {
setupRouters(routes: Map<string, InstanceWrapper<Controller>>, moduleName: string, modulePath: string, express: Application): void;
setupNotFoundHandler(express: Application): void;
setupExceptionHandler(express: Application): void;
mapExternalException(err: any): any;
}
11 changes: 10 additions & 1 deletion lib/core/router/routes-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class RoutesResolver {
this.setupRouters(routes, moduleName, path, router);
});
this.setupNotFoundHandler(router);
this.setupExceptionHandler(router);
this.setupExceptionHandler(express);
}
setupRouters(routes, moduleName, modulePath, express) {
Expand All @@ -48,11 +49,19 @@ class RoutesResolver {
}
setupExceptionHandler(express) {
const callback = (err, req, res, next) => {
throw err;
throw this.mapExternalException(err);
};
const exceptionHandler = this.routerExceptionsFilter.create({}, callback);
const proxy = this.routerProxy.createExceptionLayerProxy(callback, exceptionHandler);
express.use(proxy);
}
mapExternalException(err) {
switch (true) {
case (err instanceof SyntaxError):
return new common_1.BadRequestException(err.message);
default:
return err;
}
}
}
exports.RoutesResolver = RoutesResolver;
1 change: 1 addition & 0 deletions lib/microservices/nest-microservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class NestMicroservice extends nest_application_context_1.NestApplicationContext
this.server = strategy
? strategy
: server_factory_1.ServerFactory.create(this.microserviceConfig);
this.selectContextModule();
}
setupModules() {
this.socketModule && this.socketModule.setup(this.container, this.applicationConfig);
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": "nestjs",
"version": "4.6.3",
"version": "4.6.4",
"description": "Modern, fast, powerful node.js web framework",
"main": "index.js",
"scripts": {
Expand Down
33 changes: 20 additions & 13 deletions src/common/interceptors/file.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ import { MulterOptions } from '../interfaces/external/multer-options.interface';
import { mixin } from '../decorators/core/component.decorator';

export function FileInterceptor(fieldName: string, options?: MulterOptions) {
return mixin(class implements NestInterceptor {
readonly upload = multer(options);
return mixin(
class implements NestInterceptor {
readonly upload = multer(options);

async intercept(
request,
context,
stream$: Observable<any>,
): Promise<Observable<any>> {
await new Promise((resolve, reject) =>
this.upload.single(fieldName)(request, request.res, resolve),
);
return stream$;
}
});
async intercept(
request,
context,
stream$: Observable<any>,
): Promise<Observable<any>> {
await new Promise((resolve, reject) =>
this.upload.single(fieldName)(request, request.res, err => {
if (err) {
return reject(err);
}
resolve();
}),
);
return stream$;
}
},
);
}
7 changes: 6 additions & 1 deletion src/common/interceptors/files.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export function FilesInterceptor(fieldName: string, maxCount?: number, options?:
stream$: Observable<any>,
): Promise<Observable<any>> {
await new Promise((resolve, reject) =>
this.upload.array(fieldName, maxCount)(request, request.res, resolve),
this.upload.array(fieldName, maxCount)(request, request.res, err => {
if (err) {
return reject(err);
}
resolve();
}),
);
return stream$;
}
Expand Down
1 change: 0 additions & 1 deletion src/core/middlewares/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class MiddlewareBuilder implements MiddlewaresConsumer {
if (isUndefined(middlewares) || isUndefined(forRoutes)) {
throw new InvalidMiddlewareConfigurationException();
}

this.middlewaresCollection.add(configuration);
return this;
}
Expand Down

0 comments on commit b20b3cb

Please sign in to comment.