Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 2162eca4c6 Phone field input throws unhandled INVALID_COUNTRY when parsing phone numbers
https://sonarly.com/issue/2690?type=bug

The PhonesFieldInput component calls `parsePhoneNumber()` from libphonenumber-js without a try/catch. This function throws `ParseError('INVALID_COUNTRY')` for phone numbers whose country cannot be determined, crashing the UI when a user presses Enter on a phone field.

Fix: The fix wraps the `parsePhoneNumber(input)` call in a `try/catch` block inside the `formatInput` callback in `PhonesFieldInput.tsx`.

**Root cause recap:** `parsePhoneNumber` from `libphonenumber-js` is actually `parsePhoneNumberWithError` — it throws a `ParseError('INVALID_COUNTRY')` rather than returning `undefined` when the country cannot be determined. The original code had `if (phone !== undefined)` guard, revealing the developer expected non-throwing behavior, but no try/catch was in place.

**The fix:**

```typescript file=packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/components/PhonesFieldInput.tsx lines=183-200
        try {
          const phone = parsePhoneNumber(input);
          if (phone !== undefined) {
            return {
              number: phone.nationalNumber,
              callingCode: `+${phone.countryCallingCode}`,
              countryCode: phone.country as string,
            };
          }
        } catch {
          // parsePhoneNumber throws ParseError for inputs with unrecognized country
        }

        return {
          number: '',
          callingCode: '',
          countryCode: '',
        };
```

This matches the exact pattern used at every other `parsePhoneNumber` call site in the codebase:
- `PhoneDisplay.tsx` lines 23–33: try/catch
- `PhonesDisplay.tsx` lines 68–74: try/catch via `parsePhoneNumberOrReturnInvalidValue`
- `phoneSchema.ts` lines 6–10: try/catch

When `parsePhoneNumber` throws (e.g., for a local number without an international prefix like `+49`), the catch block swallows the `ParseError` and execution falls through to the existing fallback `return { number: '', callingCode: '', countryCode: '' }`. The `validateInput` callback (using `phoneSchema`) will then correctly mark the input as invalid, so no bad data is saved. Valid international numbers continue to parse and format as before.
2026-03-05 14:26:14 +00:00
@@ -180,13 +180,17 @@ export const PhonesFieldInput = () => {
};
}
const phone = parsePhoneNumber(input);
if (phone !== undefined) {
return {
number: phone.nationalNumber,
callingCode: `+${phone.countryCallingCode}`,
countryCode: phone.country as string,
};
try {
const phone = parsePhoneNumber(input);
if (phone !== undefined) {
return {
number: phone.nationalNumber,
callingCode: `+${phone.countryCallingCode}`,
countryCode: phone.country as string,
};
}
} catch {
// parsePhoneNumber throws ParseError for inputs with unrecognized country
}
return {