error-handing (#38)

This commit is contained in:
Anton Tranelis 2025-05-21 12:24:00 +02:00 committed by GitHub
parent 1040db5fa5
commit d89e160276
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2650 additions and 120 deletions

2741
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -10,14 +10,35 @@ import { directusClient } from './directus'
import type { UserApi, UserItem } from 'utopia-ui'
interface DirectusError {
errors: {
message: string
[key: string]: any
}[]
}
export class userApi implements UserApi {
async register(email: string, password: string, userName: string): Promise<any> {
try {
return await directusClient.request(createUser({ email, password, first_name: userName }))
} catch (error: any) {
console.log(error)
if (error.errors[0].message) throw error.errors[0].message
else throw error
} catch (error: unknown) {
console.error('Registration error:', error)
if (
typeof error === 'object' &&
error !== null &&
'errors' in error &&
Array.isArray((error as any).errors)
) {
const directusError = error as DirectusError
const errorMessage = directusError.errors[0]?.message
if (errorMessage.includes('has to be unique')) {
throw new Error('This e-mail address is already registered.')
}
throw new Error(errorMessage || 'Unknown error during registration.')
}
throw error
}
}