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

feat(adapter): take away error handling from adapters #1871

Merged
merged 13 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions config/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// We aim to have the same support as Next.js
// https://nextjs.org/docs/getting-started#system-requirements
// https://nextjs.org/docs/basic-features/supported-browsers-features

module.exports = {
presets: [["@babel/preset-env", { targets: { node: "10.13" } }]],
plugins: [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-runtime",
],
comments: false,
overrides: [
{
test: ["../src/client/**"],
presets: [["@babel/preset-env", { targets: { ie: "11" } }]],
},
{
test: ["../src/server/pages/**"],
presets: ["preact"],
},
],
}
15 changes: 0 additions & 15 deletions config/babel.config.json

This file was deleted.

395 changes: 386 additions & 9 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-auth",
"version": "0.0.0-semantically-released",
"version": "3.20.0-canary.3",
"description": "Authentication for Next.js",
"homepage": "https://next-auth.js.org",
"repository": "https://github.com/nextauthjs/next-auth.git",
Expand Down Expand Up @@ -30,12 +30,12 @@
},
"scripts": {
"build": "npm run build:js && npm run build:css",
"build:js": "node ./config/build.js && babel --config-file ./config/babel.config.json src --out-dir dist",
"build:js": "node ./config/build.js && babel --config-file ./config/babel.config.js src --out-dir dist",
"build:css": "postcss --config config/postcss.config.js src/**/*.css --base src --dir dist && node config/wrap-css.js",
"dev:setup": "npm run build:css && cd app && npm i",
"dev": "cd app && npm run dev",
"watch": "npm run watch:js | npm run watch:css",
"watch:js": "babel --config-file ./config/babel.config.json --watch src --out-dir dist",
"watch:js": "babel --config-file ./config/babel.config.js --watch src --out-dir dist",
"watch:css": "postcss --config config/postcss.config.js --watch src/**/*.css --base src --dir dist",
"test": "echo \"Write some tests...\"; npm run test:types",
"test:types": "dtslint types",
Expand All @@ -61,6 +61,7 @@
],
"license": "ISC",
"dependencies": {
"@babel/runtime": "^7.14.0",
"@next-auth/prisma-legacy-adapter": "canary",
"@next-auth/typeorm-legacy-adapter": "canary",
"crypto-js": "^4.0.0",
Expand Down Expand Up @@ -91,6 +92,7 @@
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-transform-runtime": "^7.13.15",
"@babel/preset-env": "^7.9.6",
"@prisma/client": "^2.16.1",
"@semantic-release/commit-analyzer": "^8.0.1",
Expand Down
22 changes: 22 additions & 0 deletions src/adapters/error-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { UnknownError } from "../lib/errors"

/**
* Handles adapter induced errors.
* @param {import("types/adapters").AdapterInstance} adapter
* @return {import("types/adapters").AdapterInstance}
*/
export default function adapterErrorHandler(adapter) {
return Object.keys(adapter).reduce((acc, method) => {
const adapterMethod = adapter[method]
acc[method] = async (...args) => {
try {
return await adapterMethod(...args)
Copy link
Member Author

Choose a reason for hiding this comment

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

should log out args with debug, and an adapter prefix(?)

@kripod, currently logger.debug is rewritten in adapters to include a prefix for code. Do you think we could return a name property next to getAdapter for this purpose?

any thoughts?

Copy link
Contributor

@kripod kripod Apr 30, 2021

Choose a reason for hiding this comment

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

We may specify an attribute like { displayName: string } as the minimum required config param when instantiating adapters. By doing so, we would also solve the typing issue of Adapter(requiredParam, …).

Copy link
Member Author

Choose a reason for hiding this comment

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

After my recent commit, it will be possible for adapters to return a displayName property in getAdapter. Unfortunately we don't have the adapter configuration when we invoke the .getAdapter() method.

} catch (error) {
const e = new UnknownError(error)
e.name = `${method[0].toUpperCase()}${method.slice(1)}Error`
throw e
}
}
return acc
}, {})
}
Loading