Skip to content

Commit

Permalink
fix(core): Gracefully handle errors in creating asset previews
Browse files Browse the repository at this point in the history
Fixes #1246
  • Loading branch information
michaelbromley committed Dec 2, 2021
1 parent d34782b commit c3cfcb3
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions packages/core/src/service/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { omit } from '@vendure/common/lib/omit';
import { ID, PaginatedList, Type } from '@vendure/common/lib/shared-types';
import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
import { unique } from '@vendure/common/lib/unique';
import { ReadStream } from 'fs-extra';
import { ReadStream as FSReadStream } from 'fs';
import { ReadStream } from 'fs-extra';
import mime from 'mime-types';
import path from 'path';
import { Readable, Stream } from 'stream';
Expand Down Expand Up @@ -250,13 +250,13 @@ export class AssetService {
stream.on('error', (err: any) => {
reject(err);
});
const result = await this.createAssetInternal(
ctx,
stream,
filename,
mimetype,
input.customFields,
);
let result: Asset | MimeTypeError;
try {
result = await this.createAssetInternal(ctx, stream, filename, mimetype, input.customFields);
} catch (e) {
reject(e);
return;
}
if (isGraphQlErrorResult(result)) {
resolve(result);
return;
Expand Down Expand Up @@ -394,7 +394,8 @@ export class AssetService {
stream: ReadStream | Readable,
maybeFilePath?: string,
): Promise<CreateAssetResult> {
const filePath = stream instanceof ReadStream || stream instanceof FSReadStream ? stream.path : maybeFilePath;
const filePath =
stream instanceof ReadStream || stream instanceof FSReadStream ? stream.path : maybeFilePath;
if (typeof filePath === 'string') {
const filename = path.basename(filePath);
const mimetype = mime.lookup(filename) || 'application/octet-stream';
Expand Down Expand Up @@ -460,7 +461,7 @@ export class AssetService {
try {
preview = await assetPreviewStrategy.generatePreviewImage(ctx, mimetype, sourceFile);
} catch (e) {
Logger.error(`Could not create Asset preview image: ${e.message}`);
Logger.error(`Could not create Asset preview image: ${e.message}`, undefined, e.stack);
throw e;
}
const previewFileIdentifier = await assetStorageStrategy.writeFileFromBuffer(
Expand Down

0 comments on commit c3cfcb3

Please sign in to comment.