Skip to content

Commit 72b8aba

Browse files
Merge pull request #100 from Four-Lights-NL/fix/model-resolving-not-working
fix(deep-populate): gracefully handle missing model
2 parents 9384862 + 37e7035 commit 72b8aba

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.11.0",
2+
"version": "1.11.1-rc.0",
33
"keywords": [
44
"strapi",
55
"strapi-plugin",

server/src/services/cache.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import isEqual from "lodash/isEqual"
77
import type { PopulateParams } from "./populate"
88

99
import { version } from "../../../package.json"
10+
import log from "../utils/log"
1011
import { getConfig } from "./deep-populate/utils"
1112

1213
type SetPopulateParams = PopulateParams &
@@ -46,14 +47,19 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
4647

4748
const entry = await documentService.findFirst({ filters: { hash: { $eq: hash } } })
4849

49-
return entry
50-
? await documentService.update({
51-
documentId: entry.documentId,
52-
data: { populate, dependencies: dependencies.join(",") } as Partial<
53-
Modules.Documents.Params.Data.Input<"plugin::deep-populate.cache">
54-
>,
55-
})
56-
: await documentService.create({ data: { hash, params, populate, dependencies: dependencies.join(",") } })
50+
try {
51+
return entry
52+
? await documentService.update({
53+
documentId: entry.documentId,
54+
data: { populate, dependencies: dependencies.join(",") } as Partial<
55+
Modules.Documents.Params.Data.Input<"plugin::deep-populate.cache">
56+
>,
57+
})
58+
: await documentService.create({ data: { hash, params, populate, dependencies: dependencies.join(",") } })
59+
} catch (error: unknown) {
60+
log.error("[Plugin: Deep Populate] Failed to save cached entry", { error })
61+
return
62+
}
5763
},
5864
async clear(params: PopulateParams) {
5965
const entry = await strapi

server/src/services/deep-populate/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { ContentTypeConfigAllow } from "../../config"
1111
import type { PopulateParams } from "../populate"
1212
import type { PopulateComponentProps, PopulateDynamicZoneProps, PopulateProps, PopulateRelationProps } from "./types"
1313

14+
import log from "../../utils/log"
1415
import { sanitizeObject } from "../../utils/sanitizeObject"
1516
import { getConfig, getRelations, hasValue, isEmpty } from "./utils"
1617

@@ -26,6 +27,16 @@ async function _populateComponent<TContentType extends UID.ContentType, TSchema
2627
}: PopulateComponentProps<TContentType, TSchema>) {
2728
const componentLookup = lookup.length === 0 ? [attrName] : [...lookup, inDynamicZone ? "on" : "populate", attrName]
2829

30+
if (strapi.getModel(schema) === undefined) {
31+
log.warn(
32+
inDynamicZone
33+
? `The dynamic zone '${lookup[lookup.length - 1]}' is referencing a non-existing component '${schema}'. You should fix this.`
34+
: `Could not find component: '${schema}'`,
35+
{ lookup },
36+
)
37+
return true
38+
}
39+
2940
const componentPopulate = populate
3041
set(componentPopulate, componentLookup, { populate: "*" })
3142

@@ -198,7 +209,13 @@ async function _populate<TContentType extends UID.ContentType, TSchema extends U
198209
}: PopulateProps<TContentType, TSchema>) {
199210
const newPopulate = {}
200211

201-
let relations = getRelations(strapi.getModel(schema))
212+
const model = strapi.getModel(schema)
213+
if (!model) {
214+
strapi.log.warn(`[Plugin: Deep Populate] Could not find model for contentType: '${schema}'`)
215+
strapi.log.warn("Please create a bug report and share the troublesome contentType.")
216+
return {}
217+
}
218+
let relations = getRelations(model)
202219
let currentPopulate = cloneDeep(populate)
203220

204221
// Make sure we won't revisit this documentId from nested children

server/src/utils/log.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { name as pkgName } from "../../../package.json"
2+
3+
export const error = (msg, context = undefined) => strapi.log.error(`[${pkgName}] ${msg}`, context)
4+
export const warn = (msg, context = undefined) => strapi.log.warn(`[${pkgName}] ${msg}`, context)
5+
export const info = (msg, context = undefined) => strapi.log.info(`[${pkgName}] ${msg}`, context)
6+
export const debug = (msg, context = undefined) => strapi.log.debug(`[${pkgName}] ${msg}`, context)
7+
8+
export default { error, warn, info, debug }

0 commit comments

Comments
 (0)