mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 12:27:43 +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 || "",
|
||||
},
|
||||
google: {
|
||||
clientId: process.env.GOOGLE_CLIENT_ID || "",
|
||||
clientId: "",
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
|
||||
},
|
||||
},
|
||||
|
||||
@@ -105,30 +105,6 @@ export function getEndpoints<
|
||||
let api: Record<string, any> = {};
|
||||
for (const [key, value] of Object.entries(endpoints)) {
|
||||
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
|
||||
* instead of response object.
|
||||
@@ -192,18 +168,19 @@ export const router = <C extends AuthContext, Option extends BetterAuthOptions>(
|
||||
...middlewares,
|
||||
],
|
||||
onError(e) {
|
||||
const log = options.verboseLog ? logger : undefined;
|
||||
if (options.disableLog !== true) {
|
||||
if (e instanceof APIError) {
|
||||
logger.warn(e);
|
||||
log?.warn(e);
|
||||
} else {
|
||||
if (typeof e === "object" && e !== null && "message" in e) {
|
||||
const errorMessage = e.message as string;
|
||||
if (!errorMessage || typeof errorMessage !== "string") {
|
||||
logger.error(e);
|
||||
log?.error(e);
|
||||
return;
|
||||
}
|
||||
if (errorMessage.includes("no such table")) {
|
||||
logger.error(
|
||||
log?.error(
|
||||
`Please run ${chalk.green(
|
||||
"npx better-auth migrate",
|
||||
)} 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("doesn't exist")
|
||||
) {
|
||||
logger.error(
|
||||
log?.error(
|
||||
`Please run ${chalk.green(
|
||||
"npx better-auth migrate",
|
||||
)} to create the tables. There are missing tables in your MySQL database.`,
|
||||
);
|
||||
} else {
|
||||
logger.error(e);
|
||||
log?.error(e);
|
||||
}
|
||||
} else {
|
||||
logger.error(e);
|
||||
log?.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
export class BetterAuthError extends Error {
|
||||
constructor(message: string) {
|
||||
constructor(message: string, cause?: string) {
|
||||
super(message);
|
||||
this.name = "BetterAuthError";
|
||||
this.message = message;
|
||||
this.cause = cause;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,13 @@ import {
|
||||
} from "./utils/cookies";
|
||||
import { createLogger } from "./utils/logger";
|
||||
import { oAuthProviderList, oAuthProviders } from "./social-providers";
|
||||
import { BetterAuthError } from "./error/better-auth-error";
|
||||
|
||||
export const init = (options: BetterAuthOptions) => {
|
||||
const adapter = getAdapter(options);
|
||||
const db = createKyselyAdapter(options);
|
||||
if (!db) {
|
||||
throw new Error("No database adapter found");
|
||||
throw new BetterAuthError("No database adapter found");
|
||||
}
|
||||
const baseURL = getBaseURL(options.baseURL, options.basePath);
|
||||
|
||||
@@ -38,6 +39,11 @@ export const init = (options: BetterAuthOptions) => {
|
||||
if (value.enabled === false) {
|
||||
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);
|
||||
})
|
||||
.filter((x) => x !== null);
|
||||
|
||||
@@ -78,7 +78,6 @@ export const github = ({
|
||||
return await githubArctic.validateAuthorizationCode(state);
|
||||
},
|
||||
async getUserInfo(token) {
|
||||
console.log(`Bearer ${token.accessToken()}`);
|
||||
const { data: profile, error } = await betterFetch<GithubProfile>(
|
||||
"https://api.github.com/user",
|
||||
{
|
||||
|
||||
@@ -46,9 +46,9 @@ export const google = (options: GoogleOptions) => {
|
||||
createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
|
||||
if (!options.clientId || !options.clientSecret) {
|
||||
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) {
|
||||
throw new BetterAuthError("codeVerifier is required for Google");
|
||||
|
||||
@@ -84,6 +84,10 @@ export interface BetterAuthOptions {
|
||||
* @default false
|
||||
*/
|
||||
disableLog?: boolean;
|
||||
/**
|
||||
* log verbose information
|
||||
*/
|
||||
verboseLog?: boolean;
|
||||
/**
|
||||
* Database configuration
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user