Skip to content

Commit 7f134f3

Browse files
committed
MC-5691: Implement better developer error reporting
- Add better error reporting into factories
1 parent 31b6a9e commit 7f134f3

File tree

11 files changed

+178
-89
lines changed

11 files changed

+178
-89
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type-factory.js

Lines changed: 21 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/master-factory.js

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/observable-updater-factory.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/preview-factory.js

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/stage-builder.js

Lines changed: 24 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type-factory.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,42 @@ export default function createContentType(
3434
data: object = {},
3535
childrenLength: number = 0,
3636
): Promise<ContentTypeInterface | ContentTypeCollectionInterface> {
37-
return new Promise((resolve: (contentType: ContentTypeInterface | ContentTypeCollectionInterface) => void) => {
37+
return new Promise(
38+
(resolve: (contentType: ContentType | ContentTypeCollection) => void,
39+
reject: (error: string) => void
40+
)=> {
3841
loadModule([config.component], (contentTypeComponent: typeof ContentType | typeof ContentTypeCollection) => {
39-
const contentType = new contentTypeComponent(
40-
parent,
41-
config,
42-
stageId,
43-
);
44-
Promise.all(
45-
[
46-
previewFactory(contentType, config),
47-
masterFactory(contentType, config),
48-
],
49-
).then(([previewComponent, masterComponent]) => {
50-
contentType.preview = previewComponent;
51-
contentType.content = masterComponent;
52-
contentType.dataStore.update(
53-
prepareData(config, data),
42+
try {
43+
const contentType = new contentTypeComponent(
44+
parent,
45+
config,
46+
stageId,
5447
);
55-
resolve(contentType);
56-
});
48+
Promise.all(
49+
[
50+
previewFactory(contentType, config),
51+
masterFactory(contentType, config),
52+
],
53+
).then(([previewComponent, masterComponent]) => {
54+
contentType.preview = previewComponent;
55+
contentType.content = masterComponent;
56+
contentType.dataStore.update(
57+
prepareData(config, data),
58+
);
59+
resolve(contentType);
60+
}).catch((error) => {
61+
reject(error);
62+
});
63+
} catch (error) {
64+
reject(`Error within component (${config.component}) for ${config.name}.`);
65+
console.error(error);
66+
}
67+
}, (error: string) => {
68+
reject(`Unable to load component (${config.component}) for ${config.name}. Please check component exists`
69+
+ ` and content type configuration is correct.`);
70+
console.error(error);
5771
});
58-
}).then((contentType: ContentTypeInterface | ContentTypeCollectionInterface) => {
72+
}).then((contentType) => {
5973
events.trigger("contentType:createAfter", {id: contentType.id, contentType});
6074
events.trigger(config.name + ":createAfter", {id: contentType.id, contentType});
6175
fireContentTypeReadyEvent(contentType, childrenLength);
@@ -94,10 +108,10 @@ function prepareData(config: ContentTypeConfigInterface, data: {}) {
94108
/**
95109
* A content type is ready once all of its children have mounted
96110
*
97-
* @param {ContentType} contentType
111+
* @param {ContentType | ContentTypeCollection} contentType
98112
* @param {number} childrenLength
99113
*/
100-
function fireContentTypeReadyEvent(contentType: ContentTypeInterface, childrenLength: number) {
114+
function fireContentTypeReadyEvent(contentType: ContentType | ContentTypeCollection, childrenLength: number) {
101115
const fire = () => {
102116
const params = {id: contentType.id, contentType, expectChildren: childrenLength};
103117
events.trigger("contentType:mountAfter", params);

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/master-factory.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import loadModule from "Magento_PageBuilder/js/utils/loader";
77
import ContentTypeConfigInterface from "../content-type-config.d";
88
import ContentTypeInterface from "../content-type.d";
9+
import ContentTypeCollectionInterface from "../content-type-collection.d";
910
import converterResolver from "./converter-resolver";
1011
import Master from "./master";
1112
import MasterCollection from "./master-collection";
@@ -14,26 +15,37 @@ import observableUpdaterFactory from "./observable-updater-factory";
1415
/**
1516
* Create new content instance
1617
*
17-
* @param {ContentTypeInterface} contentType
18+
* @param {ContentTypeInterface | ContentTypeCollectionInterface} contentType
1819
* @param {ContentTypeConfigInterface} config
1920
* @returns {Promise<ContentTypeInterface>}
2021
*/
2122
export default function create(
22-
contentType: ContentTypeInterface,
23+
contentType: ContentTypeInterface | ContentTypeCollectionInterface,
2324
config: ContentTypeConfigInterface,
2425
): Promise<Master | MasterCollection> {
25-
return new Promise((resolve: (masterComponent: Master | MasterCollection) => void) => {
26+
return new Promise(
27+
(resolve: (masterComponent: Master | MasterCollection) => void, reject: (error: string) => void
28+
) => {
2629
observableUpdaterFactory(config, converterResolver).then((observableUpdater) => {
27-
loadModule([config.master_component], (contentComponent: typeof Master | typeof MasterCollection) => {
28-
resolve(
29-
new contentComponent(
30+
loadModule([config.master_component], (masterComponent: typeof Master | typeof MasterCollection) => {
31+
try {
32+
const master = new masterComponent(
3033
contentType,
3134
observableUpdater,
32-
),
33-
);
35+
);
36+
resolve(master);
37+
} catch (error) {
38+
reject(`Error within master component (${config.master_component}) for ${config.name}.`);
39+
console.error(error);
40+
}
41+
}, (error: Error) => {
42+
reject(`Unable to load preview component (${config.master_component}) for ${config.name}. Please ` +
43+
`check preview component exists and content type configuration is correct.`);
44+
console.error(error);
3445
});
3546
}).catch((error) => {
3647
console.error(error);
48+
return null;
3749
});
3850
});
3951
}

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/observable-updater-factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default function create(
3535
);
3636
}).catch((error) => {
3737
console.error(error);
38+
return null;
3839
});
3940
});
4041
}

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/preview-factory.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import loadModule from "Magento_PageBuilder/js/utils/loader";
77
import ContentTypeConfigInterface from "../content-type-config.d";
88
import ContentTypeInterface from "../content-type.d";
9+
import ContentTypeCollectionInterface from "../content-type-collection.d";
910
import observableUpdaterFactory from "./observable-updater-factory";
1011
import Preview from "./preview";
1112
import PreviewCollection from "./preview-collection";
@@ -14,27 +15,38 @@ import previewConverterResolver from "./preview-converter-resolver";
1415
/**
1516
* Create new preview instance
1617
*
17-
* @param {ContentTypeInterface} contentType
18+
* @param {ContentTypeInterface | ContentTypeCollectionInterface} contentType
1819
* @param {ContentTypeConfigInterface} config
19-
* @returns {Promise<ContentTypeInterface>}
20+
* @returns {Promise<Preview | PreviewCollection>}
2021
*/
2122
export default function create(
22-
contentType: ContentTypeInterface,
23+
contentType: ContentTypeInterface | ContentTypeCollectionInterface,
2324
config: ContentTypeConfigInterface,
2425
): Promise<Preview | PreviewCollection> {
25-
return new Promise((resolve: (previewComponent: Preview | PreviewCollection) => void) => {
26+
return new Promise(
27+
(resolve: (previewComponent: Preview | PreviewCollection) => void, reject: (e: string) => void
28+
) => {
2629
observableUpdaterFactory(config, previewConverterResolver).then((observableUpdater) => {
2730
loadModule([config.preview_component], (previewComponent: typeof Preview | typeof PreviewCollection) => {
28-
resolve(
29-
new previewComponent(
31+
try {
32+
const preview = new previewComponent(
3033
contentType,
3134
config,
3235
observableUpdater,
33-
),
34-
);
36+
);
37+
resolve(preview);
38+
} catch (error) {
39+
reject(`Error within preview component (${config.preview_component}) for ${config.name}.`);
40+
console.error(error);
41+
}
42+
}, (error: string) => {
43+
reject(`Unable to load preview component (${config.preview_component}) for ${config.name}. Please ` +
44+
`check preview component exists and content type configuration is correct.`);
45+
console.error(error);
3546
});
3647
}).catch((error) => {
3748
console.error(error);
49+
return null;
3850
});
3951
});
4052
}

0 commit comments

Comments
 (0)