Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion packages/auth/src/better-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type DiagnosticOptions,
} from 'pgstencil/diagnostics';
import { betterAuth, type BetterAuthOptions } from 'better-auth';
import { getSessionFromCtx } from 'better-auth/api';
import { getSessionFromCtx, isAPIError } from 'better-auth/api';
import { lastLoginMethod } from 'better-auth/plugins';
import { emailOTP } from 'better-auth/plugins/email-otp';
import { Hono } from 'hono';
Expand Down Expand Up @@ -83,6 +83,8 @@ export function authOptions(options: AuthOptions): BetterAuthOptions {
onAPIError: {
errorURL: options.origin + (options.errorPath ?? '/'),
onError: (error) => {
// Expected 4xx responses are recorded by auth.rejected after dispatch.
if (isAPIError(error) && error.statusCode < 500) return;
diagnostic('request.failed', {
reason: 'unexpected_error',
...diagnosticError(error),
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/better-auth-oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,5 +1026,14 @@ test('auth diagnostics identify Microsoft token failures without recording crede
expect.objectContaining({ event: 'email.delivery.succeeded' }),
]),
);
const before = records.length;
const rejected = await next.post('sign-in/email-otp', {
email: 'private-email@example.test',
otp: '00000000',
});
expect(rejected.status).toBeGreaterThanOrEqual(400);
const rejectionLogs = records.slice(before);
expect(rejectionLogs.some((r) => r.event === 'auth.rejected')).toBe(true);
expect(rejectionLogs.some((r) => r.event === 'request.failed')).toBe(false);
expect(JSON.stringify(records)).not.toContain('private-email');
});
Loading