Skip to content

Commit

Permalink
fix: throw a proper error message on invalid environment or API type
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilK15 authored Jul 6, 2020
1 parent d3a0546 commit 3b24817
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
22 changes: 22 additions & 0 deletions packages/api-headless-cms/__tests__/environmentUrl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import useContentHandler from "./utils/useContentHandler";

describe("Invalid type and environment URL params test", () => {
it("should respond with proper error messages", async () => {
const { invoke } = useContentHandler();
let [body1] = await invoke({
pathParameters: { key: "read123/xyz" }
});

expect(body1.error.message).toEqual(
"Could not load environment, please check if the passed environment alias slug or environment ID is correct."
);

let [body2] = await invoke({
pathParameters: { key: "read/xyz" }
});

expect(body2.error.message).toEqual(
"Could not load environment, please check if the passed environment alias slug or environment ID is correct."
);
});
});
9 changes: 6 additions & 3 deletions packages/api-headless-cms/src/content/plugins/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ export default () => {
// Before continuing with the rest of the models, we must load the environment and assign it to the context.
let environment = null;
let environmentAlias = null;
if (context.cms.environment && typeof context.cms.environment === "string") {

if (context.cms.environment && typeof context.cms.environment === "string") {
if (context.models.CmsEnvironment.isId(context.cms.environment)) {
environment = await context.models.CmsEnvironment.findById(context.cms.environment);
} else {
environmentAlias = await context.models.CmsEnvironmentAlias.findOne({
query: { slug: context.cms.environment }
query: {slug: context.cms.environment}
});
environment = await environmentAlias.environment;
if (environmentAlias) {
environment = await environmentAlias.environment;
}
}
}

Expand Down

0 comments on commit 3b24817

Please sign in to comment.