Back to all reviewers

Use consistent error types

better-auth/better-auth
Based on 6 comments
TypeScript

Always use the appropriate error classes and throwing mechanisms for consistent error handling across the codebase. Prefer `BetterAuthError` or `APIError` over generic `Error` classes, use `throw` instead of returning error responses in endpoints, and structure error codes consistently.

Error Handling TypeScript

Reviewer Prompt

Always use the appropriate error classes and throwing mechanisms for consistent error handling across the codebase. Prefer BetterAuthError or APIError over generic Error classes, use throw instead of returning error responses in endpoints, and structure error codes consistently.

Key practices:

  • Use BetterAuthError instead of new Error() for domain-specific errors
  • Use throw ctx.error or throw new APIError in endpoints instead of return ctx.json(error)
  • Use toThrowError with specific error details in tests instead of generic toThrow()
  • Structure error codes consistently using ERROR_CODES constants
  • Use appropriate HTTP status codes (e.g., BAD_REQUEST instead of USER_NOT_FOUND for client errors)

Example:

// โŒ Avoid
throw new Error("unable to set a future iat time");
return ctx.json({ error: "invalid_grant" }, { status: 400 });

// โœ… Prefer  
throw new BetterAuthError("unable to set a future iat time");
throw new APIError("BAD_REQUEST", {
  message: ERROR_CODES.INVALID_GRANT,
  code: "INVALID_GRANT"
});

This ensures proper error type inference, consistent error handling patterns, and better debugging experience across the application.

6
Comments Analyzed
TypeScript
Primary Language
Error Handling
Category

Source Discussions