mirror of
https://github.com/utopia-os/utopia-ui.git
synced 2025-12-13 07:46:10 +00:00
error-handing (#38)
This commit is contained in:
parent
1040db5fa5
commit
d89e160276
2741
package-lock.json
generated
2741
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -10,14 +10,35 @@ import { directusClient } from './directus'
|
|||||||
|
|
||||||
import type { UserApi, UserItem } from 'utopia-ui'
|
import type { UserApi, UserItem } from 'utopia-ui'
|
||||||
|
|
||||||
|
interface DirectusError {
|
||||||
|
errors: {
|
||||||
|
message: string
|
||||||
|
[key: string]: any
|
||||||
|
}[]
|
||||||
|
}
|
||||||
|
|
||||||
export class userApi implements UserApi {
|
export class userApi implements UserApi {
|
||||||
async register(email: string, password: string, userName: string): Promise<any> {
|
async register(email: string, password: string, userName: string): Promise<any> {
|
||||||
try {
|
try {
|
||||||
return await directusClient.request(createUser({ email, password, first_name: userName }))
|
return await directusClient.request(createUser({ email, password, first_name: userName }))
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
console.log(error)
|
console.error('Registration error:', error)
|
||||||
if (error.errors[0].message) throw error.errors[0].message
|
if (
|
||||||
else throw error
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user