feat(medusa): Improve error messaging in plugin loader (#4410)
* feat(medusa): Improve error messaging in plugin loader * Create fair-kids-tease.md * pr comments * pr comments
This commit is contained in:
committed by
GitHub
parent
01245ac89e
commit
85eb12883e
5
.changeset/fair-kids-tease.md
Normal file
5
.changeset/fair-kids-tease.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
feat(medusa): Improve error messaging in plugin loader
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
} from "medusa-core-utils"
|
||||
import path from "path"
|
||||
import { handleConfigError } from "../../loaders/config"
|
||||
import { MEDUSA_PROJECT_NAME } from "../../loaders/plugins"
|
||||
|
||||
function createFileContentHash(path, files) {
|
||||
return path + files
|
||||
@@ -143,10 +144,11 @@ export default (directory, featureFlagRouter) => {
|
||||
return details
|
||||
})
|
||||
|
||||
// Resolve user's project as a plugin for loading purposes
|
||||
resolved.push({
|
||||
resolve: `${directory}/dist`,
|
||||
name: `project-plugin`,
|
||||
id: createPluginId(`project-plugin`),
|
||||
name: MEDUSA_PROJECT_NAME,
|
||||
id: createPluginId(MEDUSA_PROJECT_NAME),
|
||||
options: {},
|
||||
version: createFileContentHash(process.cwd(), `**`),
|
||||
})
|
||||
|
||||
@@ -5,11 +5,11 @@ import {
|
||||
Resolver,
|
||||
} from "awilix"
|
||||
import { mkdirSync, rmSync, writeFileSync } from "fs"
|
||||
import { resolve } from "path"
|
||||
import Logger from "../logger"
|
||||
import { registerServices, registerStrategies } from "../plugins"
|
||||
import { DataSource, EntityManager } from "typeorm"
|
||||
import { createMedusaContainer } from "medusa-core-utils"
|
||||
import { resolve } from "path"
|
||||
import { DataSource, EntityManager } from "typeorm"
|
||||
import Logger from "../logger"
|
||||
import { MEDUSA_PROJECT_NAME, registerServices, registerStrategies } from "../plugins"
|
||||
|
||||
// ***** TEMPLATES *****
|
||||
const buildServiceTemplate = (name: string): string => {
|
||||
@@ -116,7 +116,7 @@ describe("plugins loader", () => {
|
||||
|
||||
const pluginsDetails = {
|
||||
resolve: resolve(__dirname, "__pluginsLoaderTest__"),
|
||||
name: `project-plugin`,
|
||||
name: MEDUSA_PROJECT_NAME,
|
||||
id: "fakeId",
|
||||
options: {},
|
||||
version: '"fakeVersion',
|
||||
|
||||
@@ -55,6 +55,8 @@ type PluginDetails = {
|
||||
|
||||
export const isSearchEngineInstalledResolutionKey = "isSearchEngineInstalled"
|
||||
|
||||
export const MEDUSA_PROJECT_NAME = "project-plugin"
|
||||
|
||||
/**
|
||||
* Registers all services in the services directory
|
||||
*/
|
||||
@@ -108,10 +110,11 @@ function getResolvedPlugins(
|
||||
return details
|
||||
})
|
||||
|
||||
// Resolve user's project as a plugin for loading purposes
|
||||
resolved.push({
|
||||
resolve: `${rootDirectory}/dist`,
|
||||
name: `project-plugin`,
|
||||
id: createPluginId(`project-plugin`),
|
||||
name: MEDUSA_PROJECT_NAME,
|
||||
id: createPluginId(MEDUSA_PROJECT_NAME),
|
||||
options: configModule,
|
||||
version: createFileContentHash(process.cwd(), `**`),
|
||||
})
|
||||
@@ -325,10 +328,12 @@ function registerApi(
|
||||
activityId: string
|
||||
): Express {
|
||||
const logger = container.resolve<Logger>("logger")
|
||||
logger.progress(
|
||||
activityId,
|
||||
`Registering custom endpoints for ${pluginDetails.name}`
|
||||
)
|
||||
const projectName =
|
||||
pluginDetails.name === MEDUSA_PROJECT_NAME
|
||||
? "your Medusa project"
|
||||
: `${pluginDetails.name}`
|
||||
|
||||
logger.progress(activityId, `Registering custom endpoints for ${projectName}`)
|
||||
try {
|
||||
const routes = require(`${pluginDetails.resolve}/api`).default
|
||||
if (routes) {
|
||||
@@ -336,12 +341,16 @@ function registerApi(
|
||||
}
|
||||
return app
|
||||
} catch (err) {
|
||||
if (err.message !== `Cannot find module '${pluginDetails.resolve}/api'`) {
|
||||
logger.progress(
|
||||
activityId,
|
||||
`No customer endpoints registered for ${pluginDetails.name}`
|
||||
if (err.code !== "MODULE_NOT_FOUND") {
|
||||
logger.warn(
|
||||
`An error occured while registering endpoints in ${projectName}`
|
||||
)
|
||||
|
||||
if (err.stack) {
|
||||
logger.warn(`${err.stack}`)
|
||||
}
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user