feat(frontend): create account from discord, better error alert

This commit is contained in:
sam 2024-09-14 16:37:27 +02:00
parent ff22530f0a
commit 103ba24555
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
4 changed files with 292 additions and 66 deletions

View file

@ -3,7 +3,7 @@ export type ApiError = {
status: number;
message: string;
code: ErrorCode;
errors?: ValidationError[];
errors?: Array<{ key: string; errors: ValidationError[] }>;
};
export enum ErrorCode {
@ -26,3 +26,31 @@ export type ValidationError = {
allowed_values?: any[];
actual_value?: any;
};
/**
* Returns the first error for the value `key` in `error`.
* @param error The error object to traverse.
* @param key The JSON key to find.
*/
export const firstErrorFor = (error: ApiError, key: string): ValidationError | undefined => {
if (!error.errors) return undefined;
const field = error.errors.find((e) => e.key == key);
if (!field?.errors) return undefined;
return field.errors.length != 0 ? field.errors[0] : undefined;
};
export enum ValidationErrorType {
LengthError = 0,
DisallowedValueError = 1,
GenericValidationError = 2,
}
export const validationErrorType = (error: ValidationError) => {
if (error.min_length && error.max_length && error.actual_length) {
return ValidationErrorType.LengthError;
}
if (error.allowed_values && error.actual_value) {
return ValidationErrorType.DisallowedValueError;
}
return ValidationErrorType.GenericValidationError;
};