0.2.0 • Published 10 months ago

supabase-errors v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

supabase-errors

An error handling library for the Supabase JS and GoTrue JS auth clients.

The GoTrue API, which is also used by Supabase, does not return unique error codes. It does, however, return a descriptive error message. This package maps these error messages to more usable unique error codes. There is an error handler for every JS client function that calls the API.

Quick start

Install

npm install supabase-errors
// or
yarn add supabase-errors

Usage

import { ErrorHandler, type SignInWithPasswordErrorCode  } from 'supabase-errors';

const { data, error } = await supabase.auth.signInWithPassword({...});

const handler = new ErrorHandler({combineInternalErrors: true, exludeErrorTypes: ['TypeScriptSafe']});

// Implement error handlers
handler.handleSignInWithPasswordError(error, {
    onEmailLoginDisabled: () => console.log('Email logins are disabled'),
    onInternalError: (errorCode) => console.log('Internal error: ', errorCode),
    onUnhandledError: (errorCode) => console.log('Unhandled error code: ', errorCode),
    onUnknownError: (error) => console.log('Unhandled auth error: ', error), 
});

// Create error mappings
const signInWithPasswordErrorMessageMap: Record<SignInWithPasswordErrorCode, string>  = {
  'email-login-disabled': 'Email logins are not supported',
}

Documentation

Config

ParameterOptionsDefaultDescription
combineInternalErrorsbooleanfalseRemoves all handlers of '5XX' errors and combines them in a handler called onInternalError.
excludeErrorTypesErrorType[][]Removes all handlers of errors that match one of the excluded types.
disableLoggingbooleanfalseControls whether unknown errors are logged.

Error types

TypeDescription
4XXBad request errors
5XXInternal errors
TypeScriptSafeErrors that can not occur when the Typescript version of the Supabase or GoTrue client is used. For example, the 'only-email-or-phone-number' error can never occur when your code is type-safe.

Coverage

This package is currently work in progress. Below is a list of all functions needing an error handler and their current development status.

StatusDescription
Static BadgeCan handle the complete list of erros
Static BadgeCan handle the complete list of bad request errors
Static BadgeCan handle some errors
Static BadgeOnly onUnknownError is implemented

Errors that occur due to a misconfiguration, e.g. an invalid webhook URL, are currently not included in the full coverage.

Error handlers

MethodStatus
signupStatic Badge
signinWithPasswordStatic Badge
signInWithOtpStatic Badge
signInWithOAuthStatic Badge
signInWithSSOStatic Badge
signOutStatic Badge
resetPasswordForEmailStatic Badge
verifyOtpStatic Badge
getSessionStatic Badge
refreshSessionStatic Badge
getUserStatic Badge
updateUserStatic Badge
reauthenticateStatic Badge
resendStatic Badge
setSessionStatic Badge
exchangeCodeForSessionStatic Badge
getUserByIdStatic Badge
listUsersStatic Badge
createUserStatic Badge
deleteUserStatic Badge
inviteUserByEmailStatic Badge
generateLinkStatic Badge
updateUserByIdStatic Badge
0.2.0

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago