Skip to content

Commit

Permalink
Merge pull request #32 from contentstack/feat/DX-317-handle-variant-a…
Browse files Browse the repository at this point in the history
…pi-errors

feat: handled variant api errors
  • Loading branch information
aman19K authored May 9, 2024
2 parents cbc09b9 + d70f22d commit 1514232
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 97 deletions.
18 changes: 1 addition & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export default class ExportPersonalization {
}
} catch (error) {
this.exportConfig.personalizationEnabled = false;
log(this.exportConfig, `Failed to export Personalization project. ${formatError(error)}`, 'error');
log(this.exportConfig, error, 'error');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export default class ImportPersonalization {
}
}
} catch (error) {
this.config.modules.personalization.importData = false; // Stop personalization import if project creation fails
log(this.config, error, 'error');
if (!this.personalization.importData){
log(this.config, 'Skipping personalization migration...', 'warn')
}
}
}
}
10 changes: 3 additions & 7 deletions packages/contentstack-variants/src/export/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@ export default class ExportAttributes extends PersonalizationAdapter<ExportConfi

if (!this.attributes?.length) {
log(this.exportConfig, 'No Attributes found with the given project!', 'info');
return;
} else {
this.sanitizeAttribs();
fsUtil.writeFile(pResolve(this.attributesFolderPath, this.attributesConfig.fileName), this.attributes);
log(this.exportConfig, 'All the attributes have been exported successfully!', 'success');
return;
}
} catch (error: any) {
if (error?.errorMessage || error?.message || error?.error_message) {
log(this.exportConfig, `Failed to export attributes! ${formatError(error)}`, 'error');
}
throw error;
} catch (error) {
log(this.exportConfig, `Failed to export attributes!`, 'error');
log(this.config, error, 'error');
}
}

Expand Down
8 changes: 3 additions & 5 deletions packages/contentstack-variants/src/export/audiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ export default class ExportAudiences extends PersonalizationAdapter<ExportConfig
log(this.exportConfig, 'All the audiences have been exported successfully!', 'success');
return;
}
} catch (error: any) {
if (error?.errorMessage || error?.message || error?.error_message) {
log(this.exportConfig, `Failed to export audiences! ${formatError(error)}`, 'error');
}
throw error;
} catch (error) {
log(this.exportConfig, `Failed to export audiences!`, 'error');
log(this.config, error, 'error');
}
}

