Use proper errors instead of string throwing

This commit is contained in:
adrinr 2023-04-10 20:06:47 +01:00
parent 57a3ff96ea
commit e1279ffecd
2 changed files with 12 additions and 3 deletions

View File

@ -97,3 +97,11 @@ export class InvalidAPIKeyError extends BudibaseError {
) )
} }
} }
// USERS
export class EmailUnavailableError extends Error {
constructor(email: string) {
super(`Email already taken: '${email}'`)
}
}

View File

@ -16,6 +16,7 @@ import {
ViewName, ViewName,
env as coreEnv, env as coreEnv,
context, context,
EmailUnavailableError,
} from "@budibase/backend-core" } from "@budibase/backend-core"
import { import {
AccountMetadata, AccountMetadata,
@ -158,7 +159,7 @@ const validateUniqueUser = async (email: string, tenantId: string) => {
if (env.MULTI_TENANCY) { if (env.MULTI_TENANCY) {
const tenantUser = await getPlatformUser(email) const tenantUser = await getPlatformUser(email)
if (tenantUser != null && tenantUser.tenantId !== tenantId) { if (tenantUser != null && tenantUser.tenantId !== tenantId) {
throw `Unavailable` throw new EmailUnavailableError(email)
} }
} }
@ -166,7 +167,7 @@ const validateUniqueUser = async (email: string, tenantId: string) => {
if (!env.SELF_HOSTED && !env.DISABLE_ACCOUNT_PORTAL) { if (!env.SELF_HOSTED && !env.DISABLE_ACCOUNT_PORTAL) {
const account = await accounts.getAccount(email) const account = await accounts.getAccount(email)
if (account && account.verified && account.tenantId !== tenantId) { if (account && account.verified && account.tenantId !== tenantId) {
throw `Unavailable` throw new EmailUnavailableError(email)
} }
} }
} }
@ -235,7 +236,7 @@ export const save = async (
// no id was specified - load from email instead // no id was specified - load from email instead
dbUser = await usersCore.getGlobalUserByEmail(email) dbUser = await usersCore.getGlobalUserByEmail(email)
if (dbUser && dbUser._id !== _id) { if (dbUser && dbUser._id !== _id) {
throw `Unavailable` throw new EmailUnavailableError(email)
} }
} }