LocalRegex uses the LocalRegexException class to report errors when validation or formatting operations fail. This provides structured error handling for your application.
LocalRegex throws LocalRegexException in the following scenarios:
formatNumber() - Invalid phone number
Thrown when attempting to format a phone number that doesn’t match any valid Zimbabwean phone number pattern (mobile, VoIP, or landline).Error message: "Phone number is not valid!"
formatID() - Invalid national ID
Thrown when attempting to format a national ID that doesn’t match the valid Zimbabwean national ID pattern.Error message: "National ID is not valid"
The formatNumber() and formatID() methods throw exceptions for invalid input. Always validate input first or use try-catch blocks to handle errors gracefully.
import 'package:localregex/localregex.dart';void main() { final phoneNumber = '077721338'; // Invalid: too short try { final formatted = phoneNumber.formatNumber( formatType: FormatType.countryCode, ); print('Formatted: $formatted'); } on LocalRegexException catch (e) { print('Error: ${e.message}'); // Output: Error: Phone number is not valid! }}
import 'package:localregex/localregex.dart';void main() { final nationalId = '12345678'; // Invalid format try { final formatted = nationalId.formatID(); print('Formatted: $formatted'); } on LocalRegexException catch (e) { print('Error: ${e.message}'); // Output: Error: National ID is not valid }}
import 'package:localregex/localregex.dart';String? safeFormatID(String nationalId) { // Validate first if (!LocalRegex.isZimID(nationalId)) { return null; // Invalid national ID } // Safe to format return nationalId.formatID();}void main() { final result = safeFormatID('75-511218 K44'); if (result != null) { print('Formatted: $result'); // Output: Formatted: 75-511218 K 44 } else { print('Invalid national ID'); }}
When processing multiple inputs, collect all errors rather than stopping at the first failure. This provides better user experience by showing all issues at once.