forked from cardosofelipe/fast-next-template
Refactor form error handling with type guards, enhance API client configuration, and update implementation plan
- Introduced `isAPIErrorArray` type guard to improve error handling in authentication forms, replacing type assertions for better runtime safety. - Refactored error handling logic across `RegisterForm`, `LoginForm`, `PasswordResetRequestForm`, and `PasswordResetConfirmForm` for unexpected error fallbacks. - Updated `next.config.ts` and `.eslintrc.json` to exclude generated API client files from linting and align configuration with latest project structure. - Added comprehensive documentation on Phase 2 completion in `IMPLEMENTATION_PLAN.md`.
This commit is contained in:
@@ -172,3 +172,28 @@ export function getGeneralError(errors: APIError[]): string | undefined {
|
||||
const generalError = errors.find((error) => !error.field);
|
||||
return generalError ? generalError.message || getErrorMessage(generalError.code) : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard to check if error is an APIError array
|
||||
* Provides runtime type safety instead of type assertions
|
||||
* @param error Unknown error object
|
||||
* @returns true if error is APIError[]
|
||||
*/
|
||||
export function isAPIErrorArray(error: unknown): error is APIError[] {
|
||||
if (!Array.isArray(error)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if all elements match APIError structure
|
||||
return error.every(
|
||||
(e) =>
|
||||
typeof e === 'object' &&
|
||||
e !== null &&
|
||||
'code' in e &&
|
||||
'message' in e &&
|
||||
typeof e.code === 'string' &&
|
||||
typeof e.message === 'string' &&
|
||||
// field is optional
|
||||
(!('field' in e) || typeof e.field === 'string')
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user