Expand Down
8 changes: 3 additions & 5 deletions packages/contentstack-variants/src/export/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ export default class ExportEvents extends PersonalizationAdapter<ExportConfig> {
log(this.exportConfig, 'All the events have been exported successfully!', 'success');
return;
}
} catch (error: any) {
if (error?.errorMessage || error?.message || error?.error_message) {
log(this.exportConfig, `Failed to export events! ${formatError(error)}`, 'error');
}
throw error;
} catch (error) {
log(this.exportConfig, `Failed to export events!`, 'error');
log(this.config, error, 'error');
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-variants/src/export/experiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
);
log(this.exportConfig, 'All the experiences have been exported successfully!', 'success');
} catch (error) {
log(this.exportConfig, `Failed to export experiences ${formatError(error)}`, 'error');
throw error;
log(this.exportConfig, `Failed to export experiences!`, 'error');
log(this.config, error, 'error');
}
}
}
4 changes: 2 additions & 2 deletions packages/contentstack-variants/src/export/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export default class ExportProjects extends PersonalizationAdapter<ExportConfig>
this.exportConfig.personalizationEnabled = false;
return;
}
this.exportConfig.project_id = project[0].uid;
this.exportConfig.personalizationEnabled = true;
this.exportConfig.project_id = project[0]?.uid;
fsUtil.writeFile(path.resolve(this.projectFolderPath, 'projects.json'), project);
log(this.exportConfig, 'Project exported successfully!', 'success');
} catch (error) {
log(this.exportConfig, `Failed to export projects ${formatError(error)}`, 'error');
log(this.exportConfig, `Failed to export projects!`, 'error');
throw error;
}
}
Expand Down
8 changes: 3 additions & 5 deletions packages/contentstack-variants/src/import/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ export default class Attribute extends PersonalizationAdapter<ImportConfig> {

fsUtil.writeFile(this.attributesUidMapperPath, this.attributesUidMapper);
this.log(this.config, this.$t(this.messages.CREATE_SUCCESS, { module: 'Attributes' }), 'info');
} catch (error: any) {
if (error?.errorMessage || error?.message || error?.error_message) {
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Attributes' }), 'error');
}
throw error;
} catch (error) {
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Attributes' }), 'error');
this.log(this.config, error, 'error');
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions packages/contentstack-variants/src/import/audiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ export default class Audiences extends PersonalizationAdapter<ImportConfig> {

fsUtil.writeFile(this.audiencesUidMapperPath, this.audiencesUidMapper);
this.log(this.config, this.$t(this.messages.CREATE_SUCCESS, { module: 'Audiences' }), 'info');
} catch (error: any) {
if (error?.errorMessage || error?.message || error?.error_message) {
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Audiences' }), 'error');
}
throw error;
} catch (error) {
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Audiences' }), 'error');
this.log(this.config, error, 'error');
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions packages/contentstack-variants/src/import/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ export default class Events extends PersonalizationAdapter<ImportConfig> {

fsUtil.writeFile(this.eventsUidMapperPath, this.eventsUidMapper);
this.log(this.config, this.$t(this.messages.CREATE_SUCCESS, { module: 'Events' }), 'info');
} catch (error: any) {
if (error?.errorMessage || error?.message || error?.error_message) {
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Events' }), 'error');
}
throw error;
} catch (error) {
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Events' }), 'error');
this.log(this.config, error, 'error');
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions packages/contentstack-variants/src/import/experiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,9 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
}

await this.createVariantIdMapper();
} catch (error: any) {
if (error?.errorMessage || error?.message || error?.error_message) {
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Experiences' }), 'error');
} else {
this.log(this.config, error, 'error');
}
} catch (error) {
this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Experiences' }), 'error');
this.log(this.config, error, 'error');
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import memoize from 'lodash/memoize';

const errors = {
CREATE_FAILURE: '{module} created failed!',
CREATE_FAILURE: '{module} creation failed!',
};

const commonMsg = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export type ExperienceStruct = {
_cms?: {
variantGroup: object;
variants: Record<string, string>;
}
};
} & AnyProperty;

export interface CreateExperienceInput {
Expand All @@ -138,6 +138,25 @@ export interface CMSExperienceStruct {
contentTypes: string[];
}

export type VariantAPIRes =
| ProjectStruct[]
| ProjectStruct
| AttributeStruct[]
| AttributeStruct
| ExperienceStruct[]
| EventStruct[]
| AudienceStruct[]
| ExperienceStruct
| EventStruct
| AudienceStruct
| CMSExperienceStruct
| Error;

export interface APIResponse {
status: number;
data: any;
}

export interface Personalization<T> extends AdapterHelperInterface<T, HttpClient> {
projects(options: GetProjectsParams): Promise<ProjectStruct[] | void>;

Expand All @@ -157,5 +176,7 @@ export interface Personalization<T> extends AdapterHelperInterface<T, HttpClient

getCTsFromExperience(experienceUid: string): Promise<CMSExperienceStruct | void>;

updateCTsInExperience(experience: UpdateExperienceInput, experienceUid: string): Promise<CMSExperienceStruct | void>;
updateCTsInExperience(experience: UpdateExperienceInput, experienceUid: string): Promise<CMSExperienceStruct | void>;

handleVariantAPIRes(res: any): VariantAPIRes;
}
Loading

0 comments on commit 1514232

Please sign in to comment.