mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 20:27:44 +00:00
fix: better error reporting
This commit is contained in:
@@ -82,7 +82,7 @@ export const auth = betterAuth({
|
|||||||
clientSecret: process.env.GITHUB_CLIENT_SECRET || "",
|
clientSecret: process.env.GITHUB_CLIENT_SECRET || "",
|
||||||
},
|
},
|
||||||
google: {
|
google: {
|
||||||
clientId: process.env.GOOGLE_CLIENT_ID || "",
|
clientId: "",
|
||||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -105,30 +105,6 @@ export function getEndpoints<
|
|||||||
let api: Record<string, any> = {};
|
let api: Record<string, any> = {};
|
||||||
for (const [key, value] of Object.entries(endpoints)) {
|
for (const [key, value] of Object.entries(endpoints)) {
|
||||||
api[key] = async (context: any) => {
|
api[key] = async (context: any) => {
|
||||||
// for (const plugin of ctx.options.plugins || []) {
|
|
||||||
// if (plugin.hooks?.before) {
|
|
||||||
// for (const hook of plugin.hooks.before) {
|
|
||||||
// const match = hook.matcher({
|
|
||||||
// ...context,
|
|
||||||
// ...value,
|
|
||||||
// });
|
|
||||||
// if (match) {
|
|
||||||
// const hookRes = await hook.handler(context);
|
|
||||||
// if (hookRes && "context" in hookRes) {
|
|
||||||
// context = {
|
|
||||||
// ...context,
|
|
||||||
// ...hookRes.context,
|
|
||||||
// ...value,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
if (context.path === "/user/update") {
|
|
||||||
const res = await api[key].options.use[1](context);
|
|
||||||
console.log(res);
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* TODO: move this to respond a json response
|
* TODO: move this to respond a json response
|
||||||
* instead of response object.
|
* instead of response object.
|
||||||
@@ -192,18 +168,19 @@ export const router = <C extends AuthContext, Option extends BetterAuthOptions>(
|
|||||||
...middlewares,
|
...middlewares,
|
||||||
],
|
],
|
||||||
onError(e) {
|
onError(e) {
|
||||||
|
const log = options.verboseLog ? logger : undefined;
|
||||||
if (options.disableLog !== true) {
|
if (options.disableLog !== true) {
|
||||||
if (e instanceof APIError) {
|
if (e instanceof APIError) {
|
||||||
logger.warn(e);
|
log?.warn(e);
|
||||||
} else {
|
} else {
|
||||||
if (typeof e === "object" && e !== null && "message" in e) {
|
if (typeof e === "object" && e !== null && "message" in e) {
|
||||||
const errorMessage = e.message as string;
|
const errorMessage = e.message as string;
|
||||||
if (!errorMessage || typeof errorMessage !== "string") {
|
if (!errorMessage || typeof errorMessage !== "string") {
|
||||||
logger.error(e);
|
log?.error(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (errorMessage.includes("no such table")) {
|
if (errorMessage.includes("no such table")) {
|
||||||
logger.error(
|
log?.error(
|
||||||
`Please run ${chalk.green(
|
`Please run ${chalk.green(
|
||||||
"npx better-auth migrate",
|
"npx better-auth migrate",
|
||||||
)} to create the tables. There are missing tables in your SQLite database.`,
|
)} to create the tables. There are missing tables in your SQLite database.`,
|
||||||
@@ -221,16 +198,16 @@ export const router = <C extends AuthContext, Option extends BetterAuthOptions>(
|
|||||||
errorMessage.includes("Table") &&
|
errorMessage.includes("Table") &&
|
||||||
errorMessage.includes("doesn't exist")
|
errorMessage.includes("doesn't exist")
|
||||||
) {
|
) {
|
||||||
logger.error(
|
log?.error(
|
||||||
`Please run ${chalk.green(
|
`Please run ${chalk.green(
|
||||||
"npx better-auth migrate",
|
"npx better-auth migrate",
|
||||||
)} to create the tables. There are missing tables in your MySQL database.`,
|
)} to create the tables. There are missing tables in your MySQL database.`,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
logger.error(e);
|
log?.error(e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.error(e);
|
log?.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
export class BetterAuthError extends Error {
|
export class BetterAuthError extends Error {
|
||||||
constructor(message: string) {
|
constructor(message: string, cause?: string) {
|
||||||
super(message);
|
super(message);
|
||||||
|
this.name = "BetterAuthError";
|
||||||
|
this.message = message;
|
||||||
|
this.cause = cause;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,12 +14,13 @@ import {
|
|||||||
} from "./utils/cookies";
|
} from "./utils/cookies";
|
||||||
import { createLogger } from "./utils/logger";
|
import { createLogger } from "./utils/logger";
|
||||||
import { oAuthProviderList, oAuthProviders } from "./social-providers";
|
import { oAuthProviderList, oAuthProviders } from "./social-providers";
|
||||||
|
import { BetterAuthError } from "./error/better-auth-error";
|
||||||
|
|
||||||
export const init = (options: BetterAuthOptions) => {
|
export const init = (options: BetterAuthOptions) => {
|
||||||
const adapter = getAdapter(options);
|
const adapter = getAdapter(options);
|
||||||
const db = createKyselyAdapter(options);
|
const db = createKyselyAdapter(options);
|
||||||
if (!db) {
|
if (!db) {
|
||||||
throw new Error("No database adapter found");
|
throw new BetterAuthError("No database adapter found");
|
||||||
}
|
}
|
||||||
const baseURL = getBaseURL(options.baseURL, options.basePath);
|
const baseURL = getBaseURL(options.baseURL, options.basePath);
|
||||||
|
|
||||||
@@ -38,6 +39,11 @@ export const init = (options: BetterAuthOptions) => {
|
|||||||
if (value.enabled === false) {
|
if (value.enabled === false) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (!value.clientId || !value.clientSecret) {
|
||||||
|
throw new BetterAuthError(
|
||||||
|
`Social provider ${key} is missing clientId or clientSecret`,
|
||||||
|
);
|
||||||
|
}
|
||||||
return oAuthProviders[key as (typeof oAuthProviderList)[number]](value);
|
return oAuthProviders[key as (typeof oAuthProviderList)[number]](value);
|
||||||
})
|
})
|
||||||
.filter((x) => x !== null);
|
.filter((x) => x !== null);
|
||||||
|
|||||||
@@ -78,7 +78,6 @@ export const github = ({
|
|||||||
return await githubArctic.validateAuthorizationCode(state);
|
return await githubArctic.validateAuthorizationCode(state);
|
||||||
},
|
},
|
||||||
async getUserInfo(token) {
|
async getUserInfo(token) {
|
||||||
console.log(`Bearer ${token.accessToken()}`);
|
|
||||||
const { data: profile, error } = await betterFetch<GithubProfile>(
|
const { data: profile, error } = await betterFetch<GithubProfile>(
|
||||||
"https://api.github.com/user",
|
"https://api.github.com/user",
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -46,9 +46,9 @@ export const google = (options: GoogleOptions) => {
|
|||||||
createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
|
createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
|
||||||
if (!options.clientId || !options.clientSecret) {
|
if (!options.clientId || !options.clientSecret) {
|
||||||
logger.error(
|
logger.error(
|
||||||
"clientId and clientSecret is required for Google. Make sure to you have provided them in the options",
|
"Client Id and Client Secret is required for Google. Make sure to provide them in the options.",
|
||||||
);
|
);
|
||||||
throw new BetterAuthError("clientId is required for Google");
|
throw new BetterAuthError("CLIENT_ID_AND_SECRET_REQUIRED");
|
||||||
}
|
}
|
||||||
if (!codeVerifier) {
|
if (!codeVerifier) {
|
||||||
throw new BetterAuthError("codeVerifier is required for Google");
|
throw new BetterAuthError("codeVerifier is required for Google");
|
||||||
|
|||||||
@@ -84,6 +84,10 @@ export interface BetterAuthOptions {
|
|||||||
* @default false
|
* @default false
|
||||||
*/
|
*/
|
||||||
disableLog?: boolean;
|
disableLog?: boolean;
|
||||||
|
/**
|
||||||
|
* log verbose information
|
||||||
|
*/
|
||||||
|
verboseLog?: boolean;
|
||||||
/**
|
/**
|
||||||
* Database configuration
|
* Database configuration
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user