Skip to content

Commit

Permalink
Merge pull request #12558 from jmcdo29/feat/streamable-error-logger
Browse files Browse the repository at this point in the history
feat: allow for custom logger with streamable files
  • Loading branch information
kamilmysliwiec authored Oct 23, 2023
2 parents 2d8a848 + 80061af commit e6276d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/common/file-stream/streamable-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { types } from 'util';
import { HttpStatus } from '../enums';
import { isFunction } from '../utils/shared.utils';
import { StreamableFileOptions, StreamableHandlerResponse } from './interfaces';
import { Logger } from '../services';

/**
* @see [Streaming files](https://docs.nestjs.com/techniques/streaming-files)
Expand All @@ -11,6 +12,7 @@ import { StreamableFileOptions, StreamableHandlerResponse } from './interfaces';
*/
export class StreamableFile {
private readonly stream: Readable;
protected logger = new Logger('StreamableFile');

protected handleError: (
err: Error,
Expand All @@ -28,6 +30,10 @@ export class StreamableFile {
res.send(err.message);
};

protected logError: (err: Error) => void = (err: Error) => {
this.logger.error(err.message, err.stack);
};

constructor(buffer: Uint8Array, options?: StreamableFileOptions);
constructor(readable: Readable, options?: StreamableFileOptions);
constructor(
Expand Down Expand Up @@ -74,4 +80,13 @@ export class StreamableFile {
this.handleError = handler;
return this;
}

get errorLogger() {
return this.logError;
}

setErrorLogger(handler: (err: Error) => void) {
this.logError = handler;
return this;
}
}
2 changes: 1 addition & 1 deletion packages/platform-express/adapters/express-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class ExpressAdapter extends AbstractHttpAdapter<
response,
(err: Error) => {
if (err) {
this.logger.error(err.message, err.stack);
body.errorLogger(err);
}
},
);
Expand Down

0 comments on commit e6276d5

Please sign in to comment.