chore(core): fix error.cause in BetterAuthError (#7347)

This commit is contained in:
Alex Yang
2026-01-13 17:24:10 -08:00
committed by GitHub
parent cf219a50e0
commit f8b052d2e1
4 changed files with 14 additions and 12 deletions

View File

@@ -43,16 +43,14 @@ export const jwt = <O extends JwtOptions>(options?: O) => {
// Remote url must be set when using signing function
if (options?.jwt?.sign && !options.jwks?.remoteUrl) {
throw new BetterAuthError(
"jwks_config",
"jwks.remoteUrl must be set when using jwt.sign",
"options.jwks.remoteUrl must be set when using options.jwt.sign",
);
}
// Alg is required to be specified when using remote url (needed in openid metadata)
if (options?.jwks?.remoteUrl && !options.jwks?.keyPairConfig?.alg) {
throw new BetterAuthError(
"jwks_config",
"must specify alg when using the oidc plugin and jwks.remoteUrl",
"options.jwks.keyPairConfig.alg must be specified when using the oidc plugin with options.jwks.remoteUrl",
);
}
@@ -64,8 +62,7 @@ export const jwt = <O extends JwtOptions>(options?: O) => {
jwksPath.includes("..")
) {
throw new BetterAuthError(
"jwks_config",
"jwksPath must be a non-empty string starting with '/' and not contain '..'",
"options.jwks.jwksPath must be a non-empty string starting with '/' and not contain '..'",
);
}

View File

@@ -419,7 +419,9 @@ describe("jwt - remote signing", async () => {
}),
],
}),
).toThrowError("jwks_config");
).toThrowError(
"options.jwks.remoteUrl must be set when using options.jwt.sign",
);
});
});
@@ -435,7 +437,9 @@ describe("jwt - remote url", async () => {
}),
],
}),
).toThrowError("jwks_config");
).toThrowError(
"options.jwks.keyPairConfig.alg must be specified when using the oidc plugin with options.jwks.remoteUrl",
);
});
it("should accept remoteUrl with alg specified", async () => {

View File

@@ -27,7 +27,9 @@ function assertHasProtocol(url: string): void {
}
throw new BetterAuthError(
`Invalid base URL: ${url}. Please provide a valid base URL.`,
String(error),
{
cause: error,
},
);
}
}

View File

@@ -1,11 +1,10 @@
import { APIError as BaseAPIError } from "better-call/error";
export class BetterAuthError extends Error {
constructor(message: string, cause?: string | undefined) {
super(message);
constructor(message: string, options?: { cause?: unknown | undefined }) {
super(message, options);
this.name = "BetterAuthError";
this.message = message;
this.cause = cause;
this.stack = "";
}
}