Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove attempt to get a more comprehensive error #7270

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ describe('gitlab backend', () => {
expect(entries).toEqual({
cursor: expect.any(Cursor),
pagination: 1,
errors: [],
entries: expect.arrayContaining(
tree.map(file => expect.objectContaining({ path: file.path })),
),
Expand All @@ -445,7 +446,7 @@ describe('gitlab backend', () => {
tree.forEach(file => interceptFiles(backend, file.path));

interceptCollection(backend, collectionManyEntriesConfig, { repeat: 5 });
const entries = await backend.listAllEntries(fromJS(collectionManyEntriesConfig));
const { entries } = await backend.listAllEntries(fromJS(collectionManyEntriesConfig));

expect(entries).toEqual(
expect.arrayContaining(tree.map(file => expect.objectContaining({ path: file.path }))),
Expand All @@ -463,6 +464,7 @@ describe('gitlab backend', () => {
entries: expect.arrayContaining(
files.map(file => expect.objectContaining({ path: file.file })),
),
errors: [],
});
expect(entries.entries).toHaveLength(2);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/decap-cms-core/src/__tests__/backend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,15 +655,15 @@ describe('Backend', () => {
backend = new Backend(implementation, { config: {}, backendName: 'github' });
backend.listAllEntries = jest.fn(collection => {
if (collection.get('name') === 'posts') {
return Promise.resolve(posts);
return Promise.resolve({ entries: posts });
}
if (collection.get('name') === 'pages') {
return Promise.resolve(pages);
return Promise.resolve({ entries: pages });
}
if (collection.get('name') === 'files') {
return Promise.resolve(files);
return Promise.resolve({ entries: files });
}
return Promise.resolve([]);
return Promise.resolve({ entries: [] });
});
});

Expand Down
18 changes: 16 additions & 2 deletions packages/decap-cms-core/src/actions/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export async function getAllEntries(state: State, collection: Collection) {
const provider: Backend = integration
? getIntegrationProvider(state.integrations, backend.getToken, integration)
: backend;
const entries = await provider.listAllEntries(collection);
const { entries } = await provider.listAllEntries(collection);
return entries;
}

Expand Down Expand Up @@ -595,9 +595,10 @@ export function loadEntries(collection: Collection, page = 0) {
cursor: Cursor;
pagination: number;
entries: EntryValue[];
errors?: string[];
} = await (loadAllEntries
? // nested collections require all entries to construct the tree
provider.listAllEntries(collection).then((entries: EntryValue[]) => ({ entries }))
provider.listAllEntries(collection)
: provider.listEntries(collection, page));
response = {
...response,
Expand All @@ -616,6 +617,19 @@ export function loadEntries(collection: Collection, page = 0) {
: Cursor.create(response.cursor),
};

response.errors?.forEach(error => {
dispatch(
addNotification({
message: {
details: error,
key: 'ui.toast.duplicateFrontmatterKey',
},
type: 'warning',
dismissAfter: 8000,
}),
);
});

dispatch(
entriesLoaded(
collection,
Expand Down
37 changes: 26 additions & 11 deletions packages/decap-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { attempt, flatten, isError, uniq, trim, sortBy, get, set } from 'lodash';
import { flatten, isError, uniq, trim, sortBy, get, set, attempt } from 'lodash';
import { List, fromJS, Set } from 'immutable';
import * as fuzzy from 'fuzzy';
import {
Expand Down Expand Up @@ -514,7 +514,12 @@ export class Backend {
},
),
);

const formattedEntries = entries.map(this.entryWithFormat(collection));
const errors = formattedEntries
.filter(e => e.parseError)
.map(e => `${e.parseError}. In ${e.path}`);

// If this collection has a "filter" property, filter entries accordingly
const collectionFilter = collection.get('filter');
const filteredEntries = collectionFilter
Expand All @@ -524,10 +529,9 @@ export class Backend {
if (hasI18n(collection)) {
const extension = selectFolderEntryExtension(collection);
const groupedEntries = groupEntries(collection, extension, filteredEntries);
return groupedEntries;
return { entries: groupedEntries, errors };
}

return filteredEntries;
return { entries: filteredEntries, errors };
}

async listEntries(collection: Collection) {
Expand Down Expand Up @@ -567,10 +571,14 @@ export class Backend {
cursorType: 'collectionEntries',
collection,
});

const { entries, errors } = this.processEntries(loadedEntries, collection);

return {
entries: this.processEntries(loadedEntries, collection),
entries,
pagination: cursor.meta?.get('page'),
cursor,
errors,
};
}

Expand All @@ -594,14 +602,17 @@ export class Backend {
}

const response = await this.listEntries(collection);
const { entries } = response;
const { entries, errors } = response;
let { cursor } = response;
while (cursor && cursor.actions!.includes('next')) {
const { entries: newEntries, cursor: newCursor } = await this.traverseCursor(cursor, 'next');
entries.push(...newEntries);
cursor = newCursor;
}
return entries;
return {
entries,
errors,
};
}

async search(collections: Collection[], searchTerm: string) {
Expand Down Expand Up @@ -639,7 +650,7 @@ export class Backend {
];
}
const filteredSearchFields = searchFields.filter(Boolean) as string[];
const collectionEntries = await this.listAllEntries(collection);
const collectionEntries = (await this.listAllEntries(collection)).entries;
return fuzzy.filter(searchTerm, collectionEntries, {
extract: extractSearchFields(uniq(filteredSearchFields)),
});
Expand Down Expand Up @@ -673,7 +684,7 @@ export class Backend {
file?: string,
limit?: number,
) {
let entries = await this.listAllEntries(collection);
let { entries } = await this.listAllEntries(collection);
if (file) {
entries = entries.filter(e => e.slug === file);
}
Expand Down Expand Up @@ -703,7 +714,7 @@ export class Backend {
const collection = data.get('collection') as Collection;
return this.implementation!.traverseCursor!(unwrappedCursor, action).then(
async ({ entries, cursor: newCursor }) => ({
entries: this.processEntries(entries, collection),
entries: this.processEntries(entries, collection).entries,
cursor: Cursor.create(newCursor).wrapData({
cursorType: 'collectionEntries',
collection,
Expand Down Expand Up @@ -870,7 +881,11 @@ export class Backend {
const format = resolveFormat(collection, entry);
if (entry && entry.raw !== undefined) {
const data = (format && attempt(format.fromFile.bind(format, entry.raw))) || {};
if (isError(data)) console.error(data);
if (isError(data)) {
console.warn(data.message, '\n', data.stack);
entry = Object.assign(entry, { parseError: data.message });
}

return Object.assign(entry, { data: isError(data) ? {} : data });
}
return format.fromFile(entry);
Expand Down
1 change: 1 addition & 0 deletions packages/decap-cms-core/src/valueObjects/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface EntryValue {
updatedOn: string;
status?: string;
meta: { path?: string };
parseError?: string;
i18n?: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[locale: string]: any;
Expand Down
1 change: 1 addition & 0 deletions packages/decap-cms-locales/src/en/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ const en = {
onFailToDelete: 'Failed to delete entry: %{details}',
onFailToUpdateStatus: 'Failed to update status: %{details}',
missingRequiredField: "Oops, you've missed a required field. Please complete before saving.",
duplicateFrontmatterKey: 'Duplicate key in frontmatter. %{details}',
entrySaved: 'Entry saved',
entryPublished: 'Entry published',
entryUnpublished: 'Entry unpublished',
Expand Down
Loading