Skip to content

Commit

Permalink
the api should 500 not 401 if there is a db error (#468)
Browse files Browse the repository at this point in the history
* the api should 500 not 401 if there is a db error (aside from not finding key).  rescue was too liberal before

* changeset

* fix bugs

* bump prisma so we can use error codes

* use relative imports in this project

* disable redwood_ts test because create-redwood-app fails to create a typescript app
  • Loading branch information
alexfarrill authored May 15, 2023
1 parent 210dd70 commit a79f937
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/shiny-parents-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"mailing": patch
"mailing-core": patch
"web": patch
---

The auth api should 500 not 401 if there is a database error
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- remix_js
- remix_ts
- redwood_js
- redwood_ts
# - redwood_ts
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,6 @@ e2e/cache
# Prisma clients
packages/web/prisma/generated
packages/cli/prisma/generated

# VSCode
.vscode
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"dependencies": {
"@babel/parser": "^7.18.11",
"@prisma/client": "^4.4.0",
"@prisma/client": "4.11.0",
"@reecelucas/react-use-hotkeys": "^1.3.5",
"@tailwindcss/line-clamp": "^0.4.2",
"@types/bcrypt": "^5.0.0",
Expand Down
11 changes: 9 additions & 2 deletions packages/cli/src/pages/api/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { apiKeyFromReq } from "../../../util/validate/validateApiKey";
import createMessage from "../../../util/createMessage";
import prisma from "../../../../prisma";
import { Prisma } from "../../../../prisma/generated/client";
import { error } from "../../../util/serverLogger";

type Data = {
error?: string;
Expand Down Expand Up @@ -45,8 +46,14 @@ export default async function handler(
});

organizationId = apiKeyRecord.organizationId;
} catch {
return res.status(401).json({ error: "API key is not valid" });
} catch (e) {
if ((e as Prisma.PrismaClientKnownRequestError).code === "P2025") {
res.status(401).json({ error: "API key is not valid" });
} else {
error(`Internal server error in ${__filename}`, e);
res.status(500).json({ error: "Internal server error" });
}
return;
}
}

Expand Down
10 changes: 9 additions & 1 deletion packages/cli/src/util/__test__/validateApiKey.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { validateApiKey } from "../validate/validateApiKey";
import prisma from "../../../prisma";
import { Prisma } from "../../../prisma/generated/client";

