Module: Supabase::Rails::Web::AuthErrorMapper

Defined in:
lib/supabase/rails/web/auth_error_mapper.rb

Overview

Translates ‘Supabase::Auth::Errors::*` (raised by `supabase-rb`) into the gem-stable `Supabase::Rails::AuthError` surface (`#code` + `#status`) per FR-W9. Controllers and the middleware error-shaping pipeline can rely on these stable codes/statuses without inspecting upstream classes.

Ordering note: the dispatch is a ‘case/when`, so the most specific classes must come before their ancestors. `AuthRetryableError`, `AuthSessionMissing`, `AuthInvalidCredentialsError`, `AuthInvalidJwtError`, and `AuthWeakPassword` all inherit from `CustomAuthError < AuthError`; `AuthApiError`, `AuthPKCEError`, and `AuthUnknownError` inherit directly from `AuthError`. None of them inherit from each other, so the within-leaf order is cosmetic.

Class Method Summary collapse

Class Method Details

.translate(err) ⇒ Supabase::Rails::AuthError

Parameters:

  • err (Supabase::Auth::Errors::AuthError, StandardError)

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/supabase/rails/web/auth_error_mapper.rb', line 25

def translate(err)
  message = err.respond_to?(:message) ? err.message.to_s : err.to_s

  case err
  when ::Supabase::Auth::Errors::AuthInvalidCredentialsError
    AuthError.new(message, AuthError::INVALID_CREDENTIALS, 401)
  when ::Supabase::Auth::Errors::AuthInvalidJwtError
    AuthError.new(message, AuthError::INVALID_CREDENTIALS, 401)
  when ::Supabase::Auth::Errors::AuthSessionMissing
    AuthError.new(message, AuthError::SESSION_MISSING, 401)
  when ::Supabase::Auth::Errors::AuthWeakPassword
    AuthError.new(message, AuthError::WEAK_PASSWORD, 422)
  when ::Supabase::Auth::Errors::AuthPKCEError
    AuthError.new(message, AuthError::PKCE_ERROR, 400)
  when ::Supabase::Auth::Errors::AuthRetryableError
    AuthError.new(message, AuthError::AUTH_RETRYABLE, 503)
  when ::Supabase::Auth::Errors::AuthApiError
    map_api_error(message, err.status)
  when ::Supabase::Auth::Errors::AuthUnknownError
    AuthError.new(message, AuthError::AUTH_GENERIC_ERROR, 500)
  else
    AuthError.new(message, AuthError::AUTH_GENERIC_ERROR, 500)
  end
end