function mockRequestResponse() {
const { req, res } = {
Expand Down Expand Up @@ -56,9 +57,16 @@ describe("validateApiKey", () => {
describe("using database", () => {
it("returns false if NODE_ENV is not development and apiKey is not valid", async () => {
const { req, res } = mockRequestResponse();

// throw an error that looks like a Prisma not found error
const notFoundError = new Prisma.PrismaClientKnownRequestError(
"NotFoundError",
{ code: "P2025", clientVersion: Prisma.prismaVersion.client }
);

jest
.spyOn(prisma.apiKey, "findFirstOrThrow")
.mockRejectedValueOnce(new Error("NOT FOUND"));
.mockRejectedValueOnce(notFoundError);
req.query = { apiKey: "321" };
const result = await validateApiKey(req, res);
expect(result).toBe(false);
Expand Down
12 changes: 10 additions & 2 deletions packages/cli/src/util/validate/validateApiKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "../../../prisma";
import { Prisma } from "../../../prisma/generated/client";
import { error } from "../serverLogger";

export const apiKeyFromReq = (
req: NextApiRequest
Expand Down Expand Up @@ -37,8 +39,14 @@ export async function validateApiKey(
});

return true;
} catch {
res.status(401).json({ error: "API key is not valid" });
} catch (e) {
if ((e as Prisma.PrismaClientKnownRequestError).code === "P2025") {
res.status(401).json({ error: "API key is not valid" });
} else {
error(`Internal server error`, e);
res.status(500).json({ error: "Internal server error" });
}

return false;
}
}
2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"jest-mock-extended": "^2.0.7",
"mailing": "^0.9.15",
"postcss": "^8.4.14",
"prisma": "^4.2.0",
"prisma": "4.11.0",
"tailwindcss": "^3.2.4"
}
}
28 changes: 26 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2055,18 +2055,35 @@
pirates "^4.0.1"
source-map-support "^0.5.16"

"@prisma/client@^4.2.0", "@prisma/client@^4.4.0":
"@prisma/[email protected]":
version "4.11.0"
resolved "https://registry.npmjs.org/@prisma/client/-/client-4.11.0.tgz#41d5664dea4172c954190a432f70b86d3e2e629b"
integrity sha512-0INHYkQIqgAjrt7NzhYpeDQi8x3Nvylc2uDngKyFDDj1tTRQ4uV1HnVmd1sQEraeVAN63SOK0dgCKQHlvjL0KA==
dependencies:
"@prisma/engines-version" "4.11.0-57.8fde8fef4033376662cad983758335009d522acb"

"@prisma/client@^4.2.0":
version "4.4.0"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-4.4.0.tgz#45f59c172dd3621ecc92d7cf9bc765d85e6c7d56"
integrity sha512-ciKOP246x1xwr04G9ajHlJ4pkmtu9Q6esVyqVBO0QJihaKQIUvbPjClp17IsRJyxqNpFm4ScbOc/s9DUzKHINQ==
dependencies:
"@prisma/engines-version" "4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6"

"@prisma/engines-version@4.11.0-57.8fde8fef4033376662cad983758335009d522acb":
version "4.11.0-57.8fde8fef4033376662cad983758335009d522acb"
resolved "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.11.0-57.8fde8fef4033376662cad983758335009d522acb.tgz#74af5ff56170c78e93ce46c56510160f58cd3c01"
integrity sha512-3Vd8Qq06d5xD8Ch5WauWcUUrsVPdMC6Ge8ILji8RFfyhUpqon6qSyGM0apvr1O8n8qH8cKkEFqRPsYjuz5r83g==

"@prisma/engines-version@4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6":
version "4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6"
resolved "https://registry.yarnpkg.com/@prisma/engines-version/-/engines-version-4.4.0-66.f352a33b70356f46311da8b00d83386dd9f145d6.tgz#00875863bb30b670a586a5b5794a000f7f3ad976"
integrity sha512-P5v/PuEIJLYXZUZBvOLPqoyCW+m6StNqHdiR6te++gYVODpPdLakks5HVx3JaZIY+LwR02juJWFlwpc9Eog/ug==

"@prisma/[email protected]":
version "4.11.0"
resolved "https://registry.npmjs.org/@prisma/engines/-/engines-4.11.0.tgz#c99749bfe20f58e8f4d2b5e04fee0785eba440e1"
integrity sha512-0AEBi2HXGV02cf6ASsBPhfsVIbVSDC9nbQed4iiY5eHttW9ZtMxHThuKZE1pnESbr8HRdgmFSa/Kn4OSNYuibg==

"@prisma/[email protected]":
version "4.4.0"
resolved "https://registry.yarnpkg.com/@prisma/engines/-/engines-4.4.0.tgz#6ca7d3ce8eee08dcfa82311b0a02f5ccaac7dc0c"
Expand Down Expand Up @@ -9057,7 +9074,14 @@ pretty-format@^29.1.2:
ansi-styles "^5.0.0"
react-is "^18.0.0"

prisma@^4.2.0, prisma@^4.4.0:
[email protected]:
version "4.11.0"
resolved "https://registry.npmjs.org/prisma/-/prisma-4.11.0.tgz#9695ba4129a43eab3e76b5f7a033c6c020377725"
integrity sha512-4zZmBXssPUEiX+GeL0MUq/Yyie4ltiKmGu7jCJFnYMamNrrulTBc+D+QwAQSJ01tyzeGHlD13kOnqPwRipnlNw==
dependencies:
"@prisma/engines" "4.11.0"

prisma@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/prisma/-/prisma-4.4.0.tgz#0c53324bf6a29474636b3e1964e0d72e0277bf8f"
integrity sha512-l/QKLmLcKJQFuc+X02LyICo0NWTUVaNNZ00jKJBqwDyhwMAhboD1FWwYV50rkH4Wls0RviAJSFzkC2ZrfawpfA==
Expand Down

3 comments on commit a79f937

@vercel
Copy link

@vercel vercel bot commented on a79f937 May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

web-emails – ./packages/web

web-emails-sofn.vercel.app
web-emails-git-main-sofn.vercel.app
mailing-web.vercel.app
emails.mailing.run

@vercel
Copy link

@vercel vercel bot commented on a79f937 May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mailing-web – ./packages/web

mailing-web-git-main-sofn.vercel.app
mailing-web-sofn.vercel.app
web-rho-puce.vercel.app
mailing.run
www.mailing.run

@vercel
Copy link

@vercel vercel bot commented on a79f937 May